use of com.couchbase.client.core.CoreContext 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);
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class AsyncCollection method mutateInRequest.
/**
* Helper method to create the underlying subdoc mutate request.
*
* @param id the outer document ID.
* @param specs the spec which specifies the type of mutations to perform.
* @param opts custom options to modify the mutation options.
* @return the subdoc mutate request.
*/
CompletableFuture<SubdocMutateRequest> mutateInRequest(final String id, final List<MutateInSpec> specs, final MutateInOptions.Built opts, final Duration timeout) {
notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
notNullOrEmpty(specs, "MutateInSpecs", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
if (specs.isEmpty()) {
throw SubdocMutateRequest.errIfNoCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
} else if (specs.size() > SubdocMutateRequest.SUBDOC_MAX_FIELDS) {
throw SubdocMutateRequest.errIfTooManyCommands(ReducedKeyValueErrorContext.create(id, collectionIdentifier));
}
final boolean requiresBucketConfig = opts.createAsDeleted() || opts.storeSemantics() == StoreSemantics.REVIVE;
CompletableFuture<BucketConfig> bucketConfigFuture;
if (requiresBucketConfig) {
bucketConfigFuture = BucketConfigUtil.waitForBucketConfig(core, bucketName(), timeout).toFuture();
} else {
// Nothing will be using the bucket config so just provide null
bucketConfigFuture = CompletableFuture.completedFuture(null);
}
return bucketConfigFuture.thenCompose(bucketConfig -> {
RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
JsonSerializer serializer = opts.serializer() == null ? environment.jsonSerializer() : opts.serializer();
final RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_MUTATE_IN, opts.parentSpan().orElse(null));
ArrayList<SubdocMutateRequest.Command> commands = new ArrayList<>(specs.size());
final RequestSpan encodeSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_ENCODING, span);
long start = System.nanoTime();
try {
for (int i = 0; i < specs.size(); i++) {
MutateInSpec spec = specs.get(i);
commands.add(spec.encode(serializer, i));
}
} finally {
encodeSpan.end();
}
long end = System.nanoTime();
// xattrs come first
commands.sort(Comparator.comparing(v -> !v.xattr()));
long expiry = opts.expiry().encode();
SubdocMutateRequest request = new SubdocMutateRequest(timeout, coreContext, collectionIdentifier, bucketConfig, retryStrategy, id, opts.storeSemantics() == StoreSemantics.INSERT, opts.storeSemantics() == StoreSemantics.UPSERT, opts.storeSemantics() == StoreSemantics.REVIVE, opts.accessDeleted(), opts.createAsDeleted(), commands, expiry, opts.preserveExpiry(), opts.cas(), opts.durabilityLevel(), span);
request.context().clientContext(opts.clientContext()).encodeLatency(end - start);
final CompletableFuture<SubdocMutateRequest> future = new CompletableFuture<>();
future.complete(request);
return future;
});
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method beforeEach.
@BeforeEach
void beforeEach() {
SimpleEventBus eventBus = new SimpleEventBus(true);
env = CoreEnvironment.builder().eventBus(eventBus).build();
CoreContext coreContext = mock(CoreContext.class);
core = mock(Core.class);
when(core.context()).thenReturn(coreContext);
when(coreContext.environment()).thenReturn(env);
ConfigurationProvider provider = mock(ConfigurationProvider.class);
refresher = new ClusterManagerBucketRefresher(provider, core);
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method retryOnTimeoutUntilEventuallyConnected.
/**
* This test fakes a situation where the channel future from netty would simply not return
* at all and time out, and the client would resubscribe. Then at some future attempt the
* future returns fine and we should end up in a connected state and ready to go.
*/
@Test
void retryOnTimeoutUntilEventuallyConnected() {
SimpleEventBus eventBus = new SimpleEventBus(true);
CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
try {
final CompletableFuture<Channel> cf = new CompletableFuture<>();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
endpoint.connect();
waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
EmbeddedChannel channel = new EmbeddedChannel();
cf.complete(channel);
waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
assertTrue(eventBus.publishedEvents().size() >= 3);
boolean failedFound = false;
boolean successFound = false;
for (Event event : eventBus.publishedEvents()) {
if (event instanceof EndpointConnectionFailedEvent) {
assertEquals(Event.Severity.WARN, event.severity());
assertEquals(Duration.ofMillis(10), event.duration());
failedFound = true;
} else if (event instanceof EndpointConnectedEvent) {
assertEquals(Event.Severity.DEBUG, event.severity());
assertTrue(event.duration().toNanos() > 0);
successFound = true;
}
}
assertTrue(failedFound);
assertTrue(successFound);
} finally {
env.shutdown();
}
}
use of com.couchbase.client.core.CoreContext 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());
}
Aggregations