Search in sources :

Example 6 with Channel

use of com.couchbase.client.core.deps.io.netty.channel.Channel in project couchbase-jvm-clients by couchbase.

the class KeyValueChannelIntegrationTest method connectNoopAndDisconnect.

/**
 * This is the most simple kv test case one can do in a full-stack manner.
 *
 * <p>It connects to a kv socket, including all auth and bucket selection. It then
 * checks that the channel is opened properly and performs a NOOP and checks for a
 * successful result. Then it shuts everything down.</p>
 *
 * @throws Exception if waiting on the response fails.
 */
@Test
void connectNoopAndDisconnect() throws Exception {
    TestNodeConfig node = config().nodes().get(0);
    Bootstrap bootstrap = new Bootstrap().remoteAddress(node.hostname(), node.ports().get(Services.KV)).group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) {
            new KeyValueEndpoint.KeyValuePipelineInitializer(endpointContext, Optional.of(config().bucketname()), endpointContext.authenticator()).init(null, ch.pipeline());
        }
    });
    Channel channel = bootstrap.connect().awaitUninterruptibly().channel();
    assertTrue(channel.isActive());
    assertTrue(channel.isOpen());
    NoopRequest request = new NoopRequest(Duration.ZERO, endpointContext, null, CollectionIdentifier.fromDefault(config().bucketname()));
    channel.writeAndFlush(request);
    NoopResponse response = request.response().get(1, TimeUnit.SECONDS);
    assertTrue(response.status().success());
    channel.close().awaitUninterruptibly();
}
Also used : NioSocketChannel(com.couchbase.client.core.deps.io.netty.channel.socket.nio.NioSocketChannel) NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) SocketChannel(com.couchbase.client.core.deps.io.netty.channel.socket.SocketChannel) NioSocketChannel(com.couchbase.client.core.deps.io.netty.channel.socket.nio.NioSocketChannel) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) SocketChannel(com.couchbase.client.core.deps.io.netty.channel.socket.SocketChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) NioSocketChannel(com.couchbase.client.core.deps.io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 7 with Channel

use of com.couchbase.client.core.deps.io.netty.channel.Channel in project couchbase-jvm-clients by couchbase.

the class QueryMessageHandlerBackpressureTest method requestRecordsExplicitly.

/**
 * This test makes sure that even if the server returns a good bunch of data, each individual
 * chunk is requested by the caller explicitly.
 */
@Test
void requestRecordsExplicitly() throws Exception {
    EndpointContext endpointContext = new EndpointContext(core.context(), new HostAndPort("127.0.0.1", 1234), NoopCircuitBreaker.INSTANCE, ServiceType.QUERY, Optional.empty(), Optional.empty(), Optional.empty());
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    when(endpoint.pipelined()).thenReturn(false);
    Bootstrap client = new Bootstrap().channel(LocalChannel.class).group(new DefaultEventLoopGroup()).remoteAddress(new LocalAddress("s1")).handler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) {
            ch.pipeline().addLast(new HttpClientCodec()).addLast(new QueryMessageHandler(endpoint, endpointContext));
        }
    });
    Channel channel = client.connect().awaitUninterruptibly().channel();
    final List<byte[]> rows = Collections.synchronizedList(new ArrayList<>());
    QueryRequest request = new QueryRequest(Duration.ofSeconds(1), endpointContext, BestEffortRetryStrategy.INSTANCE, endpointContext.authenticator(), "select 1=1", "myquery".getBytes(UTF_8), true, null, null, null, null, null);
    channel.writeAndFlush(request);
    final QueryResponse response = request.response().get();
    assertEquals(0, rows.size());
    StepVerifier.create(response.rows().map(v -> new String(v.data(), UTF_8)), 0).thenRequest(1).expectNext("{\"foo\":1}").thenRequest(1).expectNext("{\"bar\":1}").thenRequest(2).expectNext("{\"faz\":1}", "{\"baz\":1}").thenRequest(4).expectNext("{\"fazz\":1}", "{\"bazz\":1}", "{\"fizz\":1}", "{\"bizz\":1}").expectComplete().verify();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) HttpHeaderNames(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpHeaderNames) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) AfterAll(org.junit.jupiter.api.AfterAll) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) ChannelInboundHandlerAdapter(com.couchbase.client.core.deps.io.netty.channel.ChannelInboundHandlerAdapter) BeforeAll(org.junit.jupiter.api.BeforeAll) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test) List(java.util.List) Optional(java.util.Optional) HostAndPort(com.couchbase.client.core.util.HostAndPort) Mockito.mock(org.mockito.Mockito.mock) QueryResponse(com.couchbase.client.core.msg.query.QueryResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) SeedNode(com.couchbase.client.core.env.SeedNode) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) HttpServerCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpServerCodec) ArrayList(java.util.ArrayList) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) NoopCircuitBreaker(com.couchbase.client.core.endpoint.NoopCircuitBreaker) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ChannelInitializer(com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Mockito.when(org.mockito.Mockito.when) EventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.EventLoopGroup) TimeUnit(java.util.concurrent.TimeUnit) HttpClientCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpClientCodec) AfterEach(org.junit.jupiter.api.AfterEach) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) Bootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) LocalChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalChannel) HttpClientCodec(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpClientCodec) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) HostAndPort(com.couchbase.client.core.util.HostAndPort) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) QueryResponse(com.couchbase.client.core.msg.query.QueryResponse) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) Bootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap) Test(org.junit.jupiter.api.Test)

Example 8 with Channel

use of com.couchbase.client.core.deps.io.netty.channel.Channel in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method disconnectDuringRetry.

/**
 * Make sure that while we are retrying and a disconnect event comes along, we stop
 * retrying and end up in the disconnected state right away.
 */
@Test
void disconnectDuringRetry() {
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
    ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
    try {
        final CompletableFuture<Channel> cf = new CompletableFuture<>();
        InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
        endpoint.connect();
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
        endpoint.disconnect();
        waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 4);
        int warn = 0;
        int debug = 0;
        for (Event event : eventBus.publishedEvents()) {
            if (event.severity() == Event.Severity.WARN) {
                warn++;
                assertTrue(event instanceof EndpointConnectionFailedEvent);
            } else if (event.severity() == Event.Severity.DEBUG) {
                debug++;
                assertTrue(event instanceof EndpointConnectionAbortedEvent);
            } else {
                throw new RuntimeException("Unexpected Event: " + event);
            }
        }
        assertEquals(3, warn);
        assertEquals(1, debug);
    } finally {
        environment.shutdown();
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) CompletableFuture(java.util.concurrent.CompletableFuture) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Event(com.couchbase.client.core.cnc.Event) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Aggregations

Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)8 Test (org.junit.jupiter.api.Test)6 Event (com.couchbase.client.core.cnc.Event)4 EndpointConnectionIgnoredEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent)4 EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)4 ChannelInitializer (com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer)4 ServiceContext (com.couchbase.client.core.service.ServiceContext)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Core (com.couchbase.client.core.Core)3 CoreContext (com.couchbase.client.core.CoreContext)3 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)3 EndpointConnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent)3 EndpointConnectionAbortedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent)3 EndpointConnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent)3 EndpointDisconnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent)3 EndpointStateChangedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent)3 Bootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap)3 ServerBootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap)3 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)3 DefaultEventLoopGroup (com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup)3