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.
This commit is contained in:
Trustin Lee 2015-08-29 11:34:52 +09:00
parent d9d488e477
commit e1bf9d6257

View File

@ -483,19 +483,35 @@ public class DnsNameResolverTest {
}
}
@Test(timeout = 5000)
@Test
public void testNegativeTtl() throws Exception {
final int oldNegativeTtl = resolver.negativeTtl();
resolver.setNegativeTtl(10);
try {
resolveNonExistentDomain();
// If negative cache works, this loop should be done really quickly.
final List<UnknownHostException> exceptions = new ArrayList<UnknownHostException>();
final int size = 10000;
final List<UnknownHostException> exceptions = new ArrayList<UnknownHostException>();
// If negative cache works, this thread should be done really quickly.
final Thread negativeLookupThread = new Thread() {
@Override
public void run() {
for (int i = 0; i < size; i++) {
exceptions.add(resolveNonExistentDomain());
if (isInterrupted()) {
break;
}
}
}
};
negativeLookupThread.start();
negativeLookupThread.join(5000);
if (negativeLookupThread.isAlive()) {
negativeLookupThread.interrupt();
fail("Cached negative lookups did not finish quickly.");
}
assertThat(exceptions, hasSize(size));