Search in sources :

Example 1 with SrvRecord

use of io.grpc.internal.DnsNameResolver.SrvRecord 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 2 with SrvRecord

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

the class JndiResourceResolverTest method srvRecordLookup.

@SuppressWarnings("deprecation")
@Test
public void srvRecordLookup() throws Exception {
    RecordFetcher recordFetcher = mock(RecordFetcher.class);
    when(recordFetcher.getAllRecords("SRV", "dns:///service.example.com")).thenReturn(Arrays.asList("0 0 314 foo.example.com.", "0 0 42 bar.example.com.", "0 0 1 discard.example.com"));
    List<SrvRecord> golden = Arrays.asList(new SrvRecord("foo.example.com.", 314), new SrvRecord("bar.example.com.", 42));
    JndiResourceResolver resolver = new JndiResourceResolver(recordFetcher);
    assertThat(resolver.resolveSrv("service.example.com")).isEqualTo(golden);
}
Also used : JndiRecordFetcher(io.grpc.internal.JndiResourceResolverFactory.JndiRecordFetcher) RecordFetcher(io.grpc.internal.JndiResourceResolverFactory.RecordFetcher) SrvRecord(io.grpc.internal.DnsNameResolver.SrvRecord) JndiResourceResolver(io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver) Test(org.junit.Test)

Example 3 with SrvRecord

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

the class GrpclbNameResolverTest method resolve_addressFailure_stillLookUpBalancersAndServiceConfig.

@Test
public void resolve_addressFailure_stillLookUpBalancersAndServiceConfig() throws Exception {
    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)).thenThrow(new UnknownHostException("I really tried"));
    when(mockAddressResolver.resolveAddress(lbName)).thenReturn(Collections.singletonList(lbAddr));
    ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
    when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.<String>emptyList());
    when(mockResourceResolver.resolveSrv(anyString())).thenReturn(Collections.singletonList(srvRecord));
    resolver.setAddressResolver(mockAddressResolver);
    resolver.setResourceResolver(mockResourceResolver);
    resolver.start(mockListener);
    assertThat(fakeClock.runDueTasks()).isEqualTo(1);
    verify(mockListener).onResult(resultCaptor.capture());
    ResolutionResult result = resultCaptor.getValue();
    assertThat(result.getAddresses()).isEmpty();
    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()).isNull();
    verify(mockAddressResolver).resolveAddress(hostName);
    verify(mockResourceResolver).resolveTxt("_grpc_config." + hostName);
    verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName);
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) UnknownHostException(java.net.UnknownHostException) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) ResolutionResult(io.grpc.NameResolver.ResolutionResult) SrvRecord(io.grpc.internal.DnsNameResolver.SrvRecord) ResourceResolver(io.grpc.internal.DnsNameResolver.ResourceResolver) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

SrvRecord (io.grpc.internal.DnsNameResolver.SrvRecord)3 Test (org.junit.Test)3 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)2 ResolutionResult (io.grpc.NameResolver.ResolutionResult)2 AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)2 ResourceResolver (io.grpc.internal.DnsNameResolver.ResourceResolver)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ConfigOrError (io.grpc.NameResolver.ConfigOrError)1 JndiRecordFetcher (io.grpc.internal.JndiResourceResolverFactory.JndiRecordFetcher)1 JndiResourceResolver (io.grpc.internal.JndiResourceResolverFactory.JndiResourceResolver)1 RecordFetcher (io.grpc.internal.JndiResourceResolverFactory.RecordFetcher)1 UnknownHostException (java.net.UnknownHostException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1