use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_delegateDoNotAcceptEmptyAddressList_nothing.
@Test
public void handleResolvedAddressGroups_delegateDoNotAcceptEmptyAddressList_nothing() throws Exception {
Helper helper = new TestHelper();
AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"high\" } } ] }");
ConfigOrError lbConfig = lbf.parseLoadBalancerPolicy(serviceConfig);
Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(Collections.<EquivalentAddressGroup>emptyList()).setLoadBalancingPolicyConfig(lbConfig.getConfig()).build());
assertThat(testLbBalancer.canHandleEmptyAddressListFromNameResolution()).isFalse();
assertThat(handleResult.getCode()).isEqualTo(Status.Code.UNAVAILABLE);
assertThat(handleResult.getDescription()).startsWith("NameResolver returned no usable address");
assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
}
use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method forwardsCalls.
@SuppressWarnings("deprecation")
@Test
public void forwardsCalls() {
AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(new TestHelper());
final AtomicInteger calls = new AtomicInteger();
TestLoadBalancer testlb = new TestLoadBalancer() {
@Override
public void handleNameResolutionError(Status error) {
calls.getAndSet(1);
}
@Override
public void handleSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
calls.getAndSet(2);
}
@Override
public void shutdown() {
calls.getAndSet(3);
}
};
lb.setDelegate(testlb);
lb.handleNameResolutionError(Status.RESOURCE_EXHAUSTED);
assertThat(calls.getAndSet(0)).isEqualTo(1);
lb.handleSubchannelState(null, null);
assertThat(calls.getAndSet(0)).isEqualTo(2);
lb.shutdown();
assertThat(calls.getAndSet(0)).isEqualTo(3);
}
use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method channelTracing_lbPolicyChanged.
@Test
public void channelTracing_lbPolicyChanged() throws Exception {
List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
}));
Helper helper = new TestHelper() {
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
return new TestSubchannel(args);
}
};
AutoConfiguredLoadBalancer lb = new AutoConfiguredLoadBalancerFactory(GrpcUtil.DEFAULT_LB_POLICY).newLoadBalancer(helper);
Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).build());
assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
verifyNoMoreInteractions(channelLogger);
ConfigOrError testLbParsedConfig = ConfigOrError.fromConfig("foo");
nextParsedConfigOrError.set(testLbParsedConfig);
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { } } ] }");
ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
verify(channelLogger).log(eq(ChannelLogLevel.INFO), eq("Load balancer changed from {0} to {1}"), eq("PickFirstLoadBalancer"), eq(testLbBalancer.getClass().getSimpleName()));
verify(channelLogger).log(eq(ChannelLogLevel.DEBUG), eq("Load-balancing config: {0}"), eq(testLbParsedConfig.getConfig()));
verifyNoMoreInteractions(channelLogger);
testLbParsedConfig = ConfigOrError.fromConfig("bar");
nextParsedConfigOrError.set(testLbParsedConfig);
serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { } } ] }");
lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
verify(channelLogger).log(eq(ChannelLogLevel.DEBUG), eq("Load-balancing config: {0}"), eq(testLbParsedConfig.getConfig()));
verifyNoMoreInteractions(channelLogger);
}
use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method defaultIsConfigurable.
@Test
public void defaultIsConfigurable() {
AutoConfiguredLoadBalancer lb = new AutoConfiguredLoadBalancerFactory("test_lb").newLoadBalancer(new TestHelper());
assertThat(lb.getDelegateProvider()).isSameInstanceAs(testLbBalancerProvider);
assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
}
use of io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer in project grpc-java by grpc.
the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_shutsDownOldBalancer.
@Test
public void handleResolvedAddressGroups_shutsDownOldBalancer() throws Exception {
Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"round_robin\": { } } ] }");
ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(serviceConfig);
final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
}));
Helper helper = new TestHelper() {
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
assertThat(args.getAddresses()).isEqualTo(servers);
return new TestSubchannel(args);
}
};
AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
final AtomicBoolean shutdown = new AtomicBoolean();
TestLoadBalancer testlb = new TestLoadBalancer() {
@Override
public void handleNameResolutionError(Status error) {
// noop
}
@Override
public void shutdown() {
shutdown.set(true);
}
};
lb.setDelegate(testlb);
Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
assertThat(lb.getDelegateProvider().getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
assertTrue(shutdown.get());
}
Aggregations