Search in sources :

Example 16 with Subchannel

use of io.grpc.LoadBalancer.Subchannel 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());
}
Also used : Subchannel(io.grpc.LoadBalancer.Subchannel) Picker(io.grpc.util.RoundRobinLoadBalancerFactory.Picker) Test(org.junit.Test)

Example 17 with Subchannel

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

the class RoundRobinLoadBalancerTest method pickAfterStateChange.

@Test
public void pickAfterStateChange() throws Exception {
    InOrder inOrder = inOrder(mockHelper);
    loadBalancer.handleResolvedAddresses(Lists.newArrayList(servers.keySet()), Attributes.EMPTY);
    Subchannel subchannel = loadBalancer.getSubchannels().iterator().next();
    AtomicReference<ConnectivityStateInfo> subchannelStateInfo = subchannel.getAttributes().get(STATE_INFO);
    inOrder.verify(mockHelper).updatePicker(isA(Picker.class));
    assertThat(subchannelStateInfo.get()).isEqualTo(ConnectivityStateInfo.forNonError(IDLE));
    loadBalancer.handleSubchannelState(subchannel, ConnectivityStateInfo.forNonError(ConnectivityState.READY));
    inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
    assertNull(pickerCaptor.getValue().getStatus());
    assertThat(subchannelStateInfo.get()).isEqualTo(ConnectivityStateInfo.forNonError(ConnectivityState.READY));
    Status error = Status.UNKNOWN.withDescription("¯\\_(ツ)_//¯");
    loadBalancer.handleSubchannelState(subchannel, ConnectivityStateInfo.forTransientFailure(error));
    assertThat(subchannelStateInfo.get()).isEqualTo(ConnectivityStateInfo.forTransientFailure(error));
    inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
    assertNull(pickerCaptor.getValue().getStatus());
    loadBalancer.handleSubchannelState(subchannel, ConnectivityStateInfo.forNonError(ConnectivityState.IDLE));
    inOrder.verify(mockHelper).updatePicker(pickerCaptor.capture());
    assertNull(pickerCaptor.getValue().getStatus());
    assertThat(subchannelStateInfo.get()).isEqualTo(ConnectivityStateInfo.forNonError(ConnectivityState.IDLE));
    verify(subchannel, times(2)).requestConnection();
    verify(mockHelper, times(3)).createSubchannel(any(EquivalentAddressGroup.class), any(Attributes.class));
    verifyNoMoreInteractions(mockHelper);
}
Also used : Status(io.grpc.Status) InOrder(org.mockito.InOrder) ConnectivityStateInfo(io.grpc.ConnectivityStateInfo) Subchannel(io.grpc.LoadBalancer.Subchannel) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Picker(io.grpc.util.RoundRobinLoadBalancerFactory.Picker) Attributes(io.grpc.Attributes) Test(org.junit.Test)

Example 18 with Subchannel

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

the class ManagedChannelImplIdlenessTest method realTransportsHoldsOffIdleness.

@Test
public void realTransportsHoldsOffIdleness() throws Exception {
    final EquivalentAddressGroup addressGroup = addressGroupList.get(1);
    // Start a call, which goes to delayed transport
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    // Verify that we have exited the idle mode
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(null);
    verify(mockLoadBalancerFactory).newLoadBalancer(helperCaptor.capture());
    Helper helper = helperCaptor.getValue();
    assertTrue(channel.inUseStateAggregator.isInUse());
    // Assume LoadBalancer has received an address, then create a subchannel.
    Subchannel subchannel = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    subchannel.requestConnection();
    MockClientTransportInfo t0 = newTransports.poll();
    t0.listener.transportReady();
    SubchannelPicker mockPicker = mock(SubchannelPicker.class);
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    helper.updatePicker(mockPicker);
    // Delayed transport creates real streams in the app executor
    executor.runDueTasks();
    // Delayed transport exits in-use, while real transport has not entered in-use yet.
    assertFalse(channel.inUseStateAggregator.isInUse());
    // Now it's in-use
    t0.listener.transportInUse(true);
    assertTrue(channel.inUseStateAggregator.isInUse());
    // As long as the transport is in-use, the channel won't go idle.
    timer.forwardTime(IDLE_TIMEOUT_SECONDS * 2, TimeUnit.SECONDS);
    assertTrue(channel.inUseStateAggregator.isInUse());
    t0.listener.transportInUse(false);
    assertFalse(channel.inUseStateAggregator.isInUse());
    // And allow the channel to go idle.
    timer.forwardTime(IDLE_TIMEOUT_SECONDS - 1, TimeUnit.SECONDS);
    verify(mockLoadBalancer, never()).shutdown();
    timer.forwardTime(1, TimeUnit.SECONDS);
    verify(mockLoadBalancer).shutdown();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 19 with Subchannel

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

the class ManagedChannelImplTest method subchannelsNoConnectionShutdown.

@Test
public void subchannelsNoConnectionShutdown() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    Subchannel sub1 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    Subchannel sub2 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    sub1.shutdown();
    assertFalse(channel.isTerminated());
    sub2.shutdown();
    assertTrue(channel.isTerminated());
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), anyString(), anyString());
}
Also used : Subchannel(io.grpc.LoadBalancer.Subchannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 20 with Subchannel

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

the class ManagedChannelImplTest method subchannels.

@Test
public void subchannels() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    // createSubchannel() always return a new Subchannel
    Attributes attrs1 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr1").build();
    Attributes attrs2 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr2").build();
    Subchannel sub1 = helper.createSubchannel(addressGroup, attrs1);
    Subchannel sub2 = helper.createSubchannel(addressGroup, attrs2);
    assertNotSame(sub1, sub2);
    assertNotSame(attrs1, attrs2);
    assertSame(attrs1, sub1.getAttributes());
    assertSame(attrs2, sub2.getAttributes());
    assertSame(addressGroup, sub1.getAddresses());
    assertSame(addressGroup, sub2.getAddresses());
    // requestConnection()
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), any(String.class), any(String.class));
    sub1.requestConnection();
    verify(mockTransportFactory).newClientTransport(socketAddress, authority, userAgent);
    MockClientTransportInfo transportInfo1 = transports.poll();
    assertNotNull(transportInfo1);
    sub2.requestConnection();
    verify(mockTransportFactory, times(2)).newClientTransport(socketAddress, authority, userAgent);
    MockClientTransportInfo transportInfo2 = transports.poll();
    assertNotNull(transportInfo2);
    sub1.requestConnection();
    sub2.requestConnection();
    verify(mockTransportFactory, times(2)).newClientTransport(socketAddress, authority, userAgent);
    // shutdown() has a delay
    sub1.shutdown();
    timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS - 1, TimeUnit.SECONDS);
    sub1.shutdown();
    verify(transportInfo1.transport, never()).shutdown();
    timer.forwardTime(1, TimeUnit.SECONDS);
    verify(transportInfo1.transport).shutdown();
    // ... but not after Channel is terminating
    verify(mockLoadBalancer, never()).shutdown();
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    verify(transportInfo2.transport, never()).shutdown();
    sub2.shutdown();
    verify(transportInfo2.transport).shutdown();
}
Also used : Subchannel(io.grpc.LoadBalancer.Subchannel) Attributes(io.grpc.Attributes) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Matchers.anyString(org.mockito.Matchers.anyString) 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