Search in sources :

Example 1 with ChannelFuture

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

the class ErrorMapLoadingHandlerTest 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);
        }
    };
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    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)

Example 2 with ChannelFuture

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

the class ErrorMapLoadingHandlerTest method decodeUnsuccessfulResponse.

/**
 * Make sure that when the server returns a non-successful response we still handle
 * it and not crash.
 */
@Test
void decodeUnsuccessfulResponse() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.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.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("error_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    ErrorMapLoadingFailedEvent event = (ErrorMapLoadingFailedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.WARN, event.severity());
    assertEquals("KV Error Map Negotiation failed (Status 0x1)", event.description());
    assertNull(channel.attr(ChannelAttributes.ERROR_MAP_KEY).get());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ErrorMapLoadingFailedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadingFailedEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 3 with ChannelFuture

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

the class FeatureNegotiatingHandlerTest method decodeAndPropagateSuccessHelloResponse.

/**
 * This test verifies that a successful hello response is properly handled.
 */
@Test
void decodeAndPropagateSuccessHelloResponse() {
    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("success_hello_response.txt", FeatureNegotiatingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(0);
    assertEquals("Negotiated [TCPNODELAY, XATTR, XERROR, SELECT_BUCKET, SNAPPY, TRACING]", event.description());
    assertEquals(Event.Severity.DEBUG, event.severity());
    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) 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 4 with ChannelFuture

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

the class SaslListMechanismsHandlerTest method decodesSuccessfulSaslMechsList.

@Test
void decodesSuccessfulSaslMechsList() {
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.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.SASL_LIST_MECHS.opcode(), false, false, false);
    assertNotNull(channel.pipeline().get(SaslListMechanismsHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_sasl_list_mechs_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    Set<SaslMechanism> saslMechanisms = channel.attr(ChannelAttributes.SASL_MECHS_KEY).get();
    assertEquals(saslMechanisms, EnumSet.of(SaslMechanism.PLAIN, SaslMechanism.SCRAM_SHA1, SaslMechanism.SCRAM_SHA256, SaslMechanism.SCRAM_SHA512));
    assertTrue(eventBus.publishedEvents().get(0) instanceof SaslMechanismsListedEvent);
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) SaslMechanismsListedEvent(com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent) InetSocketAddress(java.net.InetSocketAddress) SaslMechanism(com.couchbase.client.core.env.SaslMechanism) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 5 with ChannelFuture

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

the class SaslListMechanismsHandlerTest method failConnectIfSaslMechsListEmpty.

@Test
void failConnectIfSaslMechsListEmpty() {
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.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.SASL_LIST_MECHS.opcode(), false, false, false);
    assertNotNull(channel.pipeline().get(SaslListMechanismsHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_sasl_list_mechs_empty_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertFalse(connectFuture.isSuccess());
    AuthenticationFailureException cause = (AuthenticationFailureException) connectFuture.cause();
    assertTrue(cause.getMessage().contains("Received empty mechanism list from server"));
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

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