Search in sources :

Example 21 with Endpoint

use of com.couchbase.client.core.endpoint.Endpoint 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)

Example 22 with Endpoint

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

the class PartitionSelectionStrategyTest method selectPinnedForBinaryWithKey.

@Test
void selectPinnedForBinaryWithKey() {
    EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
    Endpoint endpoint1 = mock(Endpoint.class);
    Endpoint endpoint2 = mock(Endpoint.class);
    Endpoint endpoint3 = mock(Endpoint.class);
    when(endpoint1.state()).thenReturn(EndpointState.CONNECTED);
    when(endpoint2.state()).thenReturn(EndpointState.CONNECTED);
    when(endpoint3.state()).thenReturn(EndpointState.CONNECTED);
    when(endpoint1.freeToWrite()).thenReturn(true);
    when(endpoint2.freeToWrite()).thenReturn(true);
    when(endpoint3.freeToWrite()).thenReturn(true);
    List<Endpoint> endpoints = Arrays.asList(endpoint1, endpoint2, endpoint3);
    GetRequest request = mock(GetRequest.class);
    when(request.partition()).thenReturn((short) 12);
    Endpoint selected = strategy.select(request, endpoints);
    for (int i = 0; i < 1000; i++) {
        assertNotNull(selected);
        assertEquals(selected, endpoint1);
    }
}
Also used : Endpoint(com.couchbase.client.core.endpoint.Endpoint) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) EndpointSelectionStrategy(com.couchbase.client.core.service.EndpointSelectionStrategy) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Example 23 with Endpoint

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

the class PartitionSelectionStrategyTest method returnNullIfEmptyEndpointList.

@Test
@SuppressWarnings("unchecked")
void returnNullIfEmptyEndpointList() {
    EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
    Endpoint selected = strategy.select(mock(Request.class), Collections.emptyList());
    assertNull(selected);
}
Also used : Endpoint(com.couchbase.client.core.endpoint.Endpoint) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Request(com.couchbase.client.core.msg.Request) EndpointSelectionStrategy(com.couchbase.client.core.service.EndpointSelectionStrategy) Test(org.junit.jupiter.api.Test)

Example 24 with Endpoint

use of com.couchbase.client.core.endpoint.Endpoint 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

Endpoint (com.couchbase.client.core.endpoint.Endpoint)24 Test (org.junit.jupiter.api.Test)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 NoopRequest (com.couchbase.client.core.msg.kv.NoopRequest)9 EndpointState (com.couchbase.client.core.endpoint.EndpointState)8 ServiceConnectInitiatedEvent (com.couchbase.client.core.cnc.events.service.ServiceConnectInitiatedEvent)3 ServiceDisconnectInitiatedEvent (com.couchbase.client.core.cnc.events.service.ServiceDisconnectInitiatedEvent)3 Request (com.couchbase.client.core.msg.Request)3 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)3 EndpointSelectionStrategy (com.couchbase.client.core.service.EndpointSelectionStrategy)3 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Invocation (org.mockito.invocation.Invocation)2 IdleEndpointRemovedEvent (com.couchbase.client.core.cnc.events.service.IdleEndpointRemovedEvent)1 ServiceStateChangedEvent (com.couchbase.client.core.cnc.events.service.ServiceStateChangedEvent)1 EndpointDiagnostics (com.couchbase.client.core.diagnostics.EndpointDiagnostics)1 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)1 Response (com.couchbase.client.core.msg.Response)1 KeyValueRequest (com.couchbase.client.core.msg.kv.KeyValueRequest)1 RetryOrchestrator (com.couchbase.client.core.retry.RetryOrchestrator)1