Search in sources :

Example 16 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class ManagedChannelImplTest method channelTracing_serviceConfigChange.

@Test
public void channelTracing_serviceConfigChange() throws Exception {
    timer.forwardNanos(1234);
    channelBuilder.maxTraceEvents(10);
    List<EquivalentAddressGroup> servers = new ArrayList<>();
    servers.add(new EquivalentAddressGroup(socketAddress));
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(servers).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    createChannel();
    int prevSize = getStats(channel).channelTrace.events.size();
    ManagedChannelServiceConfig mcsc1 = createManagedChannelServiceConfig(ImmutableMap.<String, Object>of(), new PolicySelection(mockLoadBalancerProvider, null));
    ResolutionResult resolutionResult1 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult1);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
    assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
    prevSize = getStats(channel).channelTrace.events.size();
    ResolutionResult resolutionResult2 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult2);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize);
    prevSize = getStats(channel).channelTrace.events.size();
    timer.forwardNanos(1234);
    ResolutionResult resolutionResult3 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(ManagedChannelServiceConfig.empty())).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult3);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
    assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) ResolutionResult(io.grpc.NameResolver.ResolutionResult) ArrayList(java.util.ArrayList) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 17 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_presentResourceResolver.

@Test
public void resolve_presentResourceResolver() throws Exception {
    DnsNameResolver.enableTxt = true;
    InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=[{\"clientLanguage\": [\"java\"], \"serviceConfig\": {}}]"));
    ServiceConfigParser serviceConfigParser = new ServiceConfigParser() {

        @Override
        public ConfigOrError parseServiceConfig(Map<String, ?> rawServiceConfig) {
            return ConfigOrError.fromConfig(rawServiceConfig);
        }
    };
    NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(DEFAULT_PORT).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(serviceConfigParser).build();
    String name = "foo.googleapis.com";
    DnsNameResolver resolver = newResolver(name, Stopwatch.createUnstarted(), false, args);
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    ResolutionResult result = resultCaptor.getValue();
    InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
    assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
    assertThat(result.getServiceConfig().getConfig()).isNotNull();
    verify(mockAddressResolver).resolveAddress(name);
    verify(mockResourceResolver).resolveTxt("_grpc_config." + name);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) JndiResourceResolver(io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceConfigParser(io.grpc.NameResolver.ServiceConfigParser) InetAddress(java.net.InetAddress) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 18 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_nullResourceResolver.

@Test
public void resolve_nullResourceResolver() throws Exception {
    DnsNameResolver.enableTxt = true;
    InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
    String name = "foo.googleapis.com";
    DnsNameResolver resolver = newResolver(name, 81);
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(null);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    ResolutionResult result = resultCaptor.getValue();
    InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
    assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
    verify(mockAddressResolver).resolveAddress(name);
    assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
    assertThat(result.getServiceConfig()).isNull();
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 19 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_serviceConfigMalformed_serviceConfigError.

@Test
public void resolve_serviceConfigMalformed_serviceConfigError() throws Exception {
    DnsNameResolver.enableTxt = true;
    InetAddress backendAddr = InetAddresses.fromInteger(0x7f000001);
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenReturn(Collections.singletonList(backendAddr));
    String name = "foo.googleapis.com";
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=something invalid"));
    DnsNameResolver resolver = newResolver(name, 81);
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    ResolutionResult result = resultCaptor.getValue();
    InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
    assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
    verify(mockAddressResolver).resolveAddress(name);
    assertThat(result.getAttributes()).isEqualTo(Attributes.EMPTY);
    assertThat(result.getServiceConfig()).isNotNull();
    assertThat(result.getServiceConfig().getError()).isNotNull();
    verify(mockResourceResolver).resolveTxt(anyString());
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) JndiResourceResolver(io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 20 with ResolutionResult

use of io.grpc.NameResolver.ResolutionResult in project grpc-java by grpc.

the class XdsNameResolverTest method resolved_noTimeout.

@Test
public void resolved_noTimeout() {
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    Route route = Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forCluster(cluster1, Collections.<HashPolicy>emptyList(), null, // per-route timeout unset
    null), ImmutableMap.<String, FilterConfig>of());
    VirtualHost virtualHost = VirtualHost.create("does not matter", Collections.singletonList(AUTHORITY), Collections.singletonList(route), ImmutableMap.<String, FilterConfig>of());
    xdsClient.deliverLdsUpdate(0L, Collections.singletonList(virtualHost));
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    assertCallSelectClusterResult(call1, configSelector, cluster1, null);
}
Also used : InternalConfigSelector(io.grpc.InternalConfigSelector) HashPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Route(io.grpc.xds.VirtualHost.Route) Test(org.junit.Test)

Aggregations

ResolutionResult (io.grpc.NameResolver.ResolutionResult)32 Test (org.junit.Test)29 InternalConfigSelector (io.grpc.InternalConfigSelector)16 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)11 AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)10 ResourceResolver (io.grpc.internal.DnsNameResolver.ResourceResolver)9 InetAddress (java.net.InetAddress)8 InetSocketAddress (java.net.InetSocketAddress)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 ClientCall (io.grpc.ClientCall)7 NoopClientCall (io.grpc.internal.NoopClientCall)7 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)5 UnknownHostException (java.net.UnknownHostException)5 JndiResourceResolver (io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver)4 IOException (java.io.IOException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Route (io.grpc.xds.VirtualHost.Route)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Result (io.grpc.InternalConfigSelector.Result)2