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