use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel 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();
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class ManagerMessageHandlerTest method returnsNewConfigsWhenChunked.
/**
* Configs can come in all shapes and sizes chunked, but only after the 4 newlines are pushed by the cluster
* the config should be propagated into the flux.
*/
@Test
void returnsNewConfigsWhenChunked() 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 List<String> configsPushed = Collections.synchronizedList(new ArrayList<>());
final AtomicBoolean terminated = new AtomicBoolean(false);
Thread listener = new Thread(() -> completedResponse.configs().subscribe(configsPushed::add, (e) -> {
}, () -> terminated.set(true)));
listener.setDaemon(true);
listener.start();
ByteBuf fullContent = Unpooled.copiedBuffer(readResource("terse_stream_two_configs.json", ManagerMessageHandlerTest.class), StandardCharsets.UTF_8);
while (fullContent.readableBytes() > 0) {
int len = new Random().nextInt(fullContent.readableBytes() + 1);
if (len == 0) {
continue;
}
channel.writeInbound(new DefaultHttpContent(fullContent.readBytes(len)));
}
waitUntilCondition(() -> configsPushed.size() >= 1);
for (String config : configsPushed) {
assertTrue(config.startsWith("{"));
assertTrue(config.endsWith("}"));
}
assertFalse(terminated.get());
channel.writeInbound(new DefaultLastHttpContent());
waitUntilCondition(terminated::get);
ReferenceCountUtil.release(fullContent);
channel.close().awaitUninterruptibly();
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class QueryMessageHandlerTest method handlesConcurrentInFlightRequest.
@Test
void handlesConcurrentInFlightRequest() {
BaseEndpoint endpoint = mock(BaseEndpoint.class);
EmbeddedChannel channel = new EmbeddedChannel(new QueryMessageHandler(endpoint, ENDPOINT_CTX));
byte[] query = "doesn'tmatter".getBytes(CharsetUtil.UTF_8);
QueryRequest request1 = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, FailFastRetryStrategy.INSTANCE, CORE_CTX.authenticator(), "statement", query, true, null, null, null, null, null);
QueryRequest request2 = new QueryRequest(ENV.timeoutConfig().queryTimeout(), CORE_CTX, FailFastRetryStrategy.INSTANCE, CORE_CTX.authenticator(), "statement", query, true, null, null, null, null, null);
channel.writeAndFlush(request1);
channel.writeAndFlush(request2);
waitUntilCondition(() -> request2.context().retryAttempts() > 0);
verify(endpoint, times(1)).decrementOutstandingRequests();
channel.finishAndReleaseAll();
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method emitsEventOnFailedDisconnect.
/**
* If the disconnect failed for some reason, make sure the proper warning event
* is raised and captured.
*/
@Test
void emitsEventOnFailedDisconnect() {
final Throwable expectedCause = new Exception("something failed");
EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
promise.tryFailure(expectedCause);
}
});
InstrumentedEndpoint endpoint = connectSuccessfully(channel);
endpoint.disconnect();
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
assertEquals(2, eventBus.publishedEvents().size());
assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectedEvent);
EndpointDisconnectionFailedEvent event = (EndpointDisconnectionFailedEvent) eventBus.publishedEvents().get(1);
assertEquals(expectedCause, event.cause());
assertEquals(Event.Severity.WARN, event.severity());
}
use of com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method writeAndFlushToChannelIfFullyConnected.
/**
* If we are fully connected and the circuit breaker is closed, make sure that we can
* write the request into the channel and it is flushed as well.
*/
@Test
@SuppressWarnings({ "unchecked" })
void writeAndFlushToChannelIfFullyConnected() {
EmbeddedChannel channel = new EmbeddedChannel();
InstrumentedEndpoint endpoint = connectSuccessfully(channel);
assertEquals(0, endpoint.lastResponseReceived());
Request<Response> request = mock(Request.class);
CompletableFuture<Response> response = new CompletableFuture<>();
when(request.response()).thenReturn(response);
when(request.context()).thenReturn(new RequestContext(ctx, request));
assertEquals(0, endpoint.outstandingRequests());
endpoint.send(request);
assertEquals(1, endpoint.outstandingRequests());
assertEquals(request, channel.readOutbound());
assertEquals(0, endpoint.lastResponseReceived());
response.complete(mock(Response.class));
endpoint.markRequestCompletion();
assertTrue(endpoint.lastResponseReceived() > 0);
assertEquals(0, endpoint.outstandingRequests());
}
Aggregations