Search in sources :

Example 1 with ServiceConfigParser

use of io.grpc.NameResolver.ServiceConfigParser 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 2 with ServiceConfigParser

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

the class XdsNameResolverTest method retryPolicyInPerMethodConfigGeneratedByResolverIsValid.

@Test
public void retryPolicyInPerMethodConfigGeneratedByResolverIsValid() {
    ServiceConfigParser realParser = new ScParser(true, 5, 5, new AutoConfiguredLoadBalancerFactory("pick-first"));
    resolver = new XdsNameResolver(null, AUTHORITY, realParser, syncContext, scheduler, xdsClientPoolFactory, mockRandom, FilterRegistry.getDefaultRegistry(), null);
    resolver.start(mockListener);
    FakeXdsClient xdsClient = (FakeXdsClient) resolver.getXdsClient();
    RetryPolicy retryPolicy = RetryPolicy.create(4, ImmutableList.of(Code.UNAVAILABLE), Durations.fromMillis(100), Durations.fromMillis(200), null);
    xdsClient.deliverLdsUpdate(Collections.singletonList(Route.forAction(RouteMatch.withPathExactOnly(call1.getFullMethodNameForPath()), RouteAction.forCluster(cluster1, Collections.<HashPolicy>emptyList(), null, retryPolicy), ImmutableMap.<String, FilterConfig>of())));
    verify(mockListener).onResult(resolutionResultCaptor.capture());
    ResolutionResult result = resolutionResultCaptor.getValue();
    InternalConfigSelector configSelector = result.getAttributes().get(InternalConfigSelector.KEY);
    Result selectResult = configSelector.selectConfig(new PickSubchannelArgsImpl(call1.methodDescriptor, new Metadata(), CallOptions.DEFAULT));
    Object config = selectResult.getConfig();
    // Purely validating the data (io.grpc.internal.RetryPolicy).
    // However, there's no public accessor methods the data object.
    assertThat(config.getClass().getName()).isEqualTo("io.grpc.internal.ManagedChannelServiceConfig");
    assertThat(config.toString()).contains(MoreObjects.toStringHelper("RetryPolicy").add("maxAttempts", 4).add("initialBackoffNanos", TimeUnit.MILLISECONDS.toNanos(100)).add("maxBackoffNanos", TimeUnit.MILLISECONDS.toNanos(200)).add("backoffMultiplier", 2D).add("perAttemptRecvTimeoutNanos", null).add("retryableStatusCodes", ImmutableList.of(Code.UNAVAILABLE)).toString());
}
Also used : ResolutionResult(io.grpc.NameResolver.ResolutionResult) Metadata(io.grpc.Metadata) ResolutionResult(io.grpc.NameResolver.ResolutionResult) Result(io.grpc.InternalConfigSelector.Result) AutoConfiguredLoadBalancerFactory(io.grpc.internal.AutoConfiguredLoadBalancerFactory) PickSubchannelArgsImpl(io.grpc.internal.PickSubchannelArgsImpl) ScParser(io.grpc.internal.ScParser) InternalConfigSelector(io.grpc.InternalConfigSelector) HashPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy) ServiceConfigParser(io.grpc.NameResolver.ServiceConfigParser) RetryPolicy(io.grpc.xds.VirtualHost.Route.RouteAction.RetryPolicy) Test(org.junit.Test)

Aggregations

ResolutionResult (io.grpc.NameResolver.ResolutionResult)2 ServiceConfigParser (io.grpc.NameResolver.ServiceConfigParser)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 InternalConfigSelector (io.grpc.InternalConfigSelector)1 Result (io.grpc.InternalConfigSelector.Result)1 Metadata (io.grpc.Metadata)1 NameResolver (io.grpc.NameResolver)1 AutoConfiguredLoadBalancerFactory (io.grpc.internal.AutoConfiguredLoadBalancerFactory)1 AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)1 ResourceResolver (io.grpc.internal.DnsNameResolver.ResourceResolver)1 JndiResourceResolver (io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver)1 PickSubchannelArgsImpl (io.grpc.internal.PickSubchannelArgsImpl)1 ScParser (io.grpc.internal.ScParser)1 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)1 RetryPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.RetryPolicy)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1