Search in sources :

Example 1 with LocalAddress

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

the class BaseEndpointIntegrationTest method mustReconnectWhenChannelCloses.

/**
 * When the underlying channel closes, the endpoint must continue to reconnect until being instructed
 * to stop with an explicit disconnect command.
 */
@Test
void mustReconnectWhenChannelCloses() {
    LocalServerController localServerController = startLocalServer(eventLoopGroup);
    ServiceContext serviceContext = new ServiceContext(new CoreContext(null, 1, env, authenticator()), "127.0.0.1", 1234, ServiceType.KV, Optional.empty());
    BaseEndpoint endpoint = new BaseEndpoint("127.0.0.1", 1234, eventLoopGroup, serviceContext, CircuitBreakerConfig.enabled(false).build(), ServiceType.QUERY, false) {

        @Override
        protected PipelineInitializer pipelineInitializer() {
            return (endpoint, pipeline) -> {
            };
        }

        @Override
        protected SocketAddress remoteAddress() {
            return new LocalAddress("server");
        }
    };
    List<EndpointState> transitions = Collections.synchronizedList(new ArrayList<>());
    endpoint.states().subscribe(transitions::add);
    assertEquals(0, localServerController.connectAttempts.get());
    assertNull(localServerController.channel.get());
    endpoint.connect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
    waitUntilCondition(() -> localServerController.connectAttempts.get() == 1);
    assertNotNull(localServerController.channel.get());
    localServerController.channel.get().close().awaitUninterruptibly();
    List<EndpointState> expectedTransitions = Arrays.asList(// initial state
    EndpointState.DISCONNECTED, // initial connect attempt
    EndpointState.CONNECTING, // properly connected the first time
    EndpointState.CONNECTED, // disconnected when we kill the channel from the server side
    EndpointState.DISCONNECTED, // endpoint should be reconnecting now
    EndpointState.CONNECTING, // finally, we are able to reconnect completely
    EndpointState.CONNECTED);
    waitUntilCondition(() -> transitions.size() == expectedTransitions.size());
    assertEquals(expectedTransitions, transitions);
    waitUntilCondition(() -> localServerController.connectAttempts.get() >= 2);
    endpoint.disconnect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
    boolean hasDisconnectEvent = false;
    for (Event event : eventBus.publishedEvents()) {
        if (event instanceof UnexpectedEndpointDisconnectedEvent) {
            hasDisconnectEvent = true;
            break;
        }
    }
    assertTrue(hasDisconnectEvent);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) SocketAddress(java.net.SocketAddress) SimpleChannelInboundHandler(com.couchbase.client.core.deps.io.netty.channel.SimpleChannelInboundHandler) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) ServiceContext(com.couchbase.client.core.service.ServiceContext) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) ChannelInitializer(com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer) Event(com.couchbase.client.core.cnc.Event) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Collections(java.util.Collections) CoreContext(com.couchbase.client.core.CoreContext) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) ServiceContext(com.couchbase.client.core.service.ServiceContext) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) Event(com.couchbase.client.core.cnc.Event) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with LocalAddress

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

the class BaseEndpointIntegrationTest method startLocalServer.

private LocalServerController startLocalServer(final DefaultEventLoopGroup eventLoopGroup) {
    final LocalServerController localServerController = new LocalServerController();
    ServerBootstrap bootstrap = new ServerBootstrap().group(eventLoopGroup).localAddress(new LocalAddress("server")).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
                }

                @Override
                public void channelActive(ChannelHandlerContext ctx) {
                    localServerController.channel.set(ctx.channel());
                    localServerController.connectAttempts.incrementAndGet();
                    ctx.fireChannelActive();
                }
            });
        }
    }).channel(LocalServerChannel.class);
    bootstrap.bind().awaitUninterruptibly();
    return localServerController;
}
Also used : LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelInitializer(com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap)

Example 3 with LocalAddress

use of com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress 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)

Aggregations

ServerBootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap)3 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)3 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)3 ChannelInitializer (com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer)3 LocalAddress (com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress)3 LocalServerChannel (com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel)3 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)2 DefaultEventLoopGroup (com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Event (com.couchbase.client.core.cnc.Event)1 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)1 UnexpectedEndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)1 Bootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap)1 Unpooled (com.couchbase.client.core.deps.io.netty.buffer.Unpooled)1