Search in sources :

Example 6 with Endpoint

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

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

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

the class PooledServiceTest method ignoresConnectAfterDisconnect.

/**
 * Once disconnected, make sure that another connect attempt is plainly
 * ignored.
 */
@Test
void ignoresConnectAfterDisconnect() {
    int minEndpoints = 2;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.states()).thenReturn(DirectProcessor.create());
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTED);
    when(mock2.states()).thenReturn(DirectProcessor.create());
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 2), () -> mocks.get(invocation.getAndIncrement()));
    service.connect();
    service.disconnect();
    service.connect();
    assertEquals(minEndpoints, service.trackedEndpoints().size());
    assertEquals(2, eventBus.publishedEvents().size());
}
Also used : 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 9 with Endpoint

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

the class PooledServiceTest method doNotSendRequestIfCompleted.

/**
 * If a request is completed already, do not attempt to send it in the first place.
 */
@Test
@SuppressWarnings({ "unchecked" })
void doNotSendRequestIfCompleted() {
    int minEndpoints = 2;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    when(mock1.states()).thenReturn(DirectProcessor.create());
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTED);
    when(mock2.states()).thenReturn(DirectProcessor.create());
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 2), () -> mocks.get(invocation.getAndIncrement()), new FirstEndpointSelectionStrategy());
    service.connect();
    Request<? extends Response> request = mock(Request.class);
    when(request.completed()).thenReturn(true);
    service.send(request);
    verify(mock1, never()).send(request);
    verify(mock2, never()).send(request);
}
Also used : 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 10 with Endpoint

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

the class PooledServiceTest method tracksMinEndpointStateAfterConnect.

/**
 * After a connect call, the minimum number of endpoints should be created
 * and the overall state of the service should track their state.
 */
@Test
void tracksMinEndpointStateAfterConnect() {
    int minEndpoints = 2;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTING);
    when(mock1.states()).thenReturn(DirectProcessor.create());
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTING);
    when(mock2.states()).thenReturn(DirectProcessor.create());
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 10), () -> mocks.get(invocation.getAndIncrement()));
    service.connect();
    assertEquals(minEndpoints, service.trackedEndpoints().size());
    assertEquals(ServiceState.CONNECTING, service.state());
    verify(mock1, times(1)).connect();
    verify(mock2, times(1)).connect();
    ServiceConnectInitiatedEvent event = (ServiceConnectInitiatedEvent) eventBus.publishedEvents().get(0);
    assertEquals(2, event.minimumEndpoints());
    assertEquals("Starting to connect service with 2 minimum endpoints", event.description());
}
Also used : Endpoint(com.couchbase.client.core.endpoint.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceConnectInitiatedEvent(com.couchbase.client.core.cnc.events.service.ServiceConnectInitiatedEvent) 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