Search in sources :

Example 41 with CoreContext

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

the class DefaultConfigurationProviderTest method ignoresBucketConfigWithOlderRevOrEpoch.

@ParameterizedTest
@CsvSource({ "config_higher_rev_no_epoch,2,0,config_lower_rev_no_epoch", "config_lower_rev_lower_epoch,1,1,config_higher_rev_no_epoch", "config_higher_rev_lower_epoch,2,1,config_lower_rev_lower_epoch", "config_lower_rev_higher_epoch,1,2,config_higher_rev_lower_epoch" })
void ignoresBucketConfigWithOlderRevOrEpoch(String oldConfigFile, long oldRev, long oldEpoch, String newConfigFile) {
    Core core = mock(Core.class);
    when(core.context()).thenReturn(new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class)));
    DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, SeedNode.LOCALHOST);
    String bucket = "travel-sample";
    String config = readResource(oldConfigFile + ".json", DefaultConfigurationProviderTest.class);
    provider.proposeBucketConfig(new ProposedBucketConfigContext(bucket, config, ORIGIN));
    assertEquals(oldRev, provider.config().bucketConfig(bucket).rev());
    assertEquals(oldEpoch, provider.config().bucketConfig(bucket).revEpoch());
    String newConfig = readResource(newConfigFile + ".json", DefaultConfigurationProviderTest.class);
    provider.proposeBucketConfig(new ProposedBucketConfigContext(bucket, newConfig, ORIGIN));
    assertEquals(oldRev, provider.config().bucketConfig(bucket).rev());
    assertEquals(oldEpoch, provider.config().bucketConfig(bucket).revEpoch());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) Core(com.couchbase.client.core.Core) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with CoreContext

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

the class DefaultConfigurationProviderTest method forceDefaultModeIfDefault.

@Test
void forceDefaultModeIfDefault() {
    Core core = mock(Core.class);
    CoreEnvironment environment = CoreEnvironment.builder().ioConfig(IoConfig.networkResolution(NetworkResolution.DEFAULT)).build();
    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(Optional.empty(), ctx.alternateAddress());
    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 43 with CoreContext

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

the class DefaultConfigurationProviderTest method handlesMultipleBucketOpenInProgress.

/**
 * Regression test for JVMCBC-880.
 * <p>
 * Verifies that when multiple bucket open attempts happen in parallel, the bucketConfigLoadInProgress method
 * is not returning false prematurely (namely when only one is finished but one is still oustanding).
 */
@Test
void handlesMultipleBucketOpenInProgress() throws Exception {
    Core core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
    Set<SeedNode> seedNodes = new HashSet<>(Collections.singletonList(SeedNode.create("127.0.0.1")));
    Sinks.One<ProposedBucketConfigContext> bucket1Barrier = Sinks.one();
    Sinks.One<ProposedBucketConfigContext> bucket2Barrier = Sinks.one();
    ConfigurationProvider cp = new DefaultConfigurationProvider(core, seedNodes) {

        @Override
        protected Mono<ProposedBucketConfigContext> loadBucketConfigForSeed(NodeIdentifier identifier, int mappedKvPort, int mappedManagerPort, String name, Optional<String> alternateAddress) {
            if (name.equals("bucket1")) {
                return bucket1Barrier.asMono();
            } else {
                return bucket2Barrier.asMono();
            }
        }

        @Override
        public void proposeBucketConfig(ProposedBucketConfigContext ctx) {
        }

        @Override
        protected Mono<Void> registerRefresher(String bucket) {
            return Mono.empty();
        }
    };
    assertFalse(cp.bucketConfigLoadInProgress());
    CountDownLatch latch = new CountDownLatch(2);
    cp.openBucket("bucket1").subscribe(unused -> {
    }, Assertions::fail, () -> {
        assertTrue(cp.bucketConfigLoadInProgress());
        latch.countDown();
    });
    cp.openBucket("bucket2").subscribe(unused -> {
    }, Assertions::fail, () -> {
        assertFalse(cp.bucketConfigLoadInProgress());
        latch.countDown();
    });
    // we pretend bucket 1 takes 1ms, while bucket2 takes 200ms
    Mono.delay(Duration.ofMillis(1)).subscribe(i -> bucket1Barrier.tryEmitValue(new ProposedBucketConfigContext("bucket1", "{}", "127.0.0.1")));
    Mono.delay(Duration.ofMillis(200)).subscribe(i -> bucket2Barrier.tryEmitValue(new ProposedBucketConfigContext("bucket2", "{}", "127.0.0.1")));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) Sinks(reactor.core.publisher.Sinks) Optional(java.util.Optional) SeedNode(com.couchbase.client.core.env.SeedNode) CountDownLatch(java.util.concurrent.CountDownLatch) Assertions(org.junit.jupiter.api.Assertions) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with CoreContext

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

the class DefaultConfigurationProviderTest method ignoresMultipleCollectionIdRefreshAttempts.

/**
 * It is allowed to have multiple attempts in-flight at the same time, but not for the same collection identifier
 * (since this would just spam the cluster unnecessarily).
 */
@Test
void ignoresMultipleCollectionIdRefreshAttempts() {
    Core core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
    Set<SeedNode> seedNodes = new HashSet<>(Collections.singletonList(SeedNode.create("127.0.0.1")));
    List<GetCollectionIdRequest> capturedRequests = new ArrayList<>();
    doAnswer(invocation -> {
        capturedRequests.add(invocation.getArgument(0));
        return null;
    }).when(core).send(any(GetCollectionIdRequest.class));
    DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, seedNodes);
    assertFalse(provider.collectionRefreshInProgress());
    CollectionIdentifier identifier1 = new CollectionIdentifier("bucket", Optional.of("scope"), Optional.of("collection"));
    CollectionIdentifier identifier2 = new CollectionIdentifier("bucket", Optional.of("_default"), Optional.of("_default"));
    provider.refreshCollectionId(identifier1);
    assertEquals(1, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier1));
    provider.refreshCollectionId(identifier2);
    assertEquals(2, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier2));
    provider.refreshCollectionId(identifier2);
    assertEquals(2, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier2));
    boolean found = false;
    for (Event event : EVENT_BUS.publishedEvents()) {
        if (event instanceof CollectionMapRefreshIgnoredEvent) {
            assertEquals(((CollectionMapRefreshIgnoredEvent) event).collectionIdentifier(), identifier2);
            found = true;
        }
    }
    assertTrue(found);
    capturedRequests.get(0).succeed(new GetCollectionIdResponse(ResponseStatus.SUCCESS, Optional.of(1234L)));
    assertTrue(provider.collectionRefreshInProgress());
    capturedRequests.get(1).cancel(CancellationReason.TIMEOUT);
    waitUntilCondition(() -> !provider.collectionRefreshInProgress());
    found = false;
    for (Event event : EVENT_BUS.publishedEvents()) {
        if (event instanceof CollectionMapRefreshFailedEvent) {
            assertEquals(((CollectionMapRefreshFailedEvent) event).collectionIdentifier(), identifier2);
            found = true;
        }
    }
    assertTrue(found);
}
Also used : GetCollectionIdResponse(com.couchbase.client.core.msg.kv.GetCollectionIdResponse) CoreContext(com.couchbase.client.core.CoreContext) SeedNode(com.couchbase.client.core.env.SeedNode) ArrayList(java.util.ArrayList) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) Event(com.couchbase.client.core.cnc.Event) GetCollectionIdRequest(com.couchbase.client.core.msg.kv.GetCollectionIdRequest) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with CoreContext

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

the class ErrorMapLoadingHandlerTest method beforeEach.

@BeforeEach
@Override
protected void beforeEach() {
    super.beforeEach();
    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(1000));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : HostAndPort(com.couchbase.client.core.util.HostAndPort) 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) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CoreContext (com.couchbase.client.core.CoreContext)52 Core (com.couchbase.client.core.Core)46 Test (org.junit.jupiter.api.Test)25 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)22 Authenticator (com.couchbase.client.core.env.Authenticator)17 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)14 BeforeEach (org.junit.jupiter.api.BeforeEach)13 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 ArrayList (java.util.ArrayList)10 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)9 HostAndPort (com.couchbase.client.core.util.HostAndPort)9 List (java.util.List)8 TimeoutConfig (com.couchbase.client.core.env.TimeoutConfig)7 ServiceContext (com.couchbase.client.core.service.ServiceContext)7 Duration (java.time.Duration)7 Optional (java.util.Optional)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 SeedNode (com.couchbase.client.core.env.SeedNode)6 Event (com.couchbase.client.core.cnc.Event)5