use of io.grpc.LoadBalancer.Helper 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);
}
use of io.grpc.LoadBalancer.Helper 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();
}
use of io.grpc.LoadBalancer.Helper 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);
}
use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.
the class GracefulSwitchLoadBalancerTest method switchWhileOldPolicyGoesFromReadyToNotReadyWhileNewPolicyStillIdle.
@Test
public void switchWhileOldPolicyGoesFromReadyToNotReadyWhileNewPolicyStillIdle() {
gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[0]));
LoadBalancer lb0 = balancers.get(lbPolicies[0]);
InOrder inOrder = inOrder(lb0, mockHelper);
Helper helper0 = helpers.get(lb0);
SubchannelPicker picker = mock(SubchannelPicker.class);
helper0.updateBalancingState(READY, picker);
gracefulSwitchLb.switchTo(lbProviders.get(lbPolicies[1]));
verify(lb0, never()).shutdown();
LoadBalancer lb1 = balancers.get(lbPolicies[1]);
Helper helper1 = helpers.get(lb1);
picker = mock(SubchannelPicker.class);
helper0.updateBalancingState(CONNECTING, picker);
verify(mockHelper, never()).updateBalancingState(CONNECTING, picker);
inOrder.verify(mockHelper).updateBalancingState(CONNECTING, BUFFER_PICKER);
// shutdown after update
inOrder.verify(lb0).shutdown();
picker = mock(SubchannelPicker.class);
helper1.updateBalancingState(CONNECTING, picker);
inOrder.verify(mockHelper).updateBalancingState(CONNECTING, picker);
inOrder.verifyNoMoreInteractions();
verifyNoMoreInteractions(lb1);
}
use of io.grpc.LoadBalancer.Helper in project grpc-java by grpc.
the class PriorityLoadBalancerTest method bypassReresolutionRequestsIfConfiged.
@Test
public void bypassReresolutionRequestsIfConfiged() {
PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), false);
PriorityLbConfig priorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p0", priorityChildConfig0, "p1", priorityChildConfig1), ImmutableList.of("p0", "p1"));
priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).build());
// priority p0
Helper priorityHelper0 = Iterables.getOnlyElement(fooHelpers);
priorityHelper0.refreshNameResolution();
verify(helper, never()).refreshNameResolution();
// Simulate fallback to priority p1.
priorityHelper0.updateBalancingState(TRANSIENT_FAILURE, new ErrorPicker(Status.UNAVAILABLE));
assertThat(fooHelpers).hasSize(2);
Helper priorityHelper1 = Iterables.getLast(fooHelpers);
priorityHelper1.refreshNameResolution();
verify(helper).refreshNameResolution();
}
Aggregations