Search in sources :

Example 6 with ByteBuf

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

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

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

the class FeatureNegotiatingHandlerTest method encodeAndSendHelloRequest.

/**
 * This test verifies that the sent hello request looks like it should.
 *
 * @param enabledFeatures parameterized input in the enabled features.
 */
@ParameterizedTest
@MethodSource("featureProvider")
void encodeAndSendHelloRequest(Set<ServerFeature> enabledFeatures) {
    FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, enabledFeatures);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
    channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.HELLO.opcode(), true, false, true);
    // sanity check json block
    assertTrue(ProtocolVerifier.key(writtenRequest).isPresent());
    String json = ProtocolVerifier.key(writtenRequest).get().toString(UTF_8);
    assertEquals("{\"a\":\"some/0.0.0\",\"i\":\"0000000000000001/0000000000000001\"}", json);
    // sanity check enabled features
    assertTrue(ProtocolVerifier.body(writtenRequest).isPresent());
    ByteBuf features = ProtocolVerifier.body(writtenRequest).get();
    assertEquals(enabledFeatures.size() * 2, features.readableBytes());
    ReferenceCountUtil.release(writtenRequest);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 9 with ByteBuf

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

the class KeyValueMessageHandlerTest method closesChannelOnCertainStatusCodes.

/**
 * As part of the KV error map, certain status codes have been identified as "must close" on the channel
 * to avoid further problems.
 *
 * <p>This test makes sure that on all of those codes, the channel gets closed accordingly.</p>
 */
@Test
void closesChannelOnCertainStatusCodes() {
    Set<MemcacheProtocol.Status> closeOnThese = EnumSet.of(MemcacheProtocol.Status.INTERNAL_SERVER_ERROR, MemcacheProtocol.Status.NO_BUCKET, MemcacheProtocol.Status.NOT_INITIALIZED);
    for (MemcacheProtocol.Status status : closeOnThese) {
        EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
        try {
            GetRequest request1 = new GetRequest("key", Duration.ofSeconds(1), CTX, CID, FailFastRetryStrategy.INSTANCE, null);
            channel.writeOutbound(request1);
            ByteBuf getResponse = MemcacheProtocol.response(channel.alloc(), MemcacheProtocol.Opcode.GET, (byte) 0, status.status(), request1.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
            channel.writeInbound(getResponse);
            assertFalse(channel.isOpen());
            assertEquals(0, getResponse.refCnt());
        } finally {
            channel.finishAndReleaseAll();
        }
    }
}
Also used : 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)

Example 10 with ByteBuf

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

the class KeyValueMessageHandlerTest method attemptsRetryIfInstructedByErrorMap.

/**
 * If an unknown response code is returned and the consulted error map indicates a retry, it should be passed to
 * the retry orchestrator for correct handling.
 */
@Test
void attemptsRetryIfInstructedByErrorMap() {
    EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
    ErrorMap errorMap = mock(ErrorMap.class);
    Map<Short, ErrorMap.ErrorCode> errors = new HashMap<>();
    ErrorMap.ErrorCode code = mock(ErrorMap.ErrorCode.class);
    errors.put((short) 0xFF, code);
    Set<ErrorMap.ErrorAttribute> attributes = new HashSet<>();
    attributes.add(ErrorMap.ErrorAttribute.RETRY_NOW);
    when(code.attributes()).thenReturn(attributes);
    when(errorMap.errors()).thenReturn(errors);
    channel.attr(ChannelAttributes.ERROR_MAP_KEY).set(errorMap);
    channel.pipeline().fireChannelActive();
    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, (short) 0xFF, request.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
        channel.writeInbound(getResponse);
        ExecutionException exception = assertThrows(ExecutionException.class, () -> request.response().get());
        assertTrue(exception.getCause() instanceof RequestCanceledException);
        assertEquals("NO_MORE_RETRIES", request.cancellationReason().identifier());
        assertEquals(RetryReason.KV_ERROR_MAP_INDICATED, request.cancellationReason().innerReason());
        assertEquals(0, getResponse.refCnt());
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : HashMap(java.util.HashMap) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) 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