Search in sources :

Example 1 with ServiceContext

use of com.couchbase.client.core.service.ServiceContext 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 ServiceContext

use of com.couchbase.client.core.service.ServiceContext in project couchbase-jvm-clients by couchbase.

the class KeyValueEndpointIntegrationTest method beforeAll.

@BeforeAll
static void beforeAll() {
    TestNodeConfig node = config().nodes().get(0);
    env = environment().build();
    core = Core.create(env, authenticator(), seedNodes());
    serviceContext = new ServiceContext(new CoreContext(core, 1, env, authenticator()), node.hostname(), node.ports().get(Services.KV), ServiceType.KV, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 3 with ServiceContext

use of com.couchbase.client.core.service.ServiceContext in project couchbase-jvm-clients by couchbase.

the class ViewEndpointIntegrationTest method beforeAll.

@BeforeAll
static void beforeAll() {
    TestNodeConfig node = config().nodes().get(0);
    env = environment().ioConfig(IoConfig.captureTraffic(ServiceType.VIEWS)).build();
    serviceContext = new ServiceContext(new CoreContext(null, 1, env, authenticator()), node.hostname(), node.ports().get(Services.VIEW), ServiceType.VIEWS, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with ServiceContext

use of com.couchbase.client.core.service.ServiceContext in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method retryOnTimeoutUntilEventuallyConnected.

/**
 * This test fakes a situation where the channel future from netty would simply not return
 * at all and time out, and the client would resubscribe. Then at some future attempt the
 * future returns fine and we should end up in a connected state and ready to go.
 */
@Test
void retryOnTimeoutUntilEventuallyConnected() {
    SimpleEventBus eventBus = new SimpleEventBus(true);
    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);
        EmbeddedChannel channel = new EmbeddedChannel();
        cf.complete(channel);
        waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
        assertTrue(eventBus.publishedEvents().size() >= 3);
        boolean failedFound = false;
        boolean successFound = false;
        for (Event event : eventBus.publishedEvents()) {
            if (event instanceof EndpointConnectionFailedEvent) {
                assertEquals(Event.Severity.WARN, event.severity());
                assertEquals(Duration.ofMillis(10), event.duration());
                failedFound = true;
            } else if (event instanceof EndpointConnectedEvent) {
                assertEquals(Event.Severity.DEBUG, event.severity());
                assertTrue(event.duration().toNanos() > 0);
                successFound = true;
            }
        }
        assertTrue(failedFound);
        assertTrue(successFound);
    } finally {
        env.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) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) CompletableFuture(java.util.concurrent.CompletableFuture) 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) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) Test(org.junit.jupiter.api.Test)

Example 5 with ServiceContext

use of com.couchbase.client.core.service.ServiceContext in project couchbase-jvm-clients by couchbase.

the class ManagerEndpointIntegrationTest method beforeEach.

@BeforeEach
void beforeEach() {
    TestNodeConfig node = config().nodes().get(0);
    env = CoreEnvironment.create();
    serviceContext = new ServiceContext(new CoreContext(null, 1, env, PasswordAuthenticator.create(config().adminUsername(), config().adminPassword())), node.hostname(), node.ports().get(Services.MANAGER), ServiceType.MANAGER, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CoreContext (com.couchbase.client.core.CoreContext)7 ServiceContext (com.couchbase.client.core.service.ServiceContext)7 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)4 Core (com.couchbase.client.core.Core)3 Event (com.couchbase.client.core.cnc.Event)3 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)3 TestNodeConfig (com.couchbase.client.test.TestNodeConfig)3 Test (org.junit.jupiter.api.Test)3 EndpointConnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent)2 EndpointConnectionAbortedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent)2 EndpointConnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent)2 EndpointConnectionIgnoredEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent)2 EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)2 EndpointDisconnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent)2 EndpointStateChangedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent)2 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 UnexpectedEndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)1