### Question:
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);  }### Answer:
@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); } }
@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); } }
@Test @SuppressWarnings("unchecked") void returnNullIfEmptyEndpointList() { EndpointSelectionStrategy strategy = new PartitionSelectionStrategy(); Endpoint selected = strategy.select(mock(Request.class), Collections.emptyList()); assertNull(selected); }