Search in sources :

Example 6 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest 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 7 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest 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 8 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest 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 9 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest 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 10 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest 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)

Aggregations

NoopRequest (com.couchbase.client.core.msg.kv.NoopRequest)20 Test (org.junit.jupiter.api.Test)20 NoopResponse (com.couchbase.client.core.msg.kv.NoopResponse)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Endpoint (com.couchbase.client.core.endpoint.Endpoint)9 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)8 RequestContext (com.couchbase.client.core.msg.RequestContext)8 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)8 EndpointState (com.couchbase.client.core.endpoint.EndpointState)7 StepVerifier (reactor.test.StepVerifier)6 RequestCanceledException (com.couchbase.client.core.error.RequestCanceledException)3 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)3 TestNodeConfig (com.couchbase.client.test.TestNodeConfig)3 Invocation (org.mockito.invocation.Invocation)2 Disposable (reactor.core.Disposable)2 Bootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap)1 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)1 SocketChannel (com.couchbase.client.core.deps.io.netty.channel.socket.SocketChannel)1 NioSocketChannel (com.couchbase.client.core.deps.io.netty.channel.socket.nio.NioSocketChannel)1 CancellationReason (com.couchbase.client.core.msg.CancellationReason)1