Search in sources :

Example 6 with ResolvedServerInfo

use of io.grpc.ResolvedServerInfo 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)

Example 7 with ResolvedServerInfo

use of io.grpc.ResolvedServerInfo in project grpc-java by grpc.

the class ManagedChannelImplIdlenessTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    when(timerServicePool.getObject()).thenReturn(timer.getScheduledExecutorService());
    when(executorPool.getObject()).thenReturn(executor.getScheduledExecutorService());
    when(oobExecutorPool.getObject()).thenReturn(oobExecutor.getScheduledExecutorService());
    when(mockLoadBalancerFactory.newLoadBalancer(any(Helper.class))).thenReturn(mockLoadBalancer);
    when(mockNameResolver.getServiceAuthority()).thenReturn(AUTHORITY);
    when(mockNameResolverFactory.newNameResolver(any(URI.class), any(Attributes.class))).thenReturn(mockNameResolver);
    channel = new ManagedChannelImpl("fake://target", new FakeBackoffPolicyProvider(), mockNameResolverFactory, Attributes.EMPTY, mockLoadBalancerFactory, mockTransportFactory, DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(), timerServicePool, executorPool, oobExecutorPool, timer.getStopwatchSupplier(), TimeUnit.SECONDS.toMillis(IDLE_TIMEOUT_SECONDS), USER_AGENT, Collections.<ClientInterceptor>emptyList(), NoopStatsContextFactory.INSTANCE);
    newTransports = TestUtils.captureTransports(mockTransportFactory);
    for (int i = 0; i < 2; i++) {
        ResolvedServerInfoGroup.Builder resolvedServerInfoGroup = ResolvedServerInfoGroup.builder();
        for (int j = 0; j < 2; j++) {
            resolvedServerInfoGroup.add(new ResolvedServerInfo(new FakeSocketAddress("servergroup" + i + "server" + j)));
        }
        servers.add(resolvedServerInfoGroup.build());
        addressGroupList.add(resolvedServerInfoGroup.build().toEquivalentAddressGroup());
    }
    verify(mockNameResolverFactory).newNameResolver(any(URI.class), any(Attributes.class));
    // Verify the initial idleness
    verify(mockLoadBalancerFactory, never()).newLoadBalancer(any(Helper.class));
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), anyString(), anyString());
    verify(mockNameResolver, never()).start(any(NameResolver.Listener.class));
}
Also used : Attributes(io.grpc.Attributes) ResolvedServerInfoGroup(io.grpc.ResolvedServerInfoGroup) ResolvedServerInfo(io.grpc.ResolvedServerInfo) URI(java.net.URI) Helper(io.grpc.LoadBalancer.Helper) ClientInterceptor(io.grpc.ClientInterceptor) SocketAddress(java.net.SocketAddress) Before(org.junit.Before)

Aggregations

ResolvedServerInfo (io.grpc.ResolvedServerInfo)7 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)5 SocketAddress (java.net.SocketAddress)5 Subchannel (io.grpc.LoadBalancer.Subchannel)4 Attributes (io.grpc.Attributes)3 Test (org.junit.Test)3 InOrder (org.mockito.InOrder)3 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)2 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 Metadata (io.grpc.Metadata)2 MethodDescriptor (io.grpc.MethodDescriptor)2 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 CallOptions (io.grpc.CallOptions)1 ClientInterceptor (io.grpc.ClientInterceptor)1 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)1 Helper (io.grpc.LoadBalancer.Helper)1 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)1