use of com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent in project couchbase-jvm-clients by couchbase.
the class QueryMessageHandlerTest method reportsCompletionOnlyOnceStreamingEnds.
/**
* To avoid accidentally pipelining requests, we need to make sure that the handler only reports completion
* once the streaming is fully completed, and not just the response completable future initially completed.
*
* <p>We send a request, make sure it is encoded correctly and then stream back the chunks, making sure the
* final completion is only signaled once the last chunk arrived.</p>
*/
@Test
void reportsCompletionOnlyOnceStreamingEnds() {
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EmbeddedChannel channel = new EmbeddedChannel(new QueryMessageHandler(endpoint, ENDPOINT_CTX));
byte[] query = "doesn'tmatter".getBytes(CharsetUtil.UTF_8);
QueryRequest request = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, ENV.retryStrategy(), CORE_CTX.authenticator(), "statement", query, false, null, null, null, null, null);
channel.writeAndFlush(request);
FullHttpRequest encodedRequest = channel.readOutbound();
assertEquals("doesn'tmatter", encodedRequest.content().toString(CharsetUtil.UTF_8));
ReferenceCountUtil.release(encodedRequest);
verify(endpoint, never()).markRequestCompletion();
assertFalse(request.response().isDone());
ByteBuf fullResponse = Unpooled.copiedBuffer(readResource("success_response.json", QueryMessageHandlerTest.class), CharsetUtil.UTF_8);
// send the header back first
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.writeInbound(response);
assertFalse(request.response().isDone());
// send first chunk, should complete the future but not mark the endpoint completion
HttpContent firstChunk = new DefaultHttpContent(fullResponse.readBytes(500));
channel.writeInbound(firstChunk);
assertTrue(request.response().isDone());
verify(endpoint, never()).markRequestCompletion();
// send the second chunk, no change
HttpContent secondChunk = new DefaultHttpContent(fullResponse.readBytes(500));
channel.writeInbound(secondChunk);
verify(endpoint, never()).markRequestCompletion();
// send the last chunk, mark for completion finally
LastHttpContent lastChunk = new DefaultLastHttpContent(fullResponse.readBytes(fullResponse.readableBytes()));
channel.writeInbound(lastChunk);
verify(endpoint, times(1)).markRequestCompletion();
channel.finishAndReleaseAll();
}
use of com.couchbase.client.core.deps.io.netty.handler.codec.http.LastHttpContent in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpResponse) {
currentResponse = ((HttpResponse) msg);
if (isStreamingConfigRequest()) {
streamingResponse = (BucketConfigStreamingResponse) currentRequest.decode(currentResponse, null);
currentRequest.succeed(streamingResponse);
ctx.pipeline().addFirst(new IdleStateHandler(coreContext.environment().ioConfig().configIdleRedialTimeout().toMillis(), 0, 0, TimeUnit.MILLISECONDS));
}
} else if (msg instanceof HttpContent) {
currentContent.writeBytes(((HttpContent) msg).content());
if (isStreamingConfigRequest()) {
// there might be more than one config in the full batch, so keep iterating until all are pushed
while (true) {
String encodedConfig = currentContent.toString(StandardCharsets.UTF_8);
int separatorIndex = encodedConfig.indexOf("\n\n\n\n");
// if -1 is returned it means that no full config has been located yet, need to wait for more chunks
if (separatorIndex >= 0) {
String content = encodedConfig.substring(0, separatorIndex);
streamingResponse.pushConfig(content.trim());
currentContent.clear();
currentContent.writeBytes(encodedConfig.substring(separatorIndex + 4).getBytes(StandardCharsets.UTF_8));
} else {
break;
}
}
}
if (msg instanceof LastHttpContent) {
if (isStreamingConfigRequest()) {
streamingResponse.completeStream();
streamingResponse = null;
ctx.pipeline().remove(IdleStateHandler.class);
} else {
byte[] copy = new byte[currentContent.readableBytes()];
currentContent.readBytes(copy);
Response response = currentRequest.decode(currentResponse, copy);
currentRequest.succeed(response);
}
currentRequest = null;
if (endpoint != null) {
endpoint.markRequestCompletion();
}
}
} else {
ioContext.environment().eventBus().publish(new UnsupportedResponseTypeReceivedEvent(ioContext, msg));
}
ReferenceCountUtil.release(msg);
}
Aggregations