Fix a bug of DnsNameResolver while working with NoopDnsCache.

Motivation:

If DnsNameResolver works with NoopDnsCache, IndexOutOfBoundsException will
be thrown.

Modifications:

Test if the result of DnsNameResolver.get(hostname) is empty before
accessing it's elements.
This commit is contained in:
joymufeng 2016-06-26 17:16:12 +08:00 committed by Norman Maurer
parent f0a5ee068f
commit 2562ef7cbe
2 changed files with 18 additions and 2 deletions

View File

@ -337,7 +337,7 @@ public class DnsNameResolver extends InetNameResolver {
Promise<InetAddress> promise,
DnsCache resolveCache) {
final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
if (cachedEntries == null) {
if (cachedEntries == null || cachedEntries.isEmpty()) {
return false;
}
@ -443,7 +443,7 @@ public class DnsNameResolver extends InetNameResolver {
Promise<List<InetAddress>> promise,
DnsCache resolveCache) {
final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
if (cachedEntries == null) {
if (cachedEntries == null || cachedEntries.isEmpty()) {
return false;
}

View File

@ -278,6 +278,12 @@ public class DnsNameResolverTest {
.resolvedAddressTypes(resolvedAddressTypes);
}
private static DnsNameResolverBuilder newNonCachedResolver(InternetProtocolFamily... resolvedAddressTypes) {
return newResolver()
.resolveCache(NoopDnsCache.INSTANCE)
.resolvedAddressTypes(resolvedAddressTypes);
}
@BeforeClass
public static void init() throws Exception {
dnsServer.start();
@ -349,6 +355,16 @@ public class DnsNameResolverTest {
}
}
@Test
public void testNonCachedResolve() throws Exception {
DnsNameResolver resolver = newNonCachedResolver(InternetProtocolFamily.IPv4).build();
try {
testResolve0(resolver, EXCLUSIONS_RESOLVE_A);
} finally {
resolver.close();
}
}
private static Map<String, InetAddress> testResolve0(DnsNameResolver resolver, Set<String> excludedDomains)
throws InterruptedException {