use of io.grpc.util.RoundRobinLoadBalancerFactory.Picker in project grpc-java by grpc.
the class RoundRobinLoadBalancerTest method pickerEmptyList.
@Test
public void pickerEmptyList() throws Exception {
Picker picker = new Picker(Lists.<Subchannel>newArrayList(), Status.UNKNOWN);
assertEquals(null, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(Status.UNKNOWN, picker.pickSubchannel(mockArgs).getStatus());
}
use of io.grpc.util.RoundRobinLoadBalancerFactory.Picker in project grpc-java by grpc.
the class RoundRobinLoadBalancerTest method subchannelStateIsolation.
@Test
public void subchannelStateIsolation() throws Exception {
Iterator<Subchannel> subchannelIterator = subchannels.values().iterator();
Subchannel sc1 = subchannelIterator.next();
Subchannel sc2 = subchannelIterator.next();
Subchannel sc3 = subchannelIterator.next();
loadBalancer.handleResolvedAddresses(Lists.newArrayList(servers.keySet()), Attributes.EMPTY);
verify(sc1, times(1)).requestConnection();
verify(sc2, times(1)).requestConnection();
verify(sc3, times(1)).requestConnection();
loadBalancer.handleSubchannelState(sc1, ConnectivityStateInfo.forNonError(READY));
loadBalancer.handleSubchannelState(sc2, ConnectivityStateInfo.forNonError(READY));
loadBalancer.handleSubchannelState(sc3, ConnectivityStateInfo.forNonError(READY));
loadBalancer.handleSubchannelState(sc2, ConnectivityStateInfo.forNonError(IDLE));
loadBalancer.handleSubchannelState(sc3, ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE));
verify(mockHelper, times(6)).updatePicker(pickerCaptor.capture());
Iterator<Picker> pickers = pickerCaptor.getAllValues().iterator();
// The picker is incrementally updated as subchannels become READY
assertThat(pickers.next().getList()).isEmpty();
assertThat(pickers.next().getList()).containsExactly(sc1);
assertThat(pickers.next().getList()).containsExactly(sc1, sc2);
assertThat(pickers.next().getList()).containsExactly(sc1, sc2, sc3);
// The IDLE subchannel is dropped from the picker, but a reconnection is requested
assertThat(pickers.next().getList()).containsExactly(sc1, sc3);
verify(sc2, times(2)).requestConnection();
// The failing subchannel is dropped from the picker, with no requested reconnect
assertThat(pickers.next().getList()).containsExactly(sc1);
verify(sc3, times(1)).requestConnection();
assertThat(pickers.hasNext()).isFalse();
}
use of io.grpc.util.RoundRobinLoadBalancerFactory.Picker in project grpc-java by grpc.
the class RoundRobinLoadBalancerTest method pickAfterResolvedUpdatedHosts.
@Test
public void pickAfterResolvedUpdatedHosts() throws Exception {
Subchannel removedSubchannel = mock(Subchannel.class);
Subchannel oldSubchannel = mock(Subchannel.class);
Subchannel newSubchannel = mock(Subchannel.class);
for (Subchannel subchannel : Lists.newArrayList(removedSubchannel, oldSubchannel, newSubchannel)) {
when(subchannel.getAttributes()).thenReturn(Attributes.newBuilder().set(STATE_INFO, new AtomicReference<ConnectivityStateInfo>(ConnectivityStateInfo.forNonError(READY))).build());
}
FakeSocketAddress removedAddr = new FakeSocketAddress("removed");
FakeSocketAddress oldAddr = new FakeSocketAddress("old");
FakeSocketAddress newAddr = new FakeSocketAddress("new");
final Map<EquivalentAddressGroup, Subchannel> subchannels2 = Maps.newHashMap();
subchannels2.put(new EquivalentAddressGroup(removedAddr), removedSubchannel);
subchannels2.put(new EquivalentAddressGroup(oldAddr), oldSubchannel);
List<ResolvedServerInfoGroup> currentServers = Lists.newArrayList(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(removedAddr)).add(new ResolvedServerInfo(oldAddr)).build());
doAnswer(new Answer<Subchannel>() {
@Override
public Subchannel answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return subchannels2.get(args[0]);
}
}).when(mockHelper).createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class));
loadBalancer.handleResolvedAddresses(currentServers, affinity);
InOrder inOrder = inOrder(mockHelper);
inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
Picker picker = pickerCaptor.getValue();
assertNull(picker.getStatus());
assertThat(picker.getList()).containsExactly(removedSubchannel, oldSubchannel);
verify(removedSubchannel, times(1)).requestConnection();
verify(oldSubchannel, times(1)).requestConnection();
assertThat(loadBalancer.getSubchannels()).containsExactly(removedSubchannel, oldSubchannel);
subchannels2.clear();
subchannels2.put(new EquivalentAddressGroup(oldAddr), oldSubchannel);
subchannels2.put(new EquivalentAddressGroup(newAddr), newSubchannel);
List<ResolvedServerInfoGroup> latestServers = Lists.newArrayList(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(oldAddr)).add(new ResolvedServerInfo(newAddr)).build());
loadBalancer.handleResolvedAddresses(latestServers, affinity);
verify(newSubchannel, times(1)).requestConnection();
verify(removedSubchannel, times(1)).shutdown();
assertThat(loadBalancer.getSubchannels()).containsExactly(oldSubchannel, newSubchannel);
verify(mockHelper, times(3)).createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class));
inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
picker = pickerCaptor.getValue();
assertNull(picker.getStatus());
assertThat(picker.getList()).containsExactly(oldSubchannel, newSubchannel);
verifyNoMoreInteractions(mockHelper);
}
use of io.grpc.util.RoundRobinLoadBalancerFactory.Picker in project grpc-java by grpc.
the class RoundRobinLoadBalancerTest method pickerRoundRobin.
@Test
public void pickerRoundRobin() throws Exception {
Subchannel subchannel = mock(Subchannel.class);
Subchannel subchannel1 = mock(Subchannel.class);
Subchannel subchannel2 = mock(Subchannel.class);
Picker picker = new Picker(Collections.unmodifiableList(Lists.<Subchannel>newArrayList(subchannel, subchannel1, subchannel2)), null);
assertThat(picker.getList()).containsExactly(subchannel, subchannel1, subchannel2);
assertEquals(subchannel, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(subchannel1, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(subchannel2, picker.pickSubchannel(mockArgs).getSubchannel());
assertEquals(subchannel, picker.pickSubchannel(mockArgs).getSubchannel());
}
Aggregations