Skip to content

Commit 9c172e0

Browse files
committed
Fix unintended timeout in negative DNS lookup cache test
Motivation: DNS lookups in DnsNameResolverTest can take longer than expected due to retries. The hard limit of 5 seconds is being applied to testNegativeTtl(), making the first uncached lookup cause a timeout. Modifications: Do not use JUnit's Timeout annotation but implement simple timeout mechanism that apples only to cached lookups. Result: testNegativeTtl() should not fail when an initial negative lookup requires a retry.
1 parent 5b9e55f commit 9c172e0

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,19 +483,35 @@ public void testQueryMx() throws Exception {
483483
}
484484
}
485485

486-
@Test(timeout = 5000)
486+
@Test
487487
public void testNegativeTtl() throws Exception {
488488
final int oldNegativeTtl = resolver.negativeTtl();
489489
resolver.setNegativeTtl(10);
490490
try {
491491
resolveNonExistentDomain();
492492

493-
// If negative cache works, this loop should be done really quickly.
494-
final List<UnknownHostException> exceptions = new ArrayList<UnknownHostException>();
495493
final int size = 10000;
494+
final List<UnknownHostException> exceptions = new ArrayList<UnknownHostException>();
495+
496+
// If negative cache works, this thread should be done really quickly.
497+
final Thread negativeLookupThread = new Thread() {
498+
@Override
499+
public void run() {
500+
for (int i = 0; i < size; i++) {
501+
exceptions.add(resolveNonExistentDomain());
502+
if (isInterrupted()) {
503+
break;
504+
}
505+
}
506+
}
507+
};
508+
509+
negativeLookupThread.start();
510+
negativeLookupThread.join(5000);
496511

497-
for (int i = 0; i < size; i++) {
498-
exceptions.add(resolveNonExistentDomain());
512+
if (negativeLookupThread.isAlive()) {
513+
negativeLookupThread.interrupt();
514+
fail("Cached negative lookups did not finish quickly.");
499515
}
500516

501517
assertThat(exceptions, hasSize(size));

0 commit comments

Comments
 (0)