Search in sources :

Example 11 with Endpoint

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

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

Example 13 with Endpoint

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

the class PooledServiceTest method retriesIfFixedSize.

@Test
void retriesIfFixedSize() {
    int minEndpoints = 1;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    when(mock1.states()).thenReturn(DirectProcessor.create());
    when(mock1.outstandingRequests()).thenReturn(1L);
    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();
    assertFalse(service.trackedEndpoints().isEmpty());
    NoopRequest request = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request);
    assertEquals(1, service.trackedEndpoints().size());
    assertTrue(request.context().retryAttempts() > 0);
    verify(mock1, never()).send(request);
}
Also used : 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 14 with Endpoint

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

the class PooledServiceTest method doesNotCleanJustOpenedConnections.

/**
 * This test makes sure that when a socket has just been opened and no operation has gone through yet,
 * it is not cleaned up by the idle cleaner.
 * <p>
 * This is a regression test for JVMCBC-856.
 */
@Test
void doesNotCleanJustOpenedConnections() throws Exception {
    long now = System.nanoTime();
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    when(mock1.states()).thenReturn(DirectProcessor.create());
    when(mock1.outstandingRequests()).thenReturn(0L);
    when(mock1.lastResponseReceived()).thenReturn(0L);
    when(mock1.lastConnectedAt()).thenReturn(now);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(0, 2, Duration.ofMillis(5000), 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);
    waitUntilCondition(() -> service.trackedEndpoints.size() == 1);
    Thread.sleep(300);
    verify(mock1, never()).disconnect();
}
Also used : 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)

Example 15 with Endpoint

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

the class PartitionSelectionStrategyTest method selectNullIfPinedIsNotConnected.

@Test
void selectNullIfPinedIsNotConnected() {
    EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
    Endpoint endpoint1 = mock(Endpoint.class);
    Endpoint endpoint2 = mock(Endpoint.class);
    Endpoint endpoint3 = mock(Endpoint.class);
    when(endpoint1.state()).thenReturn(EndpointState.DISCONNECTED);
    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++) {
        assertNull(selected);
    }
}
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)

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