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);
}
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());
}
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());
}
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();
}
}
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());
}
Aggregations