Search in sources :

Example 1 with EndpointState

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

the class PooledServiceTest method dispatchesDirectlyIfSlotAvailable.

@Test
void dispatchesDirectlyIfSlotAvailable() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    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.CONNECTED);
    waitUntilCondition(() -> !service.trackedEndpoints.isEmpty());
    assertEquals(1, service.trackedEndpoints().size());
    assertEquals(0, request.context().retryAttempts());
    waitUntilCondition(() -> {
        Collection<Invocation> invocations = Mockito.mockingDetails(mock1).getInvocations();
        for (Invocation inv : invocations) {
            if (inv.getMethod().getName().equals("send")) {
                if (inv.getArgument(0) == request) {
                    return true;
                }
            }
        }
        return false;
    });
    verify(mock1, times(1)).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) 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 2 with EndpointState

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

the class PooledServiceTest method cleansIdleConnections.

@Test
void cleansIdleConnections() throws Exception {
    int minEndpoints = 0;
    long now = System.nanoTime();
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    when(mock1.outstandingRequests()).thenReturn(1L);
    when(mock1.lastResponseReceived()).thenReturn(now);
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTED);
    when(mock2.states()).thenReturn(DirectProcessor.create());
    when(mock2.outstandingRequests()).thenReturn(1L);
    when(mock2.lastResponseReceived()).thenReturn(now);
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    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 request1 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request1);
    NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request2);
    waitUntilCondition(() -> service.trackedEndpoints.size() == 2);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    when(mock1.outstandingRequests()).thenReturn(0L);
    Thread.sleep(600);
    verify(mock1, times(1)).disconnect();
    verify(mock2, never()).disconnect();
}
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 3 with EndpointState

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

the class PooledServiceTest method cleansUpReservedEndpointIfDisconnected.

/**
 * It can happen that while the reserved endpoint connects,
 * the overall pool got the disconnect signal in the meantime.
 * <p>
 * If this happens, make sure we clean up everything properly.
 */
@Test
void cleansUpReservedEndpointIfDisconnected() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    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);
    service.disconnect();
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.DISCONNECTED);
    waitUntilCondition(() -> request.context().retryAttempts() >= 1);
    verify(mock1, never()).send(request);
    verify(mock1, atLeastOnce()).disconnect();
}
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 4 with EndpointState

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

the class PooledServiceTest method cleansDisconnectedEndpoints.

/**
 * Double check that terminated / disconnected connections (by the sdk) are also double checked and
 * cleaned up to prevent leaking.
 */
@Test
void cleansDisconnectedEndpoints() {
    int minEndpoints = 0;
    long now = System.nanoTime();
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states1 = DirectProcessor.create();
    when(mock1.states()).thenReturn(states1);
    when(mock1.outstandingRequests()).thenReturn(1L);
    // trying different format due to a CI error with mockito
    doReturn(now).when(mock1).lastResponseReceived();
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states2 = DirectProcessor.create();
    when(mock2.states()).thenReturn(states2);
    when(mock2.outstandingRequests()).thenReturn(1L);
    // trying different format due to a CI error with mockito
    doReturn(now).when(mock2).lastResponseReceived();
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    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 request1 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request1);
    states1.onNext(EndpointState.CONNECTING);
    states1.onNext(EndpointState.CONNECTED);
    NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request2);
    states2.onNext(EndpointState.CONNECTING);
    states2.onNext(EndpointState.CONNECTED);
    waitUntilCondition(() -> service.trackedEndpoints.size() == 2);
    when(mock1.receivedDisconnectSignal()).thenReturn(true);
    when(mock2.receivedDisconnectSignal()).thenReturn(true);
    waitUntilCondition(() -> service.state() == ServiceState.IDLE);
}
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 5 with EndpointState

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

the class PooledServiceTest method cleansUpNeverUsedIdleConnections.

@Test
void cleansUpNeverUsedIdleConnections() {
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    when(mock1.outstandingRequests()).thenReturn(0L);
    when(mock1.lastResponseReceived()).thenReturn(0L);
    when(mock1.lastConnectedAt()).thenReturn(0L);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(0, 2, Duration.ofMillis(1000), 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"));
    service.send(request1);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    when(mock1.lastConnectedAt()).thenReturn(System.nanoTime());
    waitUntilCondition(() -> service.trackedEndpoints.size() == 1);
    when(mock1.receivedDisconnectSignal()).thenReturn(true);
    waitUntilCondition(() -> service.state() == ServiceState.IDLE);
}
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) 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