Search in sources :

Example 1 with LoadBalancer

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

the class GracefulSwitchLoadBalancerTest method createSubchannelForwarded.

@Test
public void createSubchannelForwarded() {
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[0]));
    LoadBalancer lb0 = balancers.get(lbPolicies[0]);
    Helper helper0 = helpers.get(lb0);
    SubchannelPicker picker = mock(SubchannelPicker.class);
    helper0.updateBalancingState(READY, picker);
    CreateSubchannelArgs createSubchannelArgs = newFakeCreateSubchannelArgs();
    helper0.createSubchannel(createSubchannelArgs);
    verify(mockHelper).createSubchannel(createSubchannelArgs);
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[1]));
    LoadBalancer lb1 = balancers.get(lbPolicies[1]);
    Helper helper1 = helpers.get(lb1);
    createSubchannelArgs = newFakeCreateSubchannelArgs();
    helper1.createSubchannel(createSubchannelArgs);
    verify(mockHelper).createSubchannel(createSubchannelArgs);
    createSubchannelArgs = newFakeCreateSubchannelArgs();
    helper0.createSubchannel(createSubchannelArgs);
    verify(mockHelper).createSubchannel(createSubchannelArgs);
    verifyNoMoreInteractions(lb0, lb1);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 2 with LoadBalancer

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

the class GracefulSwitchLoadBalancerTest method shutdownTriggeredWhenSwitchAndForwardedWhenSwitchLbShutdown.

@Test
public void shutdownTriggeredWhenSwitchAndForwardedWhenSwitchLbShutdown() {
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[0]));
    LoadBalancer lb0 = balancers.get(lbPolicies[0]);
    Helper helper0 = helpers.get(lb0);
    SubchannelPicker picker = mock(SubchannelPicker.class);
    helper0.updateBalancingState(READY, picker);
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[1]));
    LoadBalancer lb1 = balancers.get(lbPolicies[1]);
    verify(lb1, never()).shutdown();
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[2]));
    verify(lb1).shutdown();
    LoadBalancer lb2 = balancers.get(lbPolicies[2]);
    verify(lb0, never()).shutdown();
    helpers.get(lb2).updateBalancingState(READY, mock(SubchannelPicker.class));
    verify(lb0).shutdown();
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[3]));
    LoadBalancer lb3 = balancers.get(lbPolicies[3]);
    verify(lb2, never()).shutdown();
    verify(lb3, never()).shutdown();
    gracefulSwitchLb.shutdown();
    verify(lb2).shutdown();
    verify(lb3).shutdown();
    verifyNoMoreInteractions(lb0, lb1, lb2, lb3);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 3 with LoadBalancer

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

the class GracefulSwitchLoadBalancerTest method newLbFactoryEqualToOldOneShouldHaveNoEffect.

@Test
public void newLbFactoryEqualToOldOneShouldHaveNoEffect() {
    final List<LoadBalancer> balancers = new ArrayList<>();
    final class LoadBalancerFactoryWithId extends LoadBalancer.Factory {

        final int id;

        LoadBalancerFactoryWithId(int id) {
            this.id = id;
        }

        @Override
        public LoadBalancer newLoadBalancer(Helper helper) {
            LoadBalancer balancer = mock(LoadBalancer.class);
            balancers.add(balancer);
            return balancer;
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof LoadBalancerFactoryWithId)) {
                return false;
            }
            LoadBalancerFactoryWithId that = (LoadBalancerFactoryWithId) o;
            return id == that.id;
        }

        @Override
        public int hashCode() {
            return id;
        }
    }
    gracefulSwitchLb.switchTo(new LoadBalancerFactoryWithId(0));
    assertThat(balancers).hasSize(1);
    LoadBalancer lb0 = balancers.get(0);
    gracefulSwitchLb.switchTo(new LoadBalancerFactoryWithId(0));
    assertThat(balancers).hasSize(1);
    gracefulSwitchLb.switchTo(new LoadBalancerFactoryWithId(1));
    assertThat(balancers).hasSize(2);
    LoadBalancer lb1 = balancers.get(1);
    verify(lb0).shutdown();
    verifyNoMoreInteractions(lb0, lb1);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) ArrayList(java.util.ArrayList) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 4 with LoadBalancer

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

the class GracefulSwitchLoadBalancerTest method canHandleEmptyAddressListFromNameResolutionForwardedToLatestPolicy.

@Test
public void canHandleEmptyAddressListFromNameResolutionForwardedToLatestPolicy() {
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[0]));
    LoadBalancer lb0 = balancers.get(lbPolicies[0]);
    Helper helper0 = helpers.get(lb0);
    SubchannelPicker picker = mock(SubchannelPicker.class);
    helper0.updateBalancingState(READY, picker);
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isFalse();
    doReturn(true).when(lb0).canHandleEmptyAddressListFromNameResolution();
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isTrue();
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[1]));
    LoadBalancer lb1 = balancers.get(lbPolicies[1]);
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isFalse();
    doReturn(true).when(lb1).canHandleEmptyAddressListFromNameResolution();
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isTrue();
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[2]));
    LoadBalancer lb2 = balancers.get(lbPolicies[2]);
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isFalse();
    doReturn(true).when(lb2).canHandleEmptyAddressListFromNameResolution();
    assertThat(gracefulSwitchLb.canHandleEmptyAddressListFromNameResolution()).isTrue();
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 5 with LoadBalancer

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

the class GracefulSwitchLoadBalancerTest method updateBalancingStateIsGraceful.

@Test
public void updateBalancingStateIsGraceful() {
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[0]));
    LoadBalancer lb0 = balancers.get(lbPolicies[0]);
    Helper helper0 = helpers.get(lb0);
    SubchannelPicker picker = mock(SubchannelPicker.class);
    helper0.updateBalancingState(READY, picker);
    verify(mockHelper).updateBalancingState(READY, picker);
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[1]));
    LoadBalancer lb1 = balancers.get(lbPolicies[1]);
    Helper helper1 = helpers.get(lb1);
    picker = mock(SubchannelPicker.class);
    helper1.updateBalancingState(CONNECTING, picker);
    verify(mockHelper, never()).updateBalancingState(CONNECTING, picker);
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[2]));
    verify(lb1).shutdown();
    LoadBalancer lb2 = balancers.get(lbPolicies[2]);
    Helper helper2 = helpers.get(lb2);
    picker = mock(SubchannelPicker.class);
    helper2.updateBalancingState(CONNECTING, picker);
    verify(mockHelper, never()).updateBalancingState(CONNECTING, picker);
    // lb2 reports READY
    SubchannelPicker picker2 = mock(SubchannelPicker.class);
    helper2.updateBalancingState(READY, picker2);
    verify(lb0).shutdown();
    verify(mockHelper).updateBalancingState(READY, picker2);
    gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[3]));
    LoadBalancer lb3 = balancers.get(lbPolicies[3]);
    Helper helper3 = helpers.get(lb3);
    SubchannelPicker picker3 = mock(SubchannelPicker.class);
    helper3.updateBalancingState(CONNECTING, picker3);
    verify(mockHelper, never()).updateBalancingState(CONNECTING, picker3);
    // lb2 out of READY
    picker2 = mock(SubchannelPicker.class);
    helper2.updateBalancingState(CONNECTING, picker2);
    verify(mockHelper, never()).updateBalancingState(CONNECTING, picker2);
    verify(mockHelper).updateBalancingState(CONNECTING, picker3);
    verify(lb2).shutdown();
    picker3 = mock(SubchannelPicker.class);
    helper3.updateBalancingState(CONNECTING, picker3);
    verify(mockHelper).updateBalancingState(CONNECTING, picker3);
    verifyNoMoreInteractions(lb0, lb1, lb2, lb3);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Aggregations

LoadBalancer (io.grpc.LoadBalancer)27 Test (org.junit.Test)22 Helper (io.grpc.LoadBalancer.Helper)20 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)13 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)5 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)4 LoadBalancerProvider (io.grpc.LoadBalancerProvider)4 Status (io.grpc.Status)4 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)4 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)4 Attributes (io.grpc.Attributes)2 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 PickResult (io.grpc.LoadBalancer.PickResult)2 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 SynchronizationContext (io.grpc.SynchronizationContext)2 GracefulSwitchLoadBalancer (io.grpc.util.GracefulSwitchLoadBalancer)2 ErrorPicker (io.grpc.xds.XdsSubchannelPickers.ErrorPicker)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2