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