Search in sources :

Example 11 with RequestContext

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

the class BaseEndpointTest method writeAndFlushToChannelIfFullyConnected.

/**
 * If we are fully connected and the circuit breaker is closed, make sure that we can
 * write the request into the channel and it is flushed as well.
 */
@Test
@SuppressWarnings({ "unchecked" })
void writeAndFlushToChannelIfFullyConnected() {
    EmbeddedChannel channel = new EmbeddedChannel();
    InstrumentedEndpoint endpoint = connectSuccessfully(channel);
    assertEquals(0, endpoint.lastResponseReceived());
    Request<Response> request = mock(Request.class);
    CompletableFuture<Response> response = new CompletableFuture<>();
    when(request.response()).thenReturn(response);
    when(request.context()).thenReturn(new RequestContext(ctx, request));
    assertEquals(0, endpoint.outstandingRequests());
    endpoint.send(request);
    assertEquals(1, endpoint.outstandingRequests());
    assertEquals(request, channel.readOutbound());
    assertEquals(0, endpoint.lastResponseReceived());
    response.complete(mock(Response.class));
    endpoint.markRequestCompletion();
    assertTrue(endpoint.lastResponseReceived() > 0);
    assertEquals(0, endpoint.outstandingRequests());
}
Also used : Response(com.couchbase.client.core.msg.Response) CompletableFuture(java.util.concurrent.CompletableFuture) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Example 12 with RequestContext

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

the class RetryOrchestratorTest method cancelIfNoMoreRetriesAllowed.

@Test
@SuppressWarnings({ "unchecked" })
void cancelIfNoMoreRetriesAllowed() {
    RetryStrategy retryStrategy = mock(RetryStrategy.class);
    when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.noRetry()));
    Request<?> request = mock(Request.class);
    when(request.completed()).thenReturn(false);
    when(request.retryStrategy()).thenReturn(retryStrategy);
    RequestContext requestContext = mock(RequestContext.class);
    when(request.context()).thenReturn(requestContext);
    CoreEnvironment env = mock(CoreEnvironment.class);
    SimpleEventBus eventBus = new SimpleEventBus(true);
    when(env.eventBus()).thenReturn(eventBus);
    CoreContext context = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    RetryOrchestrator.maybeRetry(context, request, RetryReason.UNKNOWN);
    verify(request, times(1)).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN), Function.identity());
    assertEquals(1, eventBus.publishedEvents().size());
    RequestNotRetriedEvent retryEvent = (RequestNotRetriedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.INFO, retryEvent.severity());
    assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
    assertEquals(requestContext, retryEvent.context());
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) RequestNotRetriedEvent(com.couchbase.client.core.cnc.events.request.RequestNotRetriedEvent) Request(com.couchbase.client.core.msg.Request) 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 13 with RequestContext

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

the class LoggingEventConsumerTest method doesNotAttachClientContextByDefault.

@Test
void doesNotAttachClientContextByDefault() {
    RequestContext context = mock(RequestContext.class);
    Map<String, Object> userContext = new HashMap<>();
    userContext.put("hello", "world");
    when(context.clientContext()).thenReturn(userContext);
    RequestRetryScheduledEvent retryEvent = new RequestRetryScheduledEvent(Duration.ofSeconds(1), context, GetRequest.class, RetryReason.UNKNOWN);
    loggingEventConsumer.accept(retryEvent);
    verify(logger, never()).attachContext(userContext);
}
Also used : RequestRetryScheduledEvent(com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent) HashMap(java.util.HashMap) RequestContext(com.couchbase.client.core.msg.RequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

RequestContext (com.couchbase.client.core.msg.RequestContext)13 Test (org.junit.jupiter.api.Test)12 Core (com.couchbase.client.core.Core)4 CoreContext (com.couchbase.client.core.CoreContext)4 RequestRetryScheduledEvent (com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)4 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)4 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)4 NodeInfo (com.couchbase.client.core.config.NodeInfo)4 Authenticator (com.couchbase.client.core.env.Authenticator)4 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)4 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)4 ArrayList (java.util.ArrayList)4 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)3 Request (com.couchbase.client.core.msg.Request)3 Timer (com.couchbase.client.core.Timer)2 Service (com.couchbase.client.core.service.Service)2 ServiceType (com.couchbase.client.core.service.ServiceType)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 InitGlobalConfigFailedEvent (com.couchbase.client.core.cnc.events.core.InitGlobalConfigFailedEvent)1