DnsNameResolver extends InetNameResolver { @Override public void close() { if (ch.isOpen()) { ch.close(); } } DnsNameResolver(
            EventLoop eventLoop,
            ChannelFactory<? extends DatagramChannel> channelFactory,
            final DnsCache resolveCache,
            DnsCache authoritativeDnsServerCache,
            DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
            long queryTimeoutMillis,
            ResolvedAddressTypes resolvedAddressTypes,
            boolean recursionDesired,
            int maxQueriesPerResolve,
            boolean traceEnabled,
            int maxPayloadSize,
            boolean optResourceEnabled,
            HostsFileEntriesResolver hostsFileEntriesResolver,
            DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
            String[] searchDomains,
            int ndots,
            boolean decodeIdn); DnsCache resolveCache(); DnsCache authoritativeDnsServerCache(); long queryTimeoutMillis(); ResolvedAddressTypes resolvedAddressTypes(); boolean isRecursionDesired(); int maxQueriesPerResolve(); int maxPayloadSize(); boolean isOptResourceEnabled(); HostsFileEntriesResolver hostsFileEntriesResolver(); @Override void close(); final Future<InetAddress> resolve(String inetHost, Iterable<DnsRecord> additionals); final Future<InetAddress> resolve(String inetHost, Iterable<DnsRecord> additionals,
                                             Promise<InetAddress> promise); final Future<List<InetAddress>> resolveAll(String inetHost, Iterable<DnsRecord> additionals); final Future<List<InetAddress>> resolveAll(String inetHost, Iterable<DnsRecord> additionals,
                                                Promise<List<InetAddress>> promise); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(DnsQuestion question); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            DnsQuestion question, Iterable<DnsRecord> additionals); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            DnsQuestion question, Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            InetSocketAddress nameServerAddr, DnsQuestion question); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            InetSocketAddress nameServerAddr, DnsQuestion question, Iterable<DnsRecord> additionals); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            InetSocketAddress nameServerAddr, DnsQuestion question,
            Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise); Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
            InetSocketAddress nameServerAddr, DnsQuestion question,
            Iterable<DnsRecord> additionals,
            Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise); static boolean isTransportOrTimeoutError(Throwable cause); static boolean isTimeoutError(Throwable cause);  }
@Test public void testResolveAorAAAA() throws Exception { DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV4_PREFERRED).build(); try { testResolve0(resolver, EXCLUSIONS_RESOLVE_A, AAAA); } finally { resolver.close(); } }
@Test public void testResolveAAAAorA() throws Exception { DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV6_PREFERRED).build(); try { testResolve0(resolver, EXCLUSIONS_RESOLVE_A, A); } finally { resolver.close(); } }
@Test public void testNameServerCache() throws IOException, InterruptedException { final String overriddenIP = "12.34.12.34"; final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() { @Override public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException { ResourceRecordModifier rm = new ResourceRecordModifier(); rm.setDnsClass(RecordClass.IN); rm.setDnsName(question.getDomainName()); rm.setDnsTtl(100); rm.setDnsType(question.getRecordType()); switch (question.getRecordType()) { case A: rm.put(DnsAttribute.IP_ADDRESS, overriddenIP); break; default: return null; } return Collections.singleton(rm.getEntry()); } }); dnsServer2.start(); try { final Set<String> overridenHostnames = new HashSet<String>(); for (String name : DOMAINS) { if (EXCLUSIONS_RESOLVE_A.contains(name)) { continue; } if (PlatformDependent.threadLocalRandom().nextBoolean()) { overridenHostnames.add(name); } } DnsNameResolver resolver = newResolver(false, new DnsServerAddressStreamProvider() { @Override public DnsServerAddressStream nameServerAddressStream(String hostname) { return overridenHostnames.contains(hostname) ? sequential(dnsServer2.localAddress()).stream() : null; } }).build(); try { final Map<String, InetAddress> resultA = testResolve0(resolver, EXCLUSIONS_RESOLVE_A, AAAA); for (Entry<String, InetAddress> resolvedEntry : resultA.entrySet()) { if (resolvedEntry.getValue().isLoopbackAddress()) { continue; } if (overridenHostnames.contains(resolvedEntry.getKey())) { assertEquals("failed to resolve " + resolvedEntry.getKey(), overriddenIP, resolvedEntry.getValue().getHostAddress()); } else { assertNotEquals("failed to resolve " + resolvedEntry.getKey(), overriddenIP, resolvedEntry.getValue().getHostAddress()); } } } finally { resolver.close(); } } finally { dnsServer2.stop(); } }
@Test public void testResolveAAAA() throws Exception { DnsNameResolver resolver = newResolver(ResolvedAddressTypes.IPV6_ONLY).build(); try { testResolve0(resolver, EXCLUSIONS_RESOLVE_AAAA, null); } finally { resolver.close(); } }
@Test public void testNonCachedResolve() throws Exception { DnsNameResolver resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_ONLY).build(); try { testResolve0(resolver, EXCLUSIONS_RESOLVE_A, null); } finally { resolver.close(); } }
@Test public void testNegativeTtl() throws Exception { final DnsNameResolver resolver = newResolver().negativeTtl(10).build(); try { resolveNonExistentDomain(resolver); final int size = 10000; final List<UnknownHostException> exceptions = new ArrayList<UnknownHostException>(); final Thread negativeLookupThread = new Thread() { @Override public void run() { for (int i = 0; i < size; i++) { exceptions.add(resolveNonExistentDomain(resolver)); if (isInterrupted()) { break; } } } }; negativeLookupThread.start(); negativeLookupThread.join(DEFAULT_TEST_TIMEOUT_MS); if (negativeLookupThread.isAlive()) { negativeLookupThread.interrupt(); fail("Cached negative lookups did not finish quickly."); } assertThat(exceptions, hasSize(size)); } finally { resolver.close(); } }