Search in sources :

Example 16 with ResolvedServerInfoGroup

use of io.grpc.ResolvedServerInfoGroup 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);
}
Also used : InOrder(org.mockito.InOrder) Attributes(io.grpc.Attributes) ResolvedServerInfo(io.grpc.ResolvedServerInfo) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) ConnectivityStateInfo(io.grpc.ConnectivityStateInfo) Subchannel(io.grpc.LoadBalancer.Subchannel) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Picker(io.grpc.util.RoundRobinLoadBalancerFactory.Picker) Test(org.junit.Test)

Aggregations

ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)16 Test (org.junit.Test)13 Attributes (io.grpc.Attributes)12 Status (io.grpc.Status)9 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 InOrder (org.mockito.InOrder)6 Subchannel (io.grpc.LoadBalancer.Subchannel)5 ResolvedServerInfo (io.grpc.ResolvedServerInfo)5 ErrorPicker (io.grpc.grpclb.GrpclbLoadBalancer.ErrorPicker)5 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 SocketAddress (java.net.SocketAddress)4 Helper (io.grpc.LoadBalancer.Helper)3 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)3 ManagedChannel (io.grpc.ManagedChannel)3 Metadata (io.grpc.Metadata)3 MethodDescriptor (io.grpc.MethodDescriptor)3 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)3 Matchers.anyString (org.mockito.Matchers.anyString)3 CallOptions (io.grpc.CallOptions)2 ArrayList (java.util.ArrayList)2