use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method disconnectsEndpointOnRedialTimeout.
/**
* When a http streaming connection is outstanding, the handler needs to notify the endpoint that it disconnects
* itself in an orderly manner.
*/
@Test
void disconnectsEndpointOnRedialTimeout() throws Exception {
CoreEnvironment env = CoreEnvironment.builder().ioConfig(IoConfig.configIdleRedialTimeout(Duration.ofSeconds(2))).build();
try {
CoreContext ctx = new CoreContext(mock(Core.class), 1, env, PasswordAuthenticator.create(USER, PASS));
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EndpointContext endpointContext = mock(EndpointContext.class);
when(endpointContext.environment()).thenReturn(env);
when(endpoint.context()).thenReturn(endpointContext);
EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
channel.write(request);
HttpRequest outboundHeader = channel.readOutbound();
assertEquals(HttpMethod.GET, outboundHeader.method());
assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
HttpContent httpContent = new DefaultHttpContent(Unpooled.copiedBuffer("{}\n\n\n\n", StandardCharsets.UTF_8));
channel.writeInbound(httpResponse, httpContent);
BucketConfigStreamingResponse response = request.response().get();
assertEquals("{}", response.configs().blockFirst());
waitUntilCondition(() -> {
channel.runPendingTasks();
MockingDetails mockingDetails = Mockito.mockingDetails(endpoint);
return mockingDetails.getInvocations().stream().anyMatch(i -> i.getMethod().getName().equals("disconnect"));
});
channel.finish();
} finally {
env.shutdown();
}
}
use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse in project couchbase-jvm-clients by couchbase.
the class QueryChunkResponseParserTest method runAndThrow.
/**
* Loads the mocked error response and throws the error which is then caught by the calling methods.
*
* @param file the relative filepath without the .json ending and the failure prefix.
*/
private static void runAndThrow(final String file) {
String input = readResource("failure_" + file + ".json", QueryChunkResponseParserTest.class);
// At the moment the query chunk response parser does not care about the http status code and only
// prints it as part of the error context, so we can pick whatever we want here.
HttpResponse header = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
throw QueryChunkResponseParser.errorsToThrowable(input.getBytes(StandardCharsets.UTF_8), header, null);
}
use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse 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.DefaultHttpResponse in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method shouldKeepTryingIfHttpResponseHasNonSuccessStatus.
/**
* If a connection cannot be established, the client should keep trying until it finds one.
*/
@Test
void shouldKeepTryingIfHttpResponseHasNonSuccessStatus() {
final AtomicInteger streamingRequestAttempts = new AtomicInteger();
doAnswer(i -> {
streamingRequestAttempts.incrementAndGet();
BucketConfigStreamingRequest request = i.getArgument(0);
HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
BucketConfigStreamingResponse response = request.decode(httpResponse, null);
request.succeed(response);
return null;
}).when(core).send(any(BucketConfigStreamingRequest.class));
refresher.register("bucketName").block();
waitUntilCondition(() -> streamingRequestAttempts.get() >= 2);
}
use of com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method closesStreamIfChannelClosed.
/**
* When a config stream is being processed and the underlying channel closes, the stream should also be closed
* so the upper level can re-establish a new one.
*/
@Test
void closesStreamIfChannelClosed() throws Exception {
CoreContext ctx = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create(USER, PASS));
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EndpointContext endpointContext = mock(EndpointContext.class);
when(endpointContext.environment()).thenReturn(ENV);
when(endpoint.context()).thenReturn(endpointContext);
EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
channel.write(request);
HttpRequest outboundHeader = channel.readOutbound();
assertEquals(HttpMethod.GET, outboundHeader.method());
assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
CompletableFuture<BucketConfigStreamingResponse> response = request.response();
assertFalse(response.isDone());
HttpResponse inboundResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
channel.writeInbound(inboundResponse);
BucketConfigStreamingResponse completedResponse = request.response().get();
final AtomicBoolean terminated = new AtomicBoolean(false);
Thread listener = new Thread(() -> completedResponse.configs().subscribe((v) -> {
}, (e) -> {
}, () -> terminated.set(true)));
listener.setDaemon(true);
listener.start();
assertFalse(terminated.get());
channel.close().awaitUninterruptibly();
waitUntilCondition(terminated::get);
channel.finish();
}
Aggregations