Search in sources :

Example 16 with AddressResolver

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

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

the class DnsNameResolverTest method doNotResolveWhenProxyDetected.

@Test
public void doNotResolveWhenProxyDetected() throws Exception {
    final String name = "foo.googleapis.com";
    final int port = 81;
    final InetSocketAddress proxyAddress = new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 1000);
    ProxyDetector alwaysDetectProxy = new ProxyDetector() {

        @Override
        public HttpConnectProxiedSocketAddress proxyFor(SocketAddress targetAddress) {
            return HttpConnectProxiedSocketAddress.newBuilder().setTargetAddress((InetSocketAddress) targetAddress).setProxyAddress(proxyAddress).setUsername("username").setPassword("password").build();
        }
    };
    DnsNameResolver resolver = newResolver(name, port, alwaysDetectProxy, Stopwatch.createUnstarted());
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new AssertionError());
    resolver.setAddressResolver(mockAddressResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    List<EquivalentAddressGroup> result = resultCaptor.getValue().getAddresses();
    assertThat(result).hasSize(1);
    EquivalentAddressGroup eag = result.get(0);
    assertThat(eag.getAddresses()).hasSize(1);
    HttpConnectProxiedSocketAddress socketAddress = (HttpConnectProxiedSocketAddress) eag.getAddresses().get(0);
    assertSame(proxyAddress, socketAddress.getProxyAddress());
    assertEquals("username", socketAddress.getUsername());
    assertEquals("password", socketAddress.getPassword());
    assertTrue(socketAddress.getTargetAddress().isUnresolved());
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ProxyDetector(io.grpc.ProxyDetector) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) HttpConnectProxiedSocketAddress(io.grpc.HttpConnectProxiedSocketAddress) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) HttpConnectProxiedSocketAddress(io.grpc.HttpConnectProxiedSocketAddress) Test(org.junit.Test)

Example 18 with AddressResolver

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

the class DnsNameResolverTest method resolve_nullResourceResolver_addressFailure.

@Test
public void resolve_nullResourceResolver_addressFailure() throws Exception {
    DnsNameResolver.enableTxt = true;
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new IOException("no addr"));
    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).onError(errorCaptor.capture());
    Status errorStatus = errorCaptor.getValue();
    assertThat(errorStatus.getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(errorStatus.getCause()).hasMessageThat().contains("no addr");
}
Also used : Status(io.grpc.Status) AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) IOException(java.io.IOException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 19 with AddressResolver

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

the class GrpclbNameResolverTest method resolve_addressFailure_stillLookUpBalancersAndServiceConfig.

@Test
public void resolve_addressFailure_stillLookUpBalancersAndServiceConfig() throws Exception {
    InetAddress lbAddr = InetAddress.getByAddress(new byte[] { 10, 1, 0, 0 });
    int lbPort = 8080;
    // original name in SRV record
    String lbName = "foo.example.com.";
    SrvRecord srvRecord = new SrvRecord(lbName, 8080);
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(hostName)).thenThrow(new UnknownHostException("I really tried"));
    when(mockAddressResolver.resolveAddress(lbName)).thenReturn(Collections.singletonList(lbAddr));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.<String>emptyList());
    when(mockResourceResolver.resolveSrv(anyString())).thenReturn(Collections.singletonList(srvRecord));
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertThat(fakeClock.runDueTasks()).isEqualTo(1);
    verify(mockListener).onResult(resultCaptor.capture());
    ResolutionResult result = resultCaptor.getValue();
    assertThat(result.getAddresses()).isEmpty();
    EquivalentAddressGroup resolvedBalancerAddr = Iterables.getOnlyElement(result.getAttributes().get(GrpclbConstants.ATTR_LB_ADDRS));
    assertThat(resolvedBalancerAddr.getAttributes().get(GrpclbConstants.ATTR_LB_ADDR_AUTHORITY)).isEqualTo("foo.example.com");
    InetSocketAddress resolvedBalancerSockAddr = (InetSocketAddress) Iterables.getOnlyElement(resolvedBalancerAddr.getAddresses());
    assertThat(resolvedBalancerSockAddr.getAddress()).isEqualTo(lbAddr);
    assertThat(resolvedBalancerSockAddr.getPort()).isEqualTo(lbPort);
    assertThat(result.getServiceConfig()).isNull();
    verify(mockAddressResolver).resolveAddress(hostName);
    verify(mockResourceResolver).resolveTxt("_grpc_config." + hostName);
    verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) UnknownHostException(java.net.UnknownHostException) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) SrvRecord(io.grpc.internal.DnsNameResolver.SrvRecord) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 20 with AddressResolver

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

the class GrpclbNameResolverTest method resolveAll_balancerLookupFails_stillLookUpServiceConfig.

@Test
public void resolveAll_balancerLookupFails_stillLookUpServiceConfig() throws Exception {
    InetAddress backendAddr = InetAddress.getByAddress(new byte[] { 127, 0, 0, 0 });
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(hostName)).thenReturn(Collections.singletonList(backendAddr));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.<String>emptyList());
    when(mockResourceResolver.resolveSrv(anyString())).thenThrow(new Exception("something like javax.naming.NamingException"));
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertThat(fakeClock.runDueTasks()).isEqualTo(1);
    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.getAttributes().get(GrpclbConstants.ATTR_LB_ADDRS)).isNull();
    verify(mockAddressResolver).resolveAddress(hostName);
    verify(mockResourceResolver).resolveTxt("_grpc_config." + hostName);
    verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) InetAddress(java.net.InetAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) 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