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