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