Search in sources :

Example 1 with AddressResolver

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

the class DnsNameResolverTest method resolveDefaultValue.

private void resolveDefaultValue() throws Exception {
    final List<InetAddress> answer1 = createAddressList(2);
    final List<InetAddress> answer2 = createAddressList(1);
    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).thenReturn(answer2);
    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(DnsNameResolver.DEFAULT_NETWORK_CACHE_TTL_SECONDS, TimeUnit.SECONDS);
    resolver.refresh();
    assertEquals(0, fakeExecutor.runDueTasks());
    assertEquals(0, fakeClock.numPendingTasks());
    verifyNoMoreInteractions(mockListener);
    fakeTicker.advance(1, TimeUnit.SECONDS);
    resolver.refresh();
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener, times(2)).onResult(resultCaptor.capture());
    assertAnswerMatches(answer2, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    resolver.shutdown();
    verify(mockResolver, times(2)).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)

Example 2 with AddressResolver

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

the class DnsNameResolverTest method resolve_cacheExpired.

@Test
public void resolve_cacheExpired() throws Exception {
    long ttl = 60;
    System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl));
    final List<InetAddress> answer1 = createAddressList(2);
    final List<InetAddress> answer2 = createAddressList(1);
    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).thenReturn(answer2);
    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(ttl + 1, TimeUnit.SECONDS);
    resolver.refresh();
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener, times(2)).onResult(resultCaptor.capture());
    assertAnswerMatches(answer2, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    resolver.shutdown();
    verify(mockResolver, times(2)).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 3 with AddressResolver

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

the class GrpclbNameResolverTest method resolve_presentResourceResolver.

@Test
public void resolve_presentResourceResolver() throws Exception {
    InetAddress backendAddr = InetAddress.getByAddress(new byte[] { 127, 0, 0, 0 });
    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)).thenReturn(Collections.singletonList(backendAddr));
    when(mockAddressResolver.resolveAddress(lbName)).thenReturn(Collections.singletonList(lbAddr));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=[{\"clientLanguage\": [\"java\"], \"serviceConfig\": {}}]"));
    when(mockResourceResolver.resolveSrv(anyString())).thenReturn(Collections.singletonList(srvRecord));
    when(serviceConfigParser.parseServiceConfig(ArgumentMatchers.<String, Object>anyMap())).thenAnswer(new Answer<ConfigOrError>() {

        @Override
        public ConfigOrError answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            return ConfigOrError.fromConfig(args[0]);
        }
    });
    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);
    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().getConfig()).isNotNull();
    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) SrvRecord(io.grpc.internal.DnsNameResolver.SrvRecord) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InvocationOnMock(org.mockito.invocation.InvocationOnMock) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) ConfigOrError(io.grpc.NameResolver.ConfigOrError) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with AddressResolver

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

the class GrpclbNameResolverTest method resolve_nullResourceResolver_addressFailure.

@Test
public void resolve_nullResourceResolver_addressFailure() throws Exception {
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new IOException("no addr"));
    ResourceResolver resourceResolver = null;
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(resourceResolver);
    resolver.start(mockListener);
    assertThat(fakeClock.runDueTasks()).isEqualTo(1);
    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) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with AddressResolver

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

the class GrpclbNameResolverTest method resolve_addressAndBalancersLookupFail_neverLookupServiceConfig.

@Test
public void resolve_addressAndBalancersLookupFail_neverLookupServiceConfig() throws Exception {
    AddressResolver mockAddressResolver = mock(AddressResolver.class);
    when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new UnknownHostException("I really tried"));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    lenient().when(mockResourceResolver.resolveTxt(anyString())).thenThrow(new Exception("something like javax.naming.NamingException"));
    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).onError(errorCaptor.capture());
    Status errorStatus = errorCaptor.getValue();
    assertThat(errorStatus.getCode()).isEqualTo(Code.UNAVAILABLE);
    verify(mockAddressResolver).resolveAddress(hostName);
    verify(mockResourceResolver, never()).resolveTxt("_grpc_config." + hostName);
    verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName);
}
Also used : Status(io.grpc.Status) AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) UnknownHostException(java.net.UnknownHostException) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) 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