Search in sources :

Example 31 with CreateSubchannelArgs

use of io.grpc.LoadBalancer.CreateSubchannelArgs in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < 3; i++) {
        SocketAddress addr = new FakeSocketAddress("server" + i);
        EquivalentAddressGroup eag = new EquivalentAddressGroup(addr);
        servers.add(eag);
        Subchannel sc = mock(Subchannel.class);
        subchannels.put(Arrays.asList(eag), sc);
    }
    when(mockHelper.createSubchannel(any(CreateSubchannelArgs.class))).then(new Answer<Subchannel>() {

        @Override
        public Subchannel answer(InvocationOnMock invocation) throws Throwable {
            CreateSubchannelArgs args = (CreateSubchannelArgs) invocation.getArguments()[0];
            final Subchannel subchannel = subchannels.get(args.getAddresses());
            when(subchannel.getAllAddresses()).thenReturn(args.getAddresses());
            when(subchannel.getAttributes()).thenReturn(args.getAttributes());
            doAnswer(new Answer<Void>() {

                @Override
                public Void answer(InvocationOnMock invocation) throws Throwable {
                    subchannelStateListeners.put(subchannel, (SubchannelStateListener) invocation.getArguments()[0]);
                    return null;
                }
            }).when(subchannel).start(any(SubchannelStateListener.class));
            return subchannel;
        }
    });
    loadBalancer = new LeastRequestLoadBalancer(mockHelper, mockRandom);
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Example 32 with CreateSubchannelArgs

use of io.grpc.LoadBalancer.CreateSubchannelArgs in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method pickAfterResolved.

@Test
public void pickAfterResolved() throws Exception {
    final Subchannel readySubchannel = subchannels.values().iterator().next();
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).build());
    deliverSubchannelState(readySubchannel, ConnectivityStateInfo.forNonError(READY));
    verify(mockHelper, times(3)).createSubchannel(createArgsCaptor.capture());
    List<List<EquivalentAddressGroup>> capturedAddrs = new ArrayList<>();
    for (CreateSubchannelArgs arg : createArgsCaptor.getAllValues()) {
        capturedAddrs.add(arg.getAddresses());
    }
    assertThat(capturedAddrs).containsAtLeastElementsIn(subchannels.keySet());
    for (Subchannel subchannel : subchannels.values()) {
        verify(subchannel).requestConnection();
        verify(subchannel, never()).shutdown();
    }
    verify(mockHelper, times(2)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
    assertEquals(CONNECTING, stateCaptor.getAllValues().get(0));
    assertEquals(READY, stateCaptor.getAllValues().get(1));
    assertThat(getList(pickerCaptor.getValue())).containsExactly(readySubchannel);
    verifyNoMoreInteractions(mockHelper);
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) Subchannel(io.grpc.LoadBalancer.Subchannel) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)32 Test (org.junit.Test)29 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 Subchannel (io.grpc.LoadBalancer.Subchannel)14 InOrder (org.mockito.InOrder)13 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 Status (io.grpc.Status)9 SocketAddress (java.net.SocketAddress)8 Attributes (io.grpc.Attributes)6 ConnectivityState (io.grpc.ConnectivityState)6 Helper (io.grpc.LoadBalancer.Helper)6 SubchannelStateListener (io.grpc.LoadBalancer.SubchannelStateListener)6 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)5 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)5 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)5 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)5 InetSocketAddress (java.net.InetSocketAddress)5 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)4 InitialLoadBalanceRequest (io.grpc.lb.v1.InitialLoadBalanceRequest)4 LoadBalanceRequest (io.grpc.lb.v1.LoadBalanceRequest)4