Search in sources :

Example 11 with SimpleEventBus

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

the class PooledServiceTest method beforeEach.

@BeforeEach
void beforeEach() {
    eventBus = new SimpleEventBus(true, Collections.singletonList(ServiceStateChangedEvent.class));
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, environment, authenticator);
    serviceContext = new ServiceContext(coreContext, "127.0.0.1", 1234, ServiceType.KV, Optional.empty());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with SimpleEventBus

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

the class RetryOrchestratorTest method retryWithDelay.

@Test
@SuppressWarnings({ "unchecked" })
void retryWithDelay() {
    Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
    Request<?> request = mock(Request.class);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.completed()).thenReturn(false);
    when(request.context()).thenReturn(requestContext);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    when(request.absoluteTimeout()).thenReturn(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(2500));
    Core core = mock(Core.class);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.timer()).thenReturn(timer);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    long start = System.nanoTime();
    RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
    verify(requestContext, times(1)).incrementRetryAttempts(Duration.ofMillis(200), RetryReason.UNKNOWN);
    verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
    waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
    long end = System.nanoTime();
    verify(core, times(1)).send(request, false);
    verify(core, never()).send(request);
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 200);
    timer.stop();
    assertEquals(1, eventBus.publishedEvents().size());
    RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
    assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Request(com.couchbase.client.core.msg.Request) Timer(com.couchbase.client.core.Timer) 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 13 with SimpleEventBus

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

the class RetryOrchestratorTest method capsRetryDelay.

@Test
@SuppressWarnings({ "unchecked" })
void capsRetryDelay() {
    Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
    Request<?> request = mock(Request.class);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.completed()).thenReturn(false);
    when(request.context()).thenReturn(requestContext);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    when(request.absoluteTimeout()).thenAnswer(invocationOnMock -> System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(10));
    Core core = mock(Core.class);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.timer()).thenReturn(timer);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    long start = System.nanoTime();
    RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
    verify(requestContext, never()).incrementRetryAttempts((Duration.ofMillis(200)), RetryReason.UNKNOWN);
    verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
    waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
    long end = System.nanoTime();
    verify(core, times(1)).send(request, false);
    verify(core, never()).send(request);
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) < 200);
    timer.stop();
    assertEquals(1, eventBus.publishedEvents().size());
    RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
    assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Request(com.couchbase.client.core.msg.Request) Timer(com.couchbase.client.core.Timer) 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 14 with SimpleEventBus

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

the class DefaultConfigurationProviderIntegrationTest method openBucketFromOneFirstValidSeed.

/**
 * Bucket config should also be loaded when the second seed in the list is not available.
 */
@Test
void openBucketFromOneFirstValidSeed() {
    TestNodeConfig cfg = config().firstNodeWith(Services.KV).get();
    Set<SeedNode> seeds = new HashSet<>(Arrays.asList(SeedNode.create(cfg.hostname(), Optional.of(cfg.ports().get(Services.KV)), Optional.of(cfg.ports().get(Services.MANAGER))), SeedNode.create("1.2.3.4")));
    SimpleEventBus eventBus = new SimpleEventBus(true);
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    core = Core.create(environment, authenticator(), seeds);
    String bucketName = config().bucketname();
    ConfigurationProvider provider = new DefaultConfigurationProvider(core, seeds);
    openAndClose(bucketName, provider);
    provider.shutdown().block();
    waitUntilCondition(() -> eventBus.publishedEvents().stream().anyMatch(e -> e instanceof EndpointConnectionFailedEvent));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) SeedNode(com.couchbase.client.core.env.SeedNode) ClusterType(com.couchbase.client.test.ClusterType) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ConfigException(com.couchbase.client.core.error.ConfigException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Set(java.util.Set) Event(com.couchbase.client.core.cnc.Event) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) BucketNotFoundDuringLoadException(com.couchbase.client.core.error.BucketNotFoundDuringLoadException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) Services(com.couchbase.client.test.Services) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) Optional(java.util.Optional) Core(com.couchbase.client.core.Core) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) Collections(java.util.Collections) SeedNode(com.couchbase.client.core.env.SeedNode) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) HashSet(java.util.HashSet) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 15 with SimpleEventBus

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

the class DefaultConfigurationProviderIntegrationTest method retriesOnBucketNotFoundDuringLoadException.

/**
 * Need to make sure that if a bucket is not found during load, we continue retrying the open
 * bucket attempts.
 */
@Test
@IgnoreWhen(clusterTypes = ClusterType.CAVES)
void retriesOnBucketNotFoundDuringLoadException() {
    TestNodeConfig cfg = config().firstNodeWith(Services.KV).get();
    Set<SeedNode> seeds = new HashSet<>(Collections.singletonList(SeedNode.create(cfg.hostname(), Optional.of(cfg.ports().get(Services.KV)), Optional.of(cfg.ports().get(Services.MANAGER)))));
    SimpleEventBus eventBus = new SimpleEventBus(true);
    environment = CoreEnvironment.builder().eventBus(eventBus).build();
    core = Core.create(environment, authenticator(), seeds);
    ConfigurationProvider provider = new DefaultConfigurationProvider(core, seeds);
    try {
        String bucketName = "this-bucket-does-not-exist";
        provider.openBucket(bucketName).subscribe(v -> {
        }, e -> assertTrue(e instanceof ConfigException));
        waitUntilCondition(() -> eventBus.publishedEvents().stream().anyMatch(p -> p instanceof BucketOpenRetriedEvent));
        for (Event event : eventBus.publishedEvents()) {
            if (event instanceof BucketOpenRetriedEvent) {
                assertEquals(bucketName, ((BucketOpenRetriedEvent) event).bucketName());
                assertTrue(event.cause() instanceof BucketNotFoundDuringLoadException);
            }
        }
    } finally {
        provider.shutdown().block();
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Arrays(java.util.Arrays) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) SeedNode(com.couchbase.client.core.env.SeedNode) ClusterType(com.couchbase.client.test.ClusterType) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ConfigException(com.couchbase.client.core.error.ConfigException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Set(java.util.Set) Event(com.couchbase.client.core.cnc.Event) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) BucketNotFoundDuringLoadException(com.couchbase.client.core.error.BucketNotFoundDuringLoadException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) Services(com.couchbase.client.test.Services) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AlreadyShutdownException(com.couchbase.client.core.error.AlreadyShutdownException) Optional(java.util.Optional) Core(com.couchbase.client.core.Core) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) Collections(java.util.Collections) SeedNode(com.couchbase.client.core.env.SeedNode) ConfigException(com.couchbase.client.core.error.ConfigException) BucketNotFoundDuringLoadException(com.couchbase.client.core.error.BucketNotFoundDuringLoadException) TestNodeConfig(com.couchbase.client.test.TestNodeConfig) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EndpointConnectionFailedEvent(com.couchbase.client.core.cnc.events.endpoint.EndpointConnectionFailedEvent) Event(com.couchbase.client.core.cnc.Event) BucketOpenRetriedEvent(com.couchbase.client.core.cnc.events.config.BucketOpenRetriedEvent) HashSet(java.util.HashSet) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) 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