Search in sources :

Example 21 with Subchannel

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

the class ManagedChannelImplTest method subchannelsWhenChannelShutdownNow.

@Test
public void subchannelsWhenChannelShutdownNow() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    Subchannel sub1 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    Subchannel sub2 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    sub1.requestConnection();
    sub2.requestConnection();
    assertEquals(2, transports.size());
    MockClientTransportInfo ti1 = transports.poll();
    MockClientTransportInfo ti2 = transports.poll();
    ti1.listener.transportReady();
    ti2.listener.transportReady();
    channel.shutdownNow();
    verify(ti1.transport).shutdownNow(any(Status.class));
    verify(ti2.transport).shutdownNow(any(Status.class));
    ti1.listener.transportShutdown(Status.UNAVAILABLE.withDescription("shutdown now"));
    ti2.listener.transportShutdown(Status.UNAVAILABLE.withDescription("shutdown now"));
    ti1.listener.transportTerminated();
    assertFalse(channel.isTerminated());
    ti2.listener.transportTerminated();
    assertTrue(channel.isTerminated());
}
Also used : Status(io.grpc.Status) Subchannel(io.grpc.LoadBalancer.Subchannel) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Test(org.junit.Test)

Example 22 with Subchannel

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

the class PickFirstLoadBalancerTest method pickAfterResolvedAndChanged.

@Test
public void pickAfterResolvedAndChanged() throws Exception {
    SocketAddress socketAddr = new FakeSocketAddress("newserver");
    List<SocketAddress> newSocketAddresses = Lists.newArrayList(socketAddr);
    List<ResolvedServerInfoGroup> newServers = Lists.newArrayList(ResolvedServerInfoGroup.builder().add(new ResolvedServerInfo(socketAddr)).build());
    final Subchannel oldSubchannel = mock(Subchannel.class);
    final EquivalentAddressGroup oldEag = new EquivalentAddressGroup(socketAddresses);
    when(oldSubchannel.getAddresses()).thenReturn(oldEag);
    final Subchannel newSubchannel = mock(Subchannel.class);
    final EquivalentAddressGroup newEag = new EquivalentAddressGroup(newSocketAddresses);
    when(newSubchannel.getAddresses()).thenReturn(newEag);
    when(mockHelper.createSubchannel(eq(oldEag), any(Attributes.class))).thenReturn(oldSubchannel);
    when(mockHelper.createSubchannel(eq(newEag), any(Attributes.class))).thenReturn(newSubchannel);
    InOrder inOrder = inOrder(mockHelper);
    loadBalancer.handleResolvedAddresses(servers, affinity);
    inOrder.verify(mockHelper).createSubchannel(eagCaptor.capture(), any(Attributes.class));
    inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
    assertEquals(socketAddresses, eagCaptor.getValue().getAddresses());
    loadBalancer.handleResolvedAddresses(newServers, affinity);
    inOrder.verify(mockHelper).createSubchannel(eagCaptor.capture(), any(Attributes.class));
    inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
    assertEquals(newSocketAddresses, eagCaptor.getValue().getAddresses());
    Subchannel subchannel = pickerCaptor.getAllValues().get(0).pickSubchannel(mockArgs).getSubchannel();
    assertEquals(oldSubchannel, subchannel);
    Subchannel subchannel2 = pickerCaptor.getAllValues().get(1).pickSubchannel(mockArgs).getSubchannel();
    assertEquals(newSubchannel, subchannel2);
    verify(subchannel2, never()).shutdown();
    verifyNoMoreInteractions(mockHelper);
}
Also used : InOrder(org.mockito.InOrder) Subchannel(io.grpc.LoadBalancer.Subchannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Aggregations

Subchannel (io.grpc.LoadBalancer.Subchannel)22 Test (org.junit.Test)18 Attributes (io.grpc.Attributes)8 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)8 SocketAddress (java.net.SocketAddress)8 Metadata (io.grpc.Metadata)7 InOrder (org.mockito.InOrder)7 Matchers.anyString (org.mockito.Matchers.anyString)7 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)6 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)6 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)5 Status (io.grpc.Status)5 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 ResolvedServerInfo (io.grpc.ResolvedServerInfo)4 Picker (io.grpc.util.RoundRobinLoadBalancerFactory.Picker)4 CallOptions (io.grpc.CallOptions)3 MethodDescriptor (io.grpc.MethodDescriptor)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)2 Helper (io.grpc.LoadBalancer.Helper)2