Search in sources :

Example 21 with EmbeddedChannel

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();
}
Also used : HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ArrayList(java.util.ArrayList) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AfterAll(org.junit.jupiter.api.AfterAll) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) CoreContext(com.couchbase.client.core.CoreContext) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) MockingDetails(org.mockito.MockingDetails) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) Util.readResource(com.couchbase.client.test.Util.readResource) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) IoConfig(com.couchbase.client.core.env.IoConfig) Mockito.mock(org.mockito.Mockito.mock) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 22 with EmbeddedChannel

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();
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ArrayList(java.util.ArrayList) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AfterAll(org.junit.jupiter.api.AfterAll) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) CoreContext(com.couchbase.client.core.CoreContext) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) MockingDetails(org.mockito.MockingDetails) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) Util.readResource(com.couchbase.client.test.Util.readResource) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) IoConfig(com.couchbase.client.core.env.IoConfig) Mockito.mock(org.mockito.Mockito.mock) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Random(java.util.Random) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) Core(com.couchbase.client.core.Core) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test)

Example 23 with EmbeddedChannel

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();
}
Also used : QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 24 with EmbeddedChannel

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());
}
Also used : EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(com.couchbase.client.core.deps.io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) ChannelException(com.couchbase.client.core.deps.io.netty.channel.ChannelException) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) Test(org.junit.jupiter.api.Test)

Example 25 with EmbeddedChannel

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());
}
Also used : Response(com.couchbase.client.core.msg.Response) CompletableFuture(java.util.concurrent.CompletableFuture) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)32 Test (org.junit.jupiter.api.Test)23 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)15 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)13 Core (com.couchbase.client.core.Core)8 CoreContext (com.couchbase.client.core.CoreContext)8 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)8 BaseEndpoint (com.couchbase.client.core.endpoint.BaseEndpoint)7 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)6 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)6 DefaultHttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse)5 DefaultLastHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent)5 HttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent)5 HttpResponse (com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)4 DefaultHttpContent (com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent)4 HostAndPort (com.couchbase.client.core.util.HostAndPort)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4