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());
}
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);
}
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);
}
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();
}
}
}
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();
}
}
Aggregations