Search in sources :

Example 6 with AddressResolver

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

the class DnsNameResolverTest method resolve_usingCache.

@Test
public void resolve_usingCache() throws Exception {
    long ttl = 60;
    System.setProperty(DnsNameResolver.NETWORKADDRESS_CACHE_TTL_PROPERTY, Long.toString(ttl));
    final List<InetAddress> answer = 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(answer).thenThrow(new AssertionError("should not reach here."));
    resolver.setAddressResolver(mockResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    assertAnswerMatches(answer, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    // this refresh should return cached result
    fakeTicker.advance(ttl - 1, TimeUnit.SECONDS);
    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 7 with AddressResolver

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

the class DnsNameResolverTest method resolveNeverCache.

private void resolveNeverCache(boolean isAndroid) throws Exception {
    final List<InetAddress> answer1 = createAddressList(2);
    final List<InetAddress> answer2 = createAddressList(1);
    String name = "foo.googleapis.com";
    DnsNameResolver resolver = newResolver(name, 81, isAndroid);
    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());
    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) InetAddress(java.net.InetAddress)

Example 8 with AddressResolver

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

the class DnsNameResolverTest method testExecutor_custom.

@Test
public void testExecutor_custom() throws Exception {
    final List<InetAddress> answer = createAddressList(2);
    final AtomicInteger executions = new AtomicInteger();
    NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(81).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(mock(ServiceConfigParser.class)).setChannelLogger(mock(ChannelLogger.class)).setOffloadExecutor(new Executor() {

        @Override
        public void execute(Runnable command) {
            executions.incrementAndGet();
            command.run();
        }
    }).build();
    DnsNameResolver resolver = newResolver("foo.googleapis.com", Stopwatch.createUnstarted(), false, args);
    AddressResolver mockResolver = mock(AddressResolver.class);
    when(mockResolver.resolveAddress(anyString())).thenReturn(answer);
    resolver.setAddressResolver(mockResolver);
    resolver.start(mockListener);
    assertEquals(0, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    assertAnswerMatches(answer, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    resolver.shutdown();
    assertThat(fakeExecutorResource.createCount.get()).isEqualTo(0);
    assertThat(executions.get()).isEqualTo(1);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) Executor(java.util.concurrent.Executor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelLogger(io.grpc.ChannelLogger) InetAddress(java.net.InetAddress) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 9 with AddressResolver

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

the class DnsNameResolverTest method testExecutor_default.

@Test
public void testExecutor_default() throws Exception {
    final List<InetAddress> answer = createAddressList(2);
    DnsNameResolver resolver = newResolver("foo.googleapis.com", 81);
    AddressResolver mockResolver = mock(AddressResolver.class);
    when(mockResolver.resolveAddress(anyString())).thenReturn(answer);
    resolver.setAddressResolver(mockResolver);
    resolver.start(mockListener);
    assertEquals(1, fakeExecutor.runDueTasks());
    verify(mockListener).onResult(resultCaptor.capture());
    assertAnswerMatches(answer, 81, resultCaptor.getValue());
    assertEquals(0, fakeClock.numPendingTasks());
    resolver.shutdown();
    assertThat(fakeExecutorResource.createCount.get()).isEqualTo(1);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 10 with AddressResolver

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

the class DnsNameResolverTest method resolve_emptyResult.

@Test
public void resolve_emptyResult() throws Exception {
    DnsNameResolver.enableTxt = true;
    DnsNameResolver nr = newResolver("dns:///addr.fake:1234", 443);
    nr.setAddressResolver(new AddressResolver() {

        @Override
        public List<InetAddress> resolveAddress(String host) throws Exception {
            return Collections.emptyList();
        }
    });
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.<String>emptyList());
    nr.setResourceResolver(mockResourceResolver);
    nr.start(mockListener);
    assertThat(fakeExecutor.runDueTasks()).isEqualTo(1);
    ArgumentCaptor<ResolutionResult> ac = ArgumentCaptor.forClass(ResolutionResult.class);
    verify(mockListener).onResult(ac.capture());
    verifyNoMoreInteractions(mockListener);
    assertThat(ac.getValue().getAddresses()).isEmpty();
    assertThat(ac.getValue().getAttributes()).isEqualTo(Attributes.EMPTY);
    assertThat(ac.getValue().getServiceConfig()).isNull();
    verify(mockResourceResolver, never()).resolveSrv(anyString());
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) ResolutionResult(io.grpc.NameResolver.ResolutionResult) JndiResourceResolver(io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ExpectedException(org.junit.rules.ExpectedException) 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