Search in sources :

Example 6 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class DefaultConfigurationProviderTest method externalModeSelectedIfAuto.

@Test
void externalModeSelectedIfAuto() {
    Core core = mock(Core.class);
    CoreEnvironment environment = CoreEnvironment.create();
    CoreContext ctx = new CoreContext(core, 1, environment, PasswordAuthenticator.create("user", "pw"));
    when(core.context()).thenReturn(ctx);
    Set<SeedNode> seedNodes = new HashSet<>(Collections.singletonList(SeedNode.create("192.168.132.234")));
    DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, seedNodes);
    assertTrue(provider.config().bucketConfigs().isEmpty());
    assertEquals(1, provider.currentSeedNodes().size());
    String bucket = "default";
    String config = readResource("config_with_external.json", DefaultConfigurationProviderTest.class);
    provider.proposeBucketConfig(new ProposedBucketConfigContext(bucket, config, ORIGIN));
    assertEquals("external", ctx.alternateAddress().get());
    environment.shutdown();
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) SeedNode(com.couchbase.client.core.env.SeedNode) Core(com.couchbase.client.core.Core) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class BaseBucketLoaderTest method setup.

@BeforeEach
void setup() {
    CoreEnvironment env = mock(CoreEnvironment.class);
    core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment in project couchbase-jvm-clients by couchbase.

the class CoreContextTest method getAndExportProperties.

@Test
void getAndExportProperties() {
    long id = 12345;
    CoreEnvironment env = mock(CoreEnvironment.class);
    Core core = mock(Core.class);
    Authenticator authenticator = mock(Authenticator.class);
    CoreContext ctx = new CoreContext(core, id, env, authenticator);
    assertEquals(core, ctx.core());
    assertEquals(id, ctx.id());
    assertEquals(env, ctx.environment());
    assertEquals(authenticator, ctx.authenticator());
    String result = ctx.exportAsString(Context.ExportFormat.JSON);
    assertEquals("{\"coreId\":\"0x3039\"}", result);
}
Also used : CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Authenticator(com.couchbase.client.core.env.Authenticator) Test(org.junit.jupiter.api.Test)

Example 9 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment 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 10 with CoreEnvironment

use of com.couchbase.client.core.env.CoreEnvironment 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)

Aggregations

CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)30 Core (com.couchbase.client.core.Core)20 CoreContext (com.couchbase.client.core.CoreContext)20 Test (org.junit.jupiter.api.Test)15 Authenticator (com.couchbase.client.core.env.Authenticator)13 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)9 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)8 HostAndPort (com.couchbase.client.core.util.HostAndPort)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)6 Stability (com.couchbase.client.core.annotation.Stability)5 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)5 Duration (java.time.Duration)5 List (java.util.List)5 Map (java.util.Map)5 Flux (reactor.core.publisher.Flux)5 Mono (reactor.core.publisher.Mono)5 Reactor (com.couchbase.client.core.Reactor)4 BucketConfig (com.couchbase.client.core.config.BucketConfig)4 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)4