use of com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent 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();
}
}
use of com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method emitsEventOnFailedDisconnect.
/**
* If the disconnect failed for some reason, make sure the proper warning event
* is raised and captured.
*/
@Test
void emitsEventOnFailedDisconnect() {
final Throwable expectedCause = new Exception("something failed");
EmbeddedChannel channel = new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
promise.tryFailure(expectedCause);
}
});
InstrumentedEndpoint endpoint = connectSuccessfully(channel);
endpoint.disconnect();
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
assertEquals(2, eventBus.publishedEvents().size());
assertTrue(eventBus.publishedEvents().get(0) instanceof EndpointConnectedEvent);
EndpointDisconnectionFailedEvent event = (EndpointDisconnectionFailedEvent) eventBus.publishedEvents().get(1);
assertEquals(expectedCause, event.cause());
assertEquals(Event.Severity.WARN, event.severity());
}
Aggregations