Search in sources :

Example 16 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method connectInstantlyIfNoFeaturesNeeded.

/**
 * This test makes sure that if no server features need to be negotiated, we are not even
 * sending a request and completing the connect phase immediately.
 */
@Test
void connectInstantlyIfNoFeaturesNeeded() {
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.emptySet());
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.runPendingTasks();
    assertTrue(connect.isSuccess());
    assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method decodeNonSuccessfulHelloResponse.

/**
 * This test checks that a non-successful response is properly handled.
 */
@Test
void decodeNonSuccessfulHelloResponse() {
    Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.TCPNODELAY, ServerFeature.XATTR, ServerFeature.XERROR, ServerFeature.SELECT_BUCKET, ServerFeature.SNAPPY, ServerFeature.TRACING);
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.HELLO.opcode(), true, false, true);
    assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ByteBuf response = decodeHexDump(readResource("error_hello_response.txt", FeatureNegotiatingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(2, eventBus.publishedEvents().size());
    FeaturesNegotiationFailedEvent failureEvent = (FeaturesNegotiationFailedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.WARN, failureEvent.severity());
    assertEquals("HELLO Negotiation failed (Status 0x1)", failureEvent.description());
    FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
    assertEquals("Negotiated []", event.description());
    assertEquals(Event.Severity.DEBUG, event.severity());
    Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
    assertTrue(serverFeatures.isEmpty());
    assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) FeaturesNegotiationFailedEvent(com.couchbase.client.core.cnc.events.io.FeaturesNegotiationFailedEvent) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) FeaturesNegotiatedEvent(com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method decodeAndIgnoreNonAskedForFeaturesInResponse.

/**
 * Should the server return non-asked-for features, ignore them.
 */
@Test
void decodeAndIgnoreNonAskedForFeaturesInResponse() {
    Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.SNAPPY, ServerFeature.TRACING);
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.HELLO.opcode(), true, false, true);
    assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ByteBuf response = decodeHexDump(readResource("success_hello_response.txt", FeatureNegotiatingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(2, eventBus.publishedEvents().size());
    UnsolicitedFeaturesReturnedEvent unsolicitedEvent = (UnsolicitedFeaturesReturnedEvent) eventBus.publishedEvents().get(0);
    assertEquals("Received unsolicited features during HELLO [TCPNODELAY, XATTR, XERROR, SELECT_BUCKET]", unsolicitedEvent.description());
    assertEquals(Event.Severity.WARN, unsolicitedEvent.severity());
    FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
    assertEquals("Negotiated [SNAPPY, TRACING]", event.description());
    Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
    assertEquals(toNegotiate, serverFeatures);
    assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) UnsolicitedFeaturesReturnedEvent(com.couchbase.client.core.cnc.events.io.UnsolicitedFeaturesReturnedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) FeaturesNegotiatedEvent(com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method failConnectIfPromiseTimesOut.

/**
 * This test makes sure that the timer fires if the connect future is not completed
 * otherwise.
 */
@Test
void failConnectIfPromiseTimesOut() throws Exception {
    Duration timeout = Duration.ofMillis(100);
    when(timeoutConfig.connectTimeout()).thenReturn(timeout);
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.singleton(ServerFeature.TRACING));
    channel.pipeline().addLast(handler);
    final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    Thread.sleep(timeout.toMillis() + 5);
    channel.runScheduledPendingTasks();
    assertTrue(connect.isDone());
    assertTrue(connect.cause() instanceof TimeoutException);
    assertEquals("KV Feature Negotiation timed out after 100ms", connect.cause().getMessage());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Duration(java.time.Duration) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method propagateConnectFailureFromDownstream.

/**
 * This test verifies that if a downstream promise fails that the error
 * is propagated through the captured promise.
 */
@Test
void propagateConnectFailureFromDownstream() {
    final Exception connectException = new Exception("I failed");
    ChannelDuplexHandler failingHandler = new ChannelDuplexHandler() {

        @Override
        public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
            promise.setFailure(connectException);
        }
    };
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.singleton(ServerFeature.TRACING));
    channel.pipeline().addLast(failingHandler).addLast(handler);
    ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertEquals(connectException, connect.awaitUninterruptibly().cause());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ChannelDuplexHandler(com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)20 InetSocketAddress (java.net.InetSocketAddress)20 Test (org.junit.jupiter.api.Test)20 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)11 TimeoutException (java.util.concurrent.TimeoutException)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 ChannelDuplexHandler (com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler)4 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)4 ChannelPromise (com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)4 SocketAddress (java.net.SocketAddress)4 ErrorMapLoadedEvent (com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent)3 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)3 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)3 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)2 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)2 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)2 Authenticator (com.couchbase.client.core.env.Authenticator)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2