use of com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent in project couchbase-jvm-clients by couchbase.
the class ErrorMapLoadingHandler method channelRead.
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
if (msg instanceof ByteBuf) {
if (successful((ByteBuf) msg)) {
Optional<ErrorMap> loadedMap = extractErrorMap((ByteBuf) msg);
loadedMap.ifPresent(errorMap -> ctx.channel().attr(ChannelAttributes.ERROR_MAP_KEY).set(errorMap));
endpointContext.environment().eventBus().publish(new ErrorMapLoadedEvent(ioContext, latency.orElse(Duration.ZERO), loadedMap));
} else {
endpointContext.environment().eventBus().publish(new ErrorMapLoadingFailedEvent(ioContext, latency.orElse(Duration.ZERO), status((ByteBuf) msg)));
}
interceptedConnectPromise.trySuccess();
ctx.pipeline().remove(this);
} else {
interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response " + "type on channel read, this is a bug - please report. " + msg));
}
ReferenceCountUtil.release(msg);
}
use of com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent in project couchbase-jvm-clients by couchbase.
the class ErrorMapLoadingHandlerTest method decodeSuccessfulErrorMapV2.
/**
* Verify that we can decode v2 of the error map.
*/
@Test
void decodeSuccessfulErrorMapV2() {
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("success_errormapv2_response.txt", ErrorMapLoadingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(1, eventBus.publishedEvents().size());
ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.DEBUG, event.severity());
Optional<ErrorMap> maybeMap = event.errorMap();
assertTrue(maybeMap.isPresent());
assertNotNull(maybeMap.get());
ErrorMap errorMap = channel.attr(ChannelAttributes.ERROR_MAP_KEY).get();
assertEquals(errorMap, maybeMap.get());
}
use of com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent in project couchbase-jvm-clients by couchbase.
the class ErrorMapLoadingHandlerTest method decodeSuccessfulResponseWithEmptyMap.
/**
* This seems to be a real edge case, but when the server returns a successful
* response but with no map, we should still handle it gracefully.
*/
@Test
void decodeSuccessfulResponseWithEmptyMap() {
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("success_empty_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(2, eventBus.publishedEvents().size());
ErrorMapUndecodableEvent undecodableEvent = (ErrorMapUndecodableEvent) eventBus.publishedEvents().get(0);
assertEquals("KV Error Map loaded but undecodable. " + "Message: \"No content in response\", Content: \"\"", undecodableEvent.description());
ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(1);
assertEquals(Event.Severity.DEBUG, event.severity());
Optional<ErrorMap> maybeMap = event.errorMap();
assertFalse(maybeMap.isPresent());
}
use of com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent in project couchbase-jvm-clients by couchbase.
the class ErrorMapLoadingHandlerTest method decodeSuccessfulErrorMap.
/**
* Verify that when a good error map comes back, we parse and return it properly.
*/
@Test
void decodeSuccessfulErrorMap() {
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("success_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(1, eventBus.publishedEvents().size());
ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.DEBUG, event.severity());
Optional<ErrorMap> maybeMap = event.errorMap();
assertTrue(maybeMap.isPresent());
assertNotNull(maybeMap.get());
ErrorMap errorMap = channel.attr(ChannelAttributes.ERROR_MAP_KEY).get();
assertEquals(errorMap, maybeMap.get());
}
Aggregations