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