Search in sources :

Example 11 with AddressResolver

use of io.grpc.internal.DnsNameResolver.AddressResolver in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_cacheForever.

@Test
public void resolve_cacheForever() throws Exception {
    System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, "-1");
    final List<InetAddress> answer1 = createAddressList(2);
    String name = "foo.googleapis.com";
    FakeTicker fakeTicker = new FakeTicker();
    DnsNameResolver resolver = newResolver(name, 81, GrpcUtil.NOOP_PROXY_DETECTOR, Stopwatch.createUnstarted(fakeTicker));
    AddressResolver mockResolver = mock(AddressResolver.class);
    when(mockResolver.resolveAddress(anyString())).thenReturn(answer1).thenThrow(new AssertionError("should not called twice"));
    resolver.setAddressResolver(mockResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    assertAnswerMatches(answer1, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    fakeTicker.advance(1, TimeUnit.DAYS);
    resolver.refresh();
    assertEquals(0, fakeExecutor.runDueTasks());
    assertEquals(0, fakeClock.numPendingTasks());
    verifyNoMoreInteractions(mockListener);
    resolver.shutdown();
    verify(mockResolver).resolveAddress(anyString());
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FakeTicker(com.google.common.testing.FakeTicker) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 12 with AddressResolver

use of io.grpc.internal.DnsNameResolver.AddressResolver in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_serviceConfigLookupFails_nullServiceConfig.

@Test
public void resolve_serviceConfigLookupFails_nullServiceConfig() 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())).thenThrow(new Exception("something like javax.naming.NamingException"));
    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()).isNull();
    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) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Example 13 with AddressResolver

use of io.grpc.internal.DnsNameResolver.AddressResolver 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 14 with AddressResolver

use of io.grpc.internal.DnsNameResolver.AddressResolver in project grpc-java by grpc.

the class DnsNameResolverTest method resolve_addressFailure_neverLookUpServiceConfig.

@Test
public void resolve_addressFailure_neverLookUpServiceConfig() throws Exception {
    DnsNameResolver.enableTxt = true;
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new IOException("no addr"));
    String name = "foo.googleapis.com";
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    DnsNameResolver resolver = newResolver(name, 81);
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onError(errorCaptor.capture());
    Status errorStatus = errorCaptor.getValue();
    assertThat(errorStatus.getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(errorStatus.getCause()).hasMessageThat().contains("no addr");
    verify(mockResourceResolver, never()).resolveTxt(anyString());
}
Also used : Status(io.grpc.Status) AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) JndiResourceResolver(io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) IOException(java.io.IOException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 15 with AddressResolver

use of io.grpc.internal.DnsNameResolver.AddressResolver 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)

Aggregations

AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)22 Test (org.junit.Test)20 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 InetAddress (java.net.InetAddress)15 ResourceResolver (io.grpc.internal.DnsNameResolver.ResourceResolver)12 ResolutionResult (io.grpc.NameResolver.ResolutionResult)10 InetSocketAddress (java.net.InetSocketAddress)9 IOException (java.io.IOException)8 UnknownHostException (java.net.UnknownHostException)6 JndiResourceResolver (io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver)5 FakeTicker (com.google.common.testing.FakeTicker)4 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)4 Status (io.grpc.Status)4 NameResolver (io.grpc.NameResolver)2 SrvRecord (io.grpc.internal.DnsNameResolver.SrvRecord)2 List (java.util.List)2 ExpectedException (org.junit.rules.ExpectedException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ChannelLogger (io.grpc.ChannelLogger)1 HttpConnectProxiedSocketAddress (io.grpc.HttpConnectProxiedSocketAddress)1