Search in sources :

Example 1 with EndpointDisconnectedEvent

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

Example 2 with 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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) 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) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Test(org.junit.jupiter.api.Test)

Example 3 with EndpointDisconnectedEvent

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

Aggregations

EndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent)3 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 Test (org.junit.jupiter.api.Test)2 EndpointConnectedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent)1 EndpointConnectionIgnoredEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent)1 EndpointDisconnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent)1 UnexpectedEndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)1 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)1 Duration (java.time.Duration)1 CompletableFuture (java.util.concurrent.CompletableFuture)1