Search in sources :

Example 96 with ByteBuf

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

the class ErrorMapLoadingHandlerTest method propagatesChannelActiveAfterSendingInitialRequest.

/**
 * This test makes sure that after the initial request is sent, the channel active signal is
 * propagated so that we do not regress bootstrap pipelining functionality.
 */
@Test
void propagatesChannelActiveAfterSendingInitialRequest() {
    final ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    final AtomicBoolean channelActiveFired = new AtomicBoolean();
    channel.pipeline().addLast(handler).addLast(new SimpleChannelInboundHandler<ByteBuf>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            channelActiveFired.set(true);
        }
    });
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.class));
    channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    waitUntilCondition(channelActiveFired::get);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 97 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf 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 98 with ByteBuf

use of com.couchbase.client.core.deps.io.netty.buffer.ByteBuf 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 99 with ByteBuf

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

the class FeatureNegotiatingHandlerTest method propagatesChannelActiveAfterSendingInitialRequest.

/**
 * This test makes sure that after the initial request is sent, the channel active signal is
 * propagated so that we do not regress bootstrap pipelining functionality.
 */
@Test
void propagatesChannelActiveAfterSendingInitialRequest() {
    final FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.singleton(ServerFeature.SELECT_BUCKET));
    final AtomicBoolean channelActiveFired = new AtomicBoolean();
    channel.pipeline().addLast(handler).addLast(new SimpleChannelInboundHandler<ByteBuf>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            channelActiveFired.set(true);
        }
    });
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    waitUntilCondition(channelActiveFired::get);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 100 with ByteBuf

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

the class KeyValueMessageHandlerTest method retriesCertainResponseStatusCodes.

/**
 * Certain status codes are identified that should be passed to the retry orchestrator rathen than complete
 * the response right away.
 */
@Test
void retriesCertainResponseStatusCodes() {
    List<MemcacheProtocol.Status> retryOnThese = Arrays.asList(MemcacheProtocol.Status.LOCKED, MemcacheProtocol.Status.TEMPORARY_FAILURE, MemcacheProtocol.Status.SYNC_WRITE_IN_PROGRESS, MemcacheProtocol.Status.SYNC_WRITE_RE_COMMIT_IN_PROGRESS);
    List<RetryReason> retryReasons = Arrays.asList(RetryReason.KV_LOCKED, RetryReason.KV_TEMPORARY_FAILURE, RetryReason.KV_SYNC_WRITE_IN_PROGRESS, RetryReason.KV_SYNC_WRITE_RE_COMMIT_IN_PROGRESS);
    int i = 0;
    for (MemcacheProtocol.Status status : retryOnThese) {
        EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
        try {
            GetRequest request = new GetRequest("key", Duration.ofSeconds(1), CTX, CID, FailFastRetryStrategy.INSTANCE, null);
            channel.writeOutbound(request);
            ByteBuf getResponse = MemcacheProtocol.response(channel.alloc(), MemcacheProtocol.Opcode.GET, (byte) 0, status.status(), request.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
            channel.writeInbound(getResponse);
            assertEquals(CancellationReason.noMoreRetries(retryReasons.get(i)), request.cancellationReason());
            assertEquals(0, getResponse.refCnt());
            i++;
        } finally {
            channel.finishAndReleaseAll();
        }
    }
}
Also used : RetryReason(com.couchbase.client.core.retry.RetryReason) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)107 Test (org.junit.jupiter.api.Test)54 InetSocketAddress (java.net.InetSocketAddress)17 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)12 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)11 Duration (java.time.Duration)10 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)9 CoreContext (com.couchbase.client.core.CoreContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 CompressionConfig (com.couchbase.client.core.env.CompressionConfig)6 ResponseStatus (com.couchbase.client.core.msg.ResponseStatus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)5 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 ArrayList (java.util.ArrayList)5 DurabilityTimeoutCoercedEvent (com.couchbase.client.core.cnc.events.io.DurabilityTimeoutCoercedEvent)4 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)4 CompositeByteBuf (com.couchbase.client.core.deps.io.netty.buffer.CompositeByteBuf)4 FullHttpRequest (com.couchbase.client.core.deps.io.netty.handler.codec.http.FullHttpRequest)4 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)4