use of com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method disconnectAfterBeingConnected.
/**
* This test tests the happy path, after being connected properly eventually a disconnect
* signal comes along.
*/
@Test
void disconnectAfterBeingConnected() {
EmbeddedChannel channel = new EmbeddedChannel();
InstrumentedEndpoint endpoint = connectSuccessfully(channel);
assertTrue(channel.isOpen());
assertTrue(channel.isActive());
endpoint.disconnect();
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
assertFalse(channel.isOpen());
assertFalse(channel.isActive());
assertEquals(2, eventBus.publishedEvents().size());
assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectedEvent);
assertTrue(eventBus.publishedEvents().get(1) instanceof EndpointDisconnectedEvent);
}
use of com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method disconnectOverridesConnectCompletion.
/**
* This test makes sure that even if we connect successfully, if there has been a
* disconnect signal in the meantime we need to properly close it all and not end
* in a connect-disconnect limbo.
*/
@Test
void disconnectOverridesConnectCompletion() {
final CompletableFuture<Channel> cf = new CompletableFuture<>();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
endpoint.connect();
waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTING);
endpoint.disconnect();
EmbeddedChannel channel = new EmbeddedChannel();
cf.complete(channel);
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
assertTrue(eventBus.publishedEvents().size() >= 2);
assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectionIgnoredEvent);
assertTrue(eventBus.publishedEvents().get(1) instanceof EndpointDisconnectedEvent);
assertEquals("Endpoint disconnected successfully", eventBus.publishedEvents().get(1).description());
}
use of com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent in project couchbase-jvm-clients by couchbase.
the class BaseEndpoint method closeChannel.
/**
* Helper method to close a channel and emit events if needed.
*
* <p>If no channel has been active already, it directly goes into a disconnected
* state. If one has been there before, it waits until the disconnect future
* completes.</p>
*
* @param channel the channel to close.
*/
private void closeChannel(final Channel channel) {
if (channel != null && !channel.eventLoop().isShutdown()) {
final EndpointContext endpointContext = this.endpointContext.get();
final long start = System.nanoTime();
channel.disconnect().addListener(future -> {
Duration latency = Duration.ofNanos(System.nanoTime() - start);
state.transition(EndpointState.DISCONNECTED);
state.close();
if (future.isSuccess()) {
endpointContext.environment().eventBus().publish(new EndpointDisconnectedEvent(latency, endpointContext));
} else {
endpointContext.environment().eventBus().publish(new EndpointDisconnectionFailedEvent(latency, endpointContext, future.cause()));
}
});
} else {
state.transition(EndpointState.DISCONNECTED);
state.close();
}
}
Aggregations