@Test void selectNullIfPinedIsNotConnected() { EndpointSelectionStrategy strategy = new PartitionSelectionStrategy(); Endpoint endpoint1 = mock(Endpoint.class); Endpoint endpoint2 = mock(Endpoint.class); Endpoint endpoint3 = mock(Endpoint.class); when(endpoint1.state()).thenReturn(EndpointState.DISCONNECTED); 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++) { assertNull(selected); } }
@Override public <R extends Request<? extends Response>> Endpoint select(final R request, final List<Endpoint> endpoints) { int size = endpoints.size(); if (size == 0) { return null; } short partition = ((KeyValueRequest<?>) request).partition(); Endpoint endpoint = size == 1 ? endpoints.get(0) : endpoints.get(partition % size); if (endpoint != null && endpoint.state() == EndpointState.CONNECTED && endpoint.freeToWrite()) { return endpoint; } return null; }
PartitionSelectionStrategy implements EndpointSelectionStrategy { @Override public <R extends Request<? extends Response>> Endpoint select(final R request, final List<Endpoint> endpoints) { int size = endpoints.size(); if (size == 0) { return null; } short partition = ((KeyValueRequest<?>) request).partition(); Endpoint endpoint = size == 1 ? endpoints.get(0) : endpoints.get(partition % size); if (endpoint != null && endpoint.state() == EndpointState.CONNECTED && endpoint.freeToWrite()) { return endpoint; } return null; } }
PartitionSelectionStrategy implements EndpointSelectionStrategy { @Override public <R extends Request<? extends Response>> Endpoint select(final R request, final List<Endpoint> endpoints) { int size = endpoints.size(); if (size == 0) { return null; } short partition = ((KeyValueRequest<?>) request).partition(); Endpoint endpoint = size == 1 ? endpoints.get(0) : endpoints.get(partition % size); if (endpoint != null && endpoint.state() == EndpointState.CONNECTED && endpoint.freeToWrite()) { return endpoint; } return null; }  }
PartitionSelectionStrategy implements EndpointSelectionStrategy { @Override public <R extends Request<? extends Response>> Endpoint select(final R request, final List<Endpoint> endpoints) { int size = endpoints.size(); if (size == 0) { return null; } short partition = ((KeyValueRequest<?>) request).partition(); Endpoint endpoint = size == 1 ? endpoints.get(0) : endpoints.get(partition % size); if (endpoint != null && endpoint.state() == EndpointState.CONNECTED && endpoint.freeToWrite()) { return endpoint; } return null; }  @Override Endpoint select(final R request, final List<Endpoint> endpoints); }
PartitionSelectionStrategy implements EndpointSelectionStrategy { @Override public <R extends Request<? extends Response>> Endpoint select(final R request, final List<Endpoint> endpoints) { int size = endpoints.size(); if (size == 0) { return null; } short partition = ((KeyValueRequest<?>) request).partition(); Endpoint endpoint = size == 1 ? endpoints.get(0) : endpoints.get(partition % size); if (endpoint != null && endpoint.state() == EndpointState.CONNECTED && endpoint.freeToWrite()) { return endpoint; } return null; }  @Override Endpoint select(final R request, final List<Endpoint> endpoints);  }