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();
}
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);
}
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);
}
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);
}
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();
}
Aggregations