Search in sources :

Example 6 with Request

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

the class NodeTest method retriesIfLocalServiceNotFound.

@Test
@SuppressWarnings({ "unchecked" })
void retriesIfLocalServiceNotFound() {
    final Service s = mock(Service.class);
    final AtomicReference<Request<?>> retried = new AtomicReference<>();
    Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {

        @Override
        protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
            when(s.state()).thenReturn(ServiceState.CONNECTED);
            when(s.states()).thenReturn(DirectProcessor.create());
            when(s.type()).thenReturn(serviceType);
            return s;
        }

        @Override
        protected <R extends Request<? extends Response>> void sendIntoRetry(R request) {
            retried.set(request);
        }
    };
    node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
    KeyValueRequest r = mock(KeyValueRequest.class);
    when(r.serviceType()).thenReturn(ServiceType.KV);
    when(r.bucket()).thenReturn("other_bucket");
    node.send(r);
    verify(s, never()).send(eq(r));
    assertEquals(r, retried.get());
}
Also used : Response(com.couchbase.client.core.msg.Response) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Optional(java.util.Optional) ServiceType(com.couchbase.client.core.service.ServiceType) QueryRequest(com.couchbase.client.core.msg.query.QueryRequest) Request(com.couchbase.client.core.msg.Request) KeyValueRequest(com.couchbase.client.core.msg.kv.KeyValueRequest) Service(com.couchbase.client.core.service.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 7 with Request

use of com.couchbase.client.core.msg.Request 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 8 with Request

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

the class RoundRobinSelectionStrategyTest method testRoundRobinSelectOverIntegerMaxValue.

@Test
@SuppressWarnings("unchecked")
void testRoundRobinSelectOverIntegerMaxValue() {
    RoundRobinSelectionStrategy strategy = new RoundRobinSelectionStrategy();
    Endpoint a = Mockito.mock(Endpoint.class);
    Endpoint b = Mockito.mock(Endpoint.class);
    Endpoint c = Mockito.mock(Endpoint.class);
    Endpoint d = Mockito.mock(Endpoint.class);
    Endpoint e = Mockito.mock(Endpoint.class);
    when(a.state()).thenReturn(EndpointState.CONNECTED);
    when(a.freeToWrite()).thenReturn(true);
    when(b.state()).thenReturn(EndpointState.CONNECTED);
    when(b.freeToWrite()).thenReturn(true);
    when(c.state()).thenReturn(EndpointState.CONNECTED);
    when(c.freeToWrite()).thenReturn(true);
    when(d.state()).thenReturn(EndpointState.CONNECTED);
    when(d.freeToWrite()).thenReturn(true);
    when(e.state()).thenReturn(EndpointState.CONNECTED);
    when(e.freeToWrite()).thenReturn(true);
    List<Endpoint> endpoints = Arrays.asList(a, b, c, d, e);
    Request request = Mockito.mock(Request.class);
    strategy.setSkip(Integer.MAX_VALUE - 2);
    // selecting brings skip to max-value - 1
    strategy.select(request, endpoints);
    int skipStart = strategy.currentSkip();
    assertTrue(skipStart > 1000);
    // max-value
    strategy.select(request, endpoints);
    assertEquals(skipStart + 1, strategy.currentSkip());
    assertTrue(strategy.currentSkip() > 0);
    // max-value + 1: wrapping
    Endpoint selected = strategy.select(request, endpoints);
    assertEquals(0, strategy.currentSkip());
    assertEquals(selected, a);
    // following selects will select B, C, D, E, A and increment skip to 5
    selected = strategy.select(request, endpoints);
    assertEquals(1, strategy.currentSkip());
    assertEquals(selected, b);
    selected = strategy.select(request, endpoints);
    assertEquals(2, strategy.currentSkip());
    assertEquals(selected, c);
    selected = strategy.select(request, endpoints);
    assertEquals(3, strategy.currentSkip());
    assertEquals(selected, d);
    selected = strategy.select(request, endpoints);
    assertEquals(4, strategy.currentSkip());
    assertEquals(selected, e);
    selected = strategy.select(request, endpoints);
    assertEquals(5, strategy.currentSkip());
    assertEquals(selected, a);
}
Also used : Endpoint(com.couchbase.client.core.endpoint.Endpoint) Request(com.couchbase.client.core.msg.Request) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Aggregations

Request (com.couchbase.client.core.msg.Request)8 Test (org.junit.jupiter.api.Test)6 CoreContext (com.couchbase.client.core.CoreContext)4 Response (com.couchbase.client.core.msg.Response)4 Core (com.couchbase.client.core.Core)3 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)3 Authenticator (com.couchbase.client.core.env.Authenticator)3 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)3 RequestContext (com.couchbase.client.core.msg.RequestContext)3 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)3 ServiceType (com.couchbase.client.core.service.ServiceType)3 Optional (java.util.Optional)3 Timer (com.couchbase.client.core.Timer)2 RequestRetryScheduledEvent (com.couchbase.client.core.cnc.events.request.RequestRetryScheduledEvent)2 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)2 RetryOrchestrator (com.couchbase.client.core.retry.RetryOrchestrator)2 RetryReason (com.couchbase.client.core.retry.RetryReason)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CbTracing (com.couchbase.client.core.cnc.CbTracing)1