use of com.couchbase.client.core.endpoint.Endpoint in project couchbase-jvm-clients by couchbase.
the class PooledServiceTest method retriesIfNoSlotAvailable.
@Test
void retriesIfNoSlotAvailable() {
int minEndpoints = 0;
Endpoint mock1 = mock(Endpoint.class);
when(mock1.state()).thenReturn(EndpointState.CONNECTED);
when(mock1.outstandingRequests()).thenReturn(1L);
DirectProcessor<EndpointState> states = DirectProcessor.create();
when(mock1.states()).thenReturn(states);
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();
assertTrue(service.trackedEndpoints().isEmpty());
NoopRequest request1 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
service.send(request1);
service.send(request2);
assertEquals(1, service.trackedEndpoints().size());
// The first request is sent into the free slot without retrying
assertEquals(0, request1.context().retryAttempts());
// No more slots available, this one goes into retry
assertTrue(request2.context().retryAttempts() > 0);
// Simulate the connecting and connected
states.onNext(EndpointState.CONNECTING);
states.onNext(EndpointState.CONNECTED);
waitUntilCondition(() -> {
Collection<Invocation> invocations = Mockito.mockingDetails(mock1).getInvocations();
for (Invocation inv : invocations) {
if (inv.getMethod().getName().equals("send")) {
if (inv.getArgument(0) == request1) {
return true;
}
}
}
return false;
});
verify(mock1, never()).send(request2);
}
use of com.couchbase.client.core.endpoint.Endpoint in project couchbase-jvm-clients by couchbase.
the class PartitionSelectionStrategyTest method selectPinnedForBinaryWithKey.
@Test
void selectPinnedForBinaryWithKey() {
EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
Endpoint endpoint1 = mock(Endpoint.class);
Endpoint endpoint2 = mock(Endpoint.class);
Endpoint endpoint3 = mock(Endpoint.class);
when(endpoint1.state()).thenReturn(EndpointState.CONNECTED);
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++) {
assertNotNull(selected);
assertEquals(selected, endpoint1);
}
}
use of com.couchbase.client.core.endpoint.Endpoint in project couchbase-jvm-clients by couchbase.
the class PartitionSelectionStrategyTest method returnNullIfEmptyEndpointList.
@Test
@SuppressWarnings("unchecked")
void returnNullIfEmptyEndpointList() {
EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
Endpoint selected = strategy.select(mock(Request.class), Collections.emptyList());
assertNull(selected);
}
use of com.couchbase.client.core.endpoint.Endpoint in project couchbase-jvm-clients by couchbase.
the class RoundRobinSelectionStrategyTest method testRoundRobinSelectOverIntegerMaxValue.
@Test
@SuppressWarnings("unchecked")
void testRoundRobinSelectOverIntegerMaxValue() {
RoundRobinSelectionStrategy strategy = new RoundRobinSelectionStrategy();
Endpoint a = Mockito.mock(Endpoint.class);
Endpoint b = Mockito.mock(Endpoint.class);
Endpoint c = Mockito.mock(Endpoint.class);
Endpoint d = Mockito.mock(Endpoint.class);
Endpoint e = Mockito.mock(Endpoint.class);
when(a.state()).thenReturn(EndpointState.CONNECTED);
when(a.freeToWrite()).thenReturn(true);
when(b.state()).thenReturn(EndpointState.CONNECTED);
when(b.freeToWrite()).thenReturn(true);
when(c.state()).thenReturn(EndpointState.CONNECTED);
when(c.freeToWrite()).thenReturn(true);
when(d.state()).thenReturn(EndpointState.CONNECTED);
when(d.freeToWrite()).thenReturn(true);
when(e.state()).thenReturn(EndpointState.CONNECTED);
when(e.freeToWrite()).thenReturn(true);
List<Endpoint> endpoints = Arrays.asList(a, b, c, d, e);
Request request = Mockito.mock(Request.class);
strategy.setSkip(Integer.MAX_VALUE - 2);
// selecting brings skip to max-value - 1
strategy.select(request, endpoints);
int skipStart = strategy.currentSkip();
assertTrue(skipStart > 1000);
// max-value
strategy.select(request, endpoints);
assertEquals(skipStart + 1, strategy.currentSkip());
assertTrue(strategy.currentSkip() > 0);
// max-value + 1: wrapping
Endpoint selected = strategy.select(request, endpoints);
assertEquals(0, strategy.currentSkip());
assertEquals(selected, a);
// following selects will select B, C, D, E, A and increment skip to 5
selected = strategy.select(request, endpoints);
assertEquals(1, strategy.currentSkip());
assertEquals(selected, b);
selected = strategy.select(request, endpoints);
assertEquals(2, strategy.currentSkip());
assertEquals(selected, c);
selected = strategy.select(request, endpoints);
assertEquals(3, strategy.currentSkip());
assertEquals(selected, d);
selected = strategy.select(request, endpoints);
assertEquals(4, strategy.currentSkip());
assertEquals(selected, e);
selected = strategy.select(request, endpoints);
assertEquals(5, strategy.currentSkip());
assertEquals(selected, a);
}
Aggregations