use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method emptyAddresses_validConfig_2ndResolution_lbNeedsAddress.
@Test
public void emptyAddresses_validConfig_2ndResolution_lbNeedsAddress() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(new ArrayList<>(ImmutableList.of(addressGroup))).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"12\"}}]}");
nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("12");
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(true)).isEqualTo(ConnectivityState.IDLE);
reset(mockLoadBalancer);
nameResolverFactory.servers.clear();
// 2nd resolution
nameResolverFactory.allResolved();
// 2nd service config without addresses
ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
verify(mockLoadBalancer, never()).handleResolvedAddresses(any(ResolvedAddresses.class));
verify(mockLoadBalancer).handleNameResolutionError(statusCaptor.capture());
assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.UNAVAILABLE);
assertThat(statusCaptor.getValue().getDescription()).contains("NameResolver returned no usable address.");
assertThat(channel.getState(true)).isEqualTo(ConnectivityState.TRANSIENT_FAILURE);
assertWithMessage("Empty address should schedule NameResolver retry").that(getNameResolverRefresh()).isNotNull();
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method invalidConfig_withDefaultConfig.
@Test
public void invalidConfig_withDefaultConfig() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> defaultServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"mate\"}}]}");
channelBuilder.defaultServiceConfig(defaultServiceConfig);
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("mate");
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method noConfig_noDefaultConfig.
@Test
public void noConfig_noDefaultConfig() {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
nameResolverFactory.nextRawServiceConfig.set(null);
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isNull();
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method invalidConfig_2ndResolution.
@Test
public void invalidConfig_2ndResolution() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"1st raw config\"}}]}");
nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
InternalConfigSelector configSelector = mock(InternalConfigSelector.class);
nameResolverFactory.nextAttributes.set(Attributes.newBuilder().set(InternalConfigSelector.KEY, configSelector).build());
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
reset(mockLoadBalancer);
// 2nd resolution lbConfig is error
nextLbPolicyConfigError.set(Status.UNKNOWN);
nameResolverFactory.allResolved();
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses newResolvedAddress = resultCaptor.getValue();
// should use previous service config because new service config is invalid.
assertThat(newResolvedAddress.getLoadBalancingPolicyConfig()).isEqualTo("1st raw config");
assertThat(channel.getConfigSelector()).isSameInstanceAs(configSelector);
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isEqualTo(ConnectivityState.IDLE);
}
use of io.grpc.LoadBalancer.ResolvedAddresses in project grpc-java by grpc.
the class ServiceConfigErrorHandlingTest method validConfig_lbDoesNotNeedAddress.
@Test
public void validConfig_lbDoesNotNeedAddress() throws Exception {
FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(ImmutableList.of(addressGroup)).build();
channelBuilder.nameResolverFactory(nameResolverFactory);
Map<String, Object> rawServiceConfig = parseJson("{\"loadBalancingConfig\": [{\"mock_lb\": {\"check\": \"foo\"}}]}");
nameResolverFactory.nextRawServiceConfig.set(rawServiceConfig);
createChannel();
ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
ResolvedAddresses resolvedAddresses = resultCaptor.getValue();
assertThat(resolvedAddresses.getAddresses()).containsExactly(addressGroup);
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo("foo");
verify(mockLoadBalancer, never()).handleNameResolutionError(any(Status.class));
assertThat(channel.getState(false)).isNotEqualTo(ConnectivityState.TRANSIENT_FAILURE);
}
Aggregations