Search in sources :

Example 1 with EndpointDisconnectionFailedEvent

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();
    }
}
Also used : UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) Duration(java.time.Duration)

Example 2 with EndpointDisconnectionFailedEvent

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());
}
Also used : EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(com.couchbase.client.core.deps.io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) ChannelPromise(com.couchbase.client.core.deps.io.netty.channel.ChannelPromise) ChannelException(com.couchbase.client.core.deps.io.netty.channel.ChannelException) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) Test(org.junit.jupiter.api.Test)

Aggregations

EndpointDisconnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent)2 EndpointConnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent)1 EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)1 UnexpectedEndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)1 ChannelException (com.couchbase.client.core.deps.io.netty.channel.ChannelException)1 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)1 ChannelOutboundHandlerAdapter (com.couchbase.client.core.deps.io.netty.channel.ChannelOutboundHandlerAdapter)1 ChannelPromise (com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)1 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)1 Duration (java.time.Duration)1 Test (org.junit.jupiter.api.Test)1