Search in sources :

Example 6 with EndpointState

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

the class PooledServiceTest method retriesRequestIfEndpointCannotConnect.

/**
 * With direct dispatch in the pool it is possible that endpoint for which the socket is
 * waiting for to be dispatched never goes into a connected state but rather into disconnected
 * (and then subsequent reconnect). As soon as we observe a disconnect state we need to retry the
 * op so that it has a chance to complete somewhere else.
 */
@Test
void retriesRequestIfEndpointCannotConnect() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.DISCONNECTED);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    when(mock1.outstandingRequests()).thenReturn(0L);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 2, Duration.ofMillis(500), false), () -> mocks.get(invocation.getAndIncrement()), new FirstEndpointSelectionStrategy());
    service.connect();
    assertTrue(service.trackedEndpoints().isEmpty());
    NoopRequest request = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.DISCONNECTED);
    waitUntilCondition(() -> !service.trackedEndpoints.isEmpty());
    assertEquals(1, service.trackedEndpoints().size());
    waitUntilCondition(() -> request.context().retryAttempts() >= 1);
    verify(mock1, never()).send(request);
}
Also used : EndpointState(com.couchbase.client.core.endpoint.EndpointState) NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) Endpoint(com.couchbase.client.core.endpoint.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Example 7 with EndpointState

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

the class PooledServiceTest method retriesIfNoSlotAvailable.

@Test
void retriesIfNoSlotAvailable() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    when(mock1.outstandingRequests()).thenReturn(1L);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 1, Duration.ofMillis(500), false), () -> mocks.get(invocation.getAndIncrement()), new FirstEndpointSelectionStrategy());
    service.connect();
    assertTrue(service.trackedEndpoints().isEmpty());
    NoopRequest request1 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request1);
    service.send(request2);
    assertEquals(1, service.trackedEndpoints().size());
    // The first request is sent into the free slot without retrying
    assertEquals(0, request1.context().retryAttempts());
    // No more slots available, this one goes into retry
    assertTrue(request2.context().retryAttempts() > 0);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    waitUntilCondition(() -> {
        Collection<Invocation> invocations = Mockito.mockingDetails(mock1).getInvocations();
        for (Invocation inv : invocations) {
            if (inv.getMethod().getName().equals("send")) {
                if (inv.getArgument(0) == request1) {
                    return true;
                }
            }
        }
        return false;
    });
    verify(mock1, never()).send(request2);
}
Also used : EndpointState(com.couchbase.client.core.endpoint.EndpointState) NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Invocation(org.mockito.invocation.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Aggregations

Endpoint (com.couchbase.client.core.endpoint.Endpoint)7 EndpointState (com.couchbase.client.core.endpoint.EndpointState)7 NoopRequest (com.couchbase.client.core.msg.kv.NoopRequest)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 Test (org.junit.jupiter.api.Test)7 Invocation (org.mockito.invocation.Invocation)2