Search in sources :

Example 21 with SimpleEventBus

use of com.couchbase.client.core.cnc.SimpleEventBus in project couchbase-jvm-clients by couchbase.

the class NodeTest method sendsEventsIntoEventBus.

@Test
void sendsEventsIntoEventBus() {
    Core core = mock(Core.class);
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(NodeStateChangedEvent.class));
    CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    try {
        Node node = new Node(ctx, mock(NodeIdentifier.class), NO_ALTERNATE) {

            @Override
            protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
                Service s = mock(Service.class);
                when(s.type()).thenReturn(serviceType);
                when(s.states()).thenReturn(DirectProcessor.create());
                when(s.state()).thenReturn(ServiceState.IDLE);
                return s;
            }
        };
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.addService(ServiceType.KV, 1, Optional.of("bucket")).block();
        node.removeService(ServiceType.KV, Optional.of("bucket")).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        node.disconnect().block();
        node.disconnect().block();
        node.addService(ServiceType.QUERY, 2, Optional.empty()).block();
        node.removeService(ServiceType.QUERY, Optional.empty()).block();
        List<Event> events = eventBus.publishedEvents();
        assertTrue(events.remove(0) instanceof NodeConnectedEvent);
        assertTrue(events.get(0) instanceof ServiceAddedEvent);
        assertTrue(events.get(1) instanceof ServiceAddedEvent);
        assertTrue(events.get(2) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(3) instanceof ServiceRemovedEvent);
        assertTrue(events.get(4) instanceof ServiceRemovedEvent);
        assertTrue(events.get(5) instanceof ServiceRemoveIgnoredEvent);
        assertTrue(events.get(6) instanceof NodeDisconnectedEvent);
        assertTrue(events.get(7) instanceof NodeDisconnectIgnoredEvent);
        assertTrue(events.get(8) instanceof ServiceAddIgnoredEvent);
        assertTrue(events.get(9) instanceof ServiceRemoveIgnoredEvent);
    } finally {
        env.shutdown();
    }
}
Also used : ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) CoreContext(com.couchbase.client.core.CoreContext) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) Optional(java.util.Optional) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) Service(com.couchbase.client.core.service.Service) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) ServiceType(com.couchbase.client.core.service.ServiceType) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) ServiceAddIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceAddIgnoredEvent) ServiceRemovedEvent(com.couchbase.client.core.cnc.events.service.ServiceRemovedEvent) NodeStateChangedEvent(com.couchbase.client.core.cnc.events.node.NodeStateChangedEvent) Event(com.couchbase.client.core.cnc.Event) ServiceRemoveIgnoredEvent(com.couchbase.client.core.cnc.events.service.ServiceRemoveIgnoredEvent) NodeConnectedEvent(com.couchbase.client.core.cnc.events.node.NodeConnectedEvent) ServiceAddedEvent(com.couchbase.client.core.cnc.events.service.ServiceAddedEvent) NodeDisconnectedEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectedEvent) NodeDisconnectIgnoredEvent(com.couchbase.client.core.cnc.events.node.NodeDisconnectIgnoredEvent) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 22 with SimpleEventBus

use of com.couchbase.client.core.cnc.SimpleEventBus in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method beforeEach.

@BeforeEach
void beforeEach() {
    eventLoopGroup = new NioEventLoopGroup(1);
    eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, environment, authenticator);
    ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) NioEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.nio.NioEventLoopGroup) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with SimpleEventBus

use of com.couchbase.client.core.cnc.SimpleEventBus in project couchbase-jvm-clients by couchbase.

the class BaseEndpointTest method disconnectDuringRetry.

/**
 * Make sure that while we are retrying and a disconnect event comes along, we stop
 * retrying and end up in the disconnected state right away.
 */
@Test
void disconnectDuringRetry() {
    SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
    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);
        endpoint.disconnect();
        waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
        waitUntilCondition(() -> eventBus.publishedEvents().size() >= 4);
        int warn = 0;
        int debug = 0;
        for (Event event : eventBus.publishedEvents()) {
            if (event.severity() == Event.Severity.WARN) {
                warn++;
                assertTrue(event instanceof EndpointConnectionFailedEvent);
            } else if (event.severity() == Event.Severity.DEBUG) {
                debug++;
                assertTrue(event instanceof EndpointConnectionAbortedEvent);
            } else {
                throw new RuntimeException("Unexpected Event: " + event);
            }
        }
        assertEquals(3, warn);
        assertEquals(1, debug);
    } finally {
        environment.shutdown();
    }
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) ServiceContext(com.couchbase.client.core.service.ServiceContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) CompletableFuture(java.util.concurrent.CompletableFuture) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointDisconnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectionFailedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) EndpointConnectionAbortedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionAbortedEvent) EndpointConnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectedEvent) EndpointConnectionIgnoredEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionIgnoredEvent) Event(com.couchbase.client.core.cnc.Event) EndpointStateChangedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointStateChangedEvent) EndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointDisconnectedEvent) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 24 with SimpleEventBus

use of com.couchbase.client.core.cnc.SimpleEventBus in project couchbase-jvm-clients by couchbase.

the class RetryOrchestratorTest method cancelIfNoMoreRetriesAllowed.

@Test
@SuppressWarnings({ "unchecked" })
void cancelIfNoMoreRetriesAllowed() {
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.noRetry()));
    Request<?> request = mock(Request.class);
    when(request.completed()).thenReturn(false);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.context()).thenReturn(requestContext);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext context = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    RetryOrchestrator.maybeRetry(context, request, RetryReason.UNKNOWN);
    verify(request, times(1)).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN), Function.identity());
    assertEquals(1, eventBus.publishedEvents().size());
    RequestNotRetriedEvent retryEvent = (RequestNotRetriedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.INFO, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) RequestNotRetriedEvent(com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent) Request(com.couchbase.client.core.msg.Request) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) RequestContext(com.couchbase.client.core.msg.RequestContext) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 25 with SimpleEventBus

use of com.couchbase.client.core.cnc.SimpleEventBus in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method failConnectIfPromiseTimesOut.

/**
 * This test makes sure that the timer fires if the connect future is not completed
 * otherwise.
 */
@Test
void failConnectIfPromiseTimesOut() throws Exception {
    channel = new EmbeddedChannel();
    eventBus = new SimpleEventBus(true);
    CoreEnvironment env = mock(CoreEnvironment.class);
    TimeoutConfig timeoutConfig = mock(TimeoutConfig.class);
    when(env.eventBus()).thenReturn(eventBus);
    when(env.timeoutConfig()).thenReturn(timeoutConfig);
    when(timeoutConfig.connectTimeout()).thenReturn(Duration.ofMillis(100));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    EndpointContext endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    Thread.sleep(Duration.ofMillis(100).toMillis() + 5);
    channel.runScheduledPendingTasks();
    assertTrue(connect.isDone());
    assertTrue(connect.cause() instanceof TimeoutException);
    assertEquals("KV Error Map loading timed out after 100ms", connect.cause().getMessage());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HostAndPort(com.couchbase.client.core.util.HostAndPort) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)25 Core (com.couchbase.client.core.Core)16 Test (org.junit.jupiter.api.Test)14 CoreContext (com.couchbase.client.core.CoreContext)13 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)12 Event (com.couchbase.client.core.cnc.Event)9 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)7 Authenticator (com.couchbase.client.core.env.Authenticator)7 EndpointConnectionFailedEvent (com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent)6 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)6 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)5 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)4 HostAndPort (com.couchbase.client.core.util.HostAndPort)4 ClusterType (com.couchbase.client.test.ClusterType)4 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)4 Util.waitUntilCondition (com.couchbase.client.test.Util.waitUntilCondition)4 Duration (java.time.Duration)4 List (java.util.List)4 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4