use of io.grpc.internal.DnsNameResolver.ResourceResolver 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);
}
use of io.grpc.internal.DnsNameResolver.ResourceResolver 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");
}
use of io.grpc.internal.DnsNameResolver.ResourceResolver 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);
}
use of io.grpc.internal.DnsNameResolver.ResourceResolver 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());
}
use of io.grpc.internal.DnsNameResolver.ResourceResolver 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());
}
Aggregations