Search in sources :

Example 11 with EmbeddedChannel

use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.

the class PasswordAuthenticatorTest method shouldOnlyNegotiatePlainWhenTlsEnabled.

/**
 * Regression test for JVMCBC-890.
 */
@Test
void shouldOnlyNegotiatePlainWhenTlsEnabled() {
    PasswordAuthenticator authenticator = PasswordAuthenticator.create("user", "pass");
    CoreEnvironment tlsEnvironment = CoreEnvironment.builder().securityConfig(SecurityConfig.enableTls(true).trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)).build();
    try {
        EndpointContext ctx = mock(EndpointContext.class);
        when(ctx.environment()).thenReturn(tlsEnvironment);
        EmbeddedChannel channel = new EmbeddedChannel();
        authenticator.authKeyValueConnection(ctx, channel.pipeline());
        SaslAuthenticationHandler handler = channel.pipeline().get(SaslAuthenticationHandler.class);
        assertEquals(EnumSet.of(SaslMechanism.PLAIN), handler.allowedMechanisms());
    } finally {
        tlsEnvironment.shutdown();
    }
}
Also used : SaslAuthenticationHandler(com.couchbase.client.core.io.netty.kv.SaslAuthenticationHandler) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 12 with EmbeddedChannel

use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method disconnectOverridesConnectCompletion.

/**
 * This test makes sure that even if we connect successfully, if there has been a
 * disconnect signal in the meantime we need to properly close it all and not end
 * in a connect-disconnect limbo.
 */
@Test
void disconnectOverridesConnectCompletion() {
    final CompletableFuture<Channel> cf = new CompletableFuture<>();
    InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
    endpoint.connect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTING);
    endpoint.disconnect();
    EmbeddedChannel channel = new EmbeddedChannel();
    cf.complete(channel);
    waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
    assertTrue(eventBus.publishedEvents().size() >= 2);
    assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectionIgnoredEvent);
    assertTrue(eventBus.publishedEvents().get(1) instanceof EndpointDisconnectedEvent);
    assertEquals("Endpoint disconnected successfully", eventBus.publishedEvents().get(1).description());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Test(org.junit.jupiter.api.Test)

Example 13 with EmbeddedChannel

use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method retryOnFailureUntilEventuallyConnected.

/**
 * The {@link #retryOnTimeoutUntilEventuallyConnected()} tests that a netty
 * channel future does not return at all and we reconnect, this one tests that
 * if netty returns with a failure we keep reconnecting until it succeeds.
 */
@Test
void retryOnFailureUntilEventuallyConnected() {
    final AtomicInteger invocationAttempt = new AtomicInteger();
    InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> invocationAttempt.incrementAndGet() > 3 ? Mono.just(new EmbeddedChannel()) : Mono.error(new ChannelException("Could not connect for some reason")));
    endpoint.connect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
    assertEquals(4, eventBus.publishedEvents().size());
    int warnings = 0;
    int debug = 0;
    for (Event event : eventBus.publishedEvents()) {
        if (event.severity() == Event.Severity.WARN) {
            assertTrue(event instanceof EndpointConnectionFailedEvent);
            warnings++;
        } else if (event.severity() == Event.Severity.DEBUG) {
            assertTrue(event instanceof EndpointConnectedEvent);
            debug++;
        } else {
            throw new RuntimeException("Unexpected Event" + event);
        }
    }
    assertEquals(3, warnings);
    assertEquals(1, debug);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Event(com.couchbase.client.core.cnc.Event) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) ChannelException(com.couchbase.client.core.deps.io.netty.channel.ChannelException) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) Test(org.junit.jupiter.api.Test)

Example 14 with EmbeddedChannel

use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method retryOnTimeoutUntilEventuallyConnected.

/**
 * This test fakes a situation where the channel future from netty would simply not return
 * at all and time out, and the client would resubscribe. Then at some future attempt the
 * future returns fine and we should end up in a connected state and ready to go.
 */
@Test
void retryOnTimeoutUntilEventuallyConnected() {
    SimpleEventBus eventBus = new SimpleEventBus(true);
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
    ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
    try {
        final CompletableFuture<Channel> cf = new CompletableFuture<>();
        InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
        endpoint.connect();
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
        EmbeddedChannel channel = new EmbeddedChannel();
        cf.complete(channel);
        waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
        assertTrue(eventBus.publishedEvents().size() >= 3);
        boolean failedFound = false;
        boolean successFound = false;
        for (Event event : eventBus.publishedEvents()) {
            if (event instanceof EndpointConnectionFailedEvent) {
                assertEquals(Event.Severity.WARN, event.severity());
                assertEquals(Duration.ofMillis(10), event.duration());
                failedFound = true;
            } else if (event instanceof EndpointConnectedEvent) {
                assertEquals(Event.Severity.DEBUG, event.severity());
                assertTrue(event.duration().toNanos() > 0);
                successFound = true;
            }
        }
        assertTrue(failedFound);
        assertTrue(successFound);
    } finally {
        env.shutdown();
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Event(com.couchbase.client.core.cnc.Event) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) Core(com.couchbase.client.core.Core) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) Test(org.junit.jupiter.api.Test)

Example 15 with EmbeddedChannel

use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.

the class MemcacheProtocolDecodeHandlerTest method shouldDecodeInput.

/**
 * Runs through all provided inputs and makes sure even if they are sliced
 * and diced they come out whole at the end.
 *
 * <p>For now each input is tried 100 times with different chunk sizes to increase
 * the chance of finding a problem, this can be increased or decreased as
 * needed.</p>
 */
@ParameterizedTest(name = "{0}")
@MethodSource("inputProvider")
void shouldDecodeInput(final InputHolder inputHolder) {
    final EmbeddedChannel channel = new EmbeddedChannel(new MemcacheProtocolDecodeHandler());
    try {
        for (int i = 0; i < 100; i++) {
            ByteBuf copy = inputHolder.input.copy();
            writeAsRandomChunks(channel, copy);
            ByteBuf read = channel.readInbound();
            copy.resetReaderIndex();
            assertEquals(inputHolder.input, read);
            ReferenceCountUtil.release(copy);
            ReferenceCountUtil.release(read);
        }
        ReferenceCountUtil.release(inputHolder.input);
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)32 Test (org.junit.jupiter.api.Test)23 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)15 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)13 Core (com.couchbase.client.core.Core)8 CoreContext (com.couchbase.client.core.CoreContext)8 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)8 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)7 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)5 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)5 HttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)5 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)4 DefaultHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent)4 HostAndPort (com.couchbase.client.core.util.HostAndPort)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4