Search in sources :

Example 26 with ResolvedAddresses

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();
}
Also used : Status(io.grpc.Status) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ArrayList(java.util.ArrayList) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 27 with ResolvedAddresses

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);
}
Also used : Status(io.grpc.Status) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 28 with ResolvedAddresses

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);
}
Also used : Status(io.grpc.Status) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 29 with ResolvedAddresses

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);
}
Also used : Status(io.grpc.Status) InternalConfigSelector(io.grpc.InternalConfigSelector) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 30 with ResolvedAddresses

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);
}
Also used : Status(io.grpc.Status) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Aggregations

ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)32 Test (org.junit.Test)31 Attributes (io.grpc.Attributes)16 Status (io.grpc.Status)12 InOrder (org.mockito.InOrder)10 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)9 Subchannel (io.grpc.LoadBalancer.Subchannel)9 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)9 Helper (io.grpc.LoadBalancer.Helper)8 SubchannelStateListener (io.grpc.LoadBalancer.SubchannelStateListener)8 LoadBalancer (io.grpc.LoadBalancer)4 ConfigOrError (io.grpc.NameResolver.ConfigOrError)3 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)3 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)3 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)3 InetSocketAddress (java.net.InetSocketAddress)3 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)2 InternalConfigSelector (io.grpc.InternalConfigSelector)2 ServingStatus (io.grpc.health.v1.HealthCheckResponse.ServingStatus)2 SocketAddress (java.net.SocketAddress)2