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());
}
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());
}
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);
}
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);
}
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());
}
Aggregations