Search in sources :

Example 31 with CoreContext

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

the class AsyncCollection method lookupInRequest.

/**
 * Helper method to create the underlying lookup subdoc request.
 *
 * @param id the outer document ID.
 * @param specs the spec which specifies the type of lookups to perform.
 * @param opts custom options to modify the lookup options.
 * @return the subdoc lookup request.
 */
SubdocGetRequest lookupInRequest(final String id, final List<LookupInSpec> specs, final LookupInOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNullOrEmpty(specs, "LookupInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    ArrayList<SubdocGetRequest.Command> commands = new ArrayList<>(specs.size());
    for (int i = 0; i < specs.size(); i++) {
        LookupInSpec spec = specs.get(i);
        commands.add(spec.export(i));
    }
    // xattrs come first
    commands.sort(Comparator.comparing(v -> !v.xattr()));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    byte flags = 0;
    if (opts.accessDeleted()) {
        flags |= SubdocMutateRequest.SUBDOC_DOC_FLAG_ACCESS_DELETED;
    }
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_LOOKUP_IN, opts.parentSpan().orElse(null));
    SubdocGetRequest request = new SubdocGetRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, flags, commands, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : MutateInAccessor(com.couchbase.client.java.kv.MutateInAccessor) JsonSerializer(com.couchbase.client.java.codec.JsonSerializer) InsertOptions(com.couchbase.client.java.kv.InsertOptions) MutationResult(com.couchbase.client.java.kv.MutationResult) DEFAULT_REPLACE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REPLACE_OPTIONS) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) PersistTo(com.couchbase.client.java.kv.PersistTo) ReplaceAccessor(com.couchbase.client.java.kv.ReplaceAccessor) TouchAccessor(com.couchbase.client.java.kv.TouchAccessor) DEFAULT_GET_AND_LOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_LOCK_OPTIONS) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) GetAndTouchOptions(com.couchbase.client.java.kv.GetAndTouchOptions) LookupInMacro(com.couchbase.client.java.kv.LookupInMacro) BucketConfig(com.couchbase.client.core.config.BucketConfig) GetAllReplicasOptions(com.couchbase.client.java.kv.GetAllReplicasOptions) TouchRequest(com.couchbase.client.core.msg.kv.TouchRequest) RemoveOptions(com.couchbase.client.java.kv.RemoveOptions) ExistsOptions(com.couchbase.client.java.kv.ExistsOptions) InsertAccessor(com.couchbase.client.java.kv.InsertAccessor) DEFAULT_INSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_INSERT_OPTIONS) GetAndTouchRequest(com.couchbase.client.core.msg.kv.GetAndTouchRequest) GetReplicaResult(com.couchbase.client.java.kv.GetReplicaResult) ExistsResult(com.couchbase.client.java.kv.ExistsResult) UnlockAccessor(com.couchbase.client.java.kv.UnlockAccessor) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) ReplicaHelper(com.couchbase.client.core.service.kv.ReplicaHelper) Expiry(com.couchbase.client.java.kv.Expiry) List(java.util.List) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetAndLockRequest(com.couchbase.client.core.msg.kv.GetAndLockRequest) LookupInOptions(com.couchbase.client.java.kv.LookupInOptions) SubdocMutateRequest(com.couchbase.client.core.msg.kv.SubdocMutateRequest) GetAccessor(com.couchbase.client.java.kv.GetAccessor) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Optional(java.util.Optional) ReplaceRequest(com.couchbase.client.core.msg.kv.ReplaceRequest) DEFAULT_GET_ALL_REPLICAS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ALL_REPLICAS_OPTIONS) DEFAULT_MUTATE_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_MUTATE_IN_OPTIONS) GetOptions(com.couchbase.client.java.kv.GetOptions) UpsertAccessor(com.couchbase.client.java.kv.UpsertAccessor) RemoveAccessor(com.couchbase.client.java.kv.RemoveAccessor) GetResult(com.couchbase.client.java.kv.GetResult) ExistsAccessor(com.couchbase.client.java.kv.ExistsAccessor) TouchOptions(com.couchbase.client.java.kv.TouchOptions) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) DEFAULT_GET_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_OPTIONS) RemoveRequest(com.couchbase.client.core.msg.kv.RemoveRequest) StoreSemantics(com.couchbase.client.java.kv.StoreSemantics) CompletableFuture(java.util.concurrent.CompletableFuture) CommonDurabilityOptions(com.couchbase.client.java.kv.CommonDurabilityOptions) GetMetaRequest(com.couchbase.client.core.msg.kv.GetMetaRequest) ArrayList(java.util.ArrayList) DEFAULT_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_TOUCH_OPTIONS) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions) ReplaceOptions(com.couchbase.client.java.kv.ReplaceOptions) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) GetAndLockOptions(com.couchbase.client.java.kv.GetAndLockOptions) DEFAULT_REMOVE_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_REMOVE_OPTIONS) MutateInSpec(com.couchbase.client.java.kv.MutateInSpec) Stability(com.couchbase.client.core.annotation.Stability) DEFAULT_EXISTS_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_EXISTS_OPTIONS) DEFAULT_GET_ANY_REPLICA_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_ANY_REPLICA_OPTIONS) UpsertOptions(com.couchbase.client.java.kv.UpsertOptions) LookupInResult(com.couchbase.client.java.kv.LookupInResult) MutateInOptions(com.couchbase.client.java.kv.MutateInOptions) MutateInResult(com.couchbase.client.java.kv.MutateInResult) DEFAULT_UPSERT_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UPSERT_OPTIONS) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) LookupInAccessor(com.couchbase.client.java.kv.LookupInAccessor) DEFAULT_GET_AND_TOUCH_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_GET_AND_TOUCH_OPTIONS) UpsertRequest(com.couchbase.client.core.msg.kv.UpsertRequest) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) DEFAULT_LOOKUP_IN_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_LOOKUP_IN_OPTIONS) Transcoder(com.couchbase.client.java.codec.Transcoder) DurabilityLevel(com.couchbase.client.core.msg.kv.DurabilityLevel) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Comparator(java.util.Comparator) DEFAULT_UNLOCK_OPTIONS(com.couchbase.client.java.ReactiveCollection.DEFAULT_UNLOCK_OPTIONS) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) SubdocCommandType(com.couchbase.client.core.msg.kv.SubdocCommandType) BucketConfigUtil(com.couchbase.client.core.util.BucketConfigUtil) ArrayList(java.util.ArrayList) LookupInSpec(com.couchbase.client.java.kv.LookupInSpec) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest)

Example 32 with CoreContext

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

the class ManagerMessageHandlerTest method closesStreamIfChannelClosed.

/**
 * When a config stream is being processed and the underlying channel closes, the stream should also be closed
 * so the upper level can re-establish a new one.
 */
@Test
void closesStreamIfChannelClosed() throws Exception {
    CoreContext ctx = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create(USER, PASS));
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    EndpointContext endpointContext = mock(EndpointContext.class);
    when(endpointContext.environment()).thenReturn(ENV);
    when(endpoint.context()).thenReturn(endpointContext);
    EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
    BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
    channel.write(request);
    HttpRequest outboundHeader = channel.readOutbound();
    assertEquals(HttpMethod.GET, outboundHeader.method());
    assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
    assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
    CompletableFuture<BucketConfigStreamingResponse> response = request.response();
    assertFalse(response.isDone());
    HttpResponse inboundResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    channel.writeInbound(inboundResponse);
    BucketConfigStreamingResponse completedResponse = request.response().get();
    final AtomicBoolean terminated = new AtomicBoolean(false);
    Thread listener = new Thread(() -> completedResponse.configs().subscribe((v) -> {
    }, (e) -> {
    }, () -> terminated.set(true)));
    listener.setDaemon(true);
    listener.start();
    assertFalse(terminated.get());
    channel.close().awaitUninterruptibly();
    waitUntilCondition(terminated::get);
    channel.finish();
}
Also used : HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ArrayList(java.util.ArrayList) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AfterAll(org.junit.jupiter.api.AfterAll) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) CoreContext(com.couchbase.client.core.CoreContext) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) MockingDetails(org.mockito.MockingDetails) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) Util.readResource(com.couchbase.client.test.Util.readResource) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) IoConfig(com.couchbase.client.core.env.IoConfig) Mockito.mock(org.mockito.Mockito.mock) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Core(com.couchbase.client.core.Core) Test(org.junit.jupiter.api.Test)

Example 33 with CoreContext

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

the class ManagerMessageHandlerTest method returnsNewConfigsWhenChunked.

/**
 * Configs can come in all shapes and sizes chunked, but only after the 4 newlines are pushed by the cluster
 * the config should be propagated into the flux.
 */
@Test
void returnsNewConfigsWhenChunked() throws Exception {
    CoreContext ctx = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create(USER, PASS));
    BaseEndpoint endpoint = mock(BaseEndpoint.class);
    EndpointContext endpointContext = mock(EndpointContext.class);
    when(endpointContext.environment()).thenReturn(ENV);
    when(endpoint.context()).thenReturn(endpointContext);
    EmbeddedChannel channel = new EmbeddedChannel(new ManagerMessageHandler(endpoint, ctx));
    BucketConfigStreamingRequest request = new BucketConfigStreamingRequest(Duration.ofSeconds(1), ctx, BestEffortRetryStrategy.INSTANCE, "bucket", ctx.authenticator());
    channel.write(request);
    HttpRequest outboundHeader = channel.readOutbound();
    assertEquals(HttpMethod.GET, outboundHeader.method());
    assertEquals("/pools/default/bs/bucket", outboundHeader.uri());
    assertEquals(HttpVersion.HTTP_1_1, outboundHeader.protocolVersion());
    CompletableFuture<BucketConfigStreamingResponse> response = request.response();
    assertFalse(response.isDone());
    HttpResponse inboundResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    channel.writeInbound(inboundResponse);
    BucketConfigStreamingResponse completedResponse = request.response().get();
    final List<String> configsPushed = Collections.synchronizedList(new ArrayList<>());
    final AtomicBoolean terminated = new AtomicBoolean(false);
    Thread listener = new Thread(() -> completedResponse.configs().subscribe(configsPushed::add, (e) -> {
    }, () -> terminated.set(true)));
    listener.setDaemon(true);
    listener.start();
    ByteBuf fullContent = Unpooled.copiedBuffer(readResource("terse_stream_two_configs.json", ManagerMessageHandlerTest.class), StandardCharsets.UTF_8);
    while (fullContent.readableBytes() > 0) {
        int len = new Random().nextInt(fullContent.readableBytes() + 1);
        if (len == 0) {
            continue;
        }
        channel.writeInbound(new DefaultHttpContent(fullContent.readBytes(len)));
    }
    waitUntilCondition(() -> configsPushed.size() >= 1);
    for (String config : configsPushed) {
        assertTrue(config.startsWith("{"));
        assertTrue(config.endsWith("}"));
    }
    assertFalse(terminated.get());
    channel.writeInbound(new DefaultLastHttpContent());
    waitUntilCondition(terminated::get);
    ReferenceCountUtil.release(fullContent);
    channel.close().awaitUninterruptibly();
}
Also used : BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) HttpVersion(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpVersion) ReferenceCountUtil(com.couchbase.client.core.deps.io.netty.util.ReferenceCountUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BestEffortRetryStrategy(com.couchbase.client.core.retry.BestEffortRetryStrategy) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ArrayList(java.util.ArrayList) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) AfterAll(org.junit.jupiter.api.AfterAll) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) CoreContext(com.couchbase.client.core.CoreContext) ResourceLeakDetector(com.couchbase.client.core.deps.io.netty.util.ResourceLeakDetector) Duration(java.time.Duration) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) HttpResponseStatus(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponseStatus) MockingDetails(org.mockito.MockingDetails) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) Unpooled(com.couchbase.client.core.deps.io.netty.buffer.Unpooled) HttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpContent) HttpMethod(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpMethod) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Mockito.when(org.mockito.Mockito.when) StandardCharsets(java.nio.charset.StandardCharsets) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) Util.readResource(com.couchbase.client.test.Util.readResource) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) IoConfig(com.couchbase.client.core.env.IoConfig) Mockito.mock(org.mockito.Mockito.mock) CoreContext(com.couchbase.client.core.CoreContext) BucketConfigStreamingResponse(com.couchbase.client.core.msg.manager.BucketConfigStreamingResponse) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) DefaultHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpContent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Random(java.util.Random) DefaultLastHttpContent(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultLastHttpContent) Core(com.couchbase.client.core.Core) HttpRequest(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) BucketConfigStreamingRequest(com.couchbase.client.core.msg.manager.BucketConfigStreamingRequest) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEndpoint(com.couchbase.client.core.endpoint.BaseEndpoint) DefaultHttpResponse(com.couchbase.client.core.deps.io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.jupiter.api.Test)

Example 34 with CoreContext

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

the class QueryMessageHandlerTest method setup.

@BeforeAll
static void setup() {
    ENV = CoreEnvironment.create();
    CORE_CTX = new CoreContext(mock(Core.class), 1, ENV, PasswordAuthenticator.create("user", "pass"));
    ENDPOINT_CTX = new EndpointContext(CORE_CTX, new HostAndPort("127.0.0.1", 1234), NoopCircuitBreaker.INSTANCE, ServiceType.QUERY, Optional.empty(), Optional.empty(), Optional.empty());
}
Also used : HostAndPort(com.couchbase.client.core.util.HostAndPort) CoreContext(com.couchbase.client.core.CoreContext) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 35 with CoreContext

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

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