Skip to content

Commit f2393e3

Browse files
committed
! improve/refactor unit test case of setDnsCachePolicy/setNegativeDnsCachePolicy
1 parent 17e642e commit f2393e3

File tree

1 file changed

+93
-44
lines changed

1 file changed

+93
-44
lines changed

library/src/test/java/com/alibaba/dcm/DnsCacheManipulatorTest.java

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
import static com.alibaba.dcm.internal.TestTimeUtil.NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS;
1313
import static java.lang.System.currentTimeMillis;
1414
import static java.lang.Thread.sleep;
15-
import static org.junit.Assert.assertArrayEquals;
16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertNotSame;
18-
import static org.junit.Assert.assertNull;
19-
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
15+
import static org.junit.Assert.*;
2116

2217
/**
2318
* @author Jerry Lee (oldratlee at gmail dot com)
@@ -111,8 +106,17 @@ public void test_setDnsCache_getAllDnsCache() {
111106

112107
final DnsCacheEntry expected = new DnsCacheEntry(
113108
host.toLowerCase(), new String[]{IP3}, new Date(Long.MAX_VALUE));
109+
DnsCacheEntry actual = allDnsCacheEntries.get(0);
114110

115-
assertEqualsIgnoreHostCase(expected, allDnsCacheEntries.get(0));
111+
assertEqualsIgnoreHostCase(expected, actual);
112+
113+
DnsCacheEntry another = DnsCacheManipulator.getDnsCache(host);
114+
DnsCacheEntry another2 = DnsCacheManipulator.getDnsCache(host);
115+
// instance equals but NOT same
116+
assertEquals(actual, another);
117+
assertNotSame(actual, another);
118+
assertEquals(another, another2);
119+
assertNotSame(another, another2);
116120

117121
// Check NegativeCache
118122
assertTrue(DnsCacheManipulator.getWholeDnsCache().getNegativeCache().isEmpty());
@@ -231,76 +235,121 @@ public void test_nullSafeForGetDnsCache() {
231235
@Test
232236
public void test_setDnsCachePolicy() throws Exception {
233237
final String host = "bing.com";
238+
String host2 = "www.bing.com";
239+
// trigger dns cache by lookup and clear, skip OS lookup time after,
240+
// otherwise the lookup operation time may take seconds.
241+
//
242+
// so reduce the lookup operation time,
243+
// make below time-tracking test code more stability
244+
skipOSLookupTimeAfterThenClear(host, host2);
245+
234246
DnsCacheManipulator.setDnsCachePolicy(2);
235247
assertEquals(2, DnsCacheManipulator.getDnsCachePolicy());
236248

249+
//////////////////////////////////////////////////
250+
// 0. trigger dns cache by lookup
251+
//////////////////////////////////////////////////
237252
getIpByName(host);
238253
final long tick = currentTimeMillis();
254+
DnsCacheEntry dnsCacheEntry = DnsCacheManipulator.getDnsCache(host);
255+
assertBetween(dnsCacheEntry.getExpiration().getTime(),
256+
tick, tick + 2020);
239257

258+
//////////////////////////////////////////////////
259+
// 1. lookup before expire
260+
//////////////////////////////////////////////////
240261
sleep(1000);
241262
getIpByName(host);
263+
// get dns cache before expire
264+
assertEquals(dnsCacheEntry, DnsCacheManipulator.getDnsCache(host));
242265

243-
final DnsCacheEntry dnsCache = DnsCacheManipulator.getDnsCache(host);
244-
assertBetween(dnsCache.getExpiration().getTime(), tick, tick + 2020);
245-
266+
//////////////////////////////////////////////////
267+
// 2. get dns cache after expire
268+
//////////////////////////////////////////////////
246269
sleep(1020);
247-
248270
// return expired entry, because of no dns cache touch by external related operation!
249-
final DnsCacheEntry next = DnsCacheManipulator.getDnsCache(host);
250-
assertNotSame(dnsCache, next);
251-
assertEquals(dnsCache, next);
271+
assertEquals(dnsCacheEntry, DnsCacheManipulator.getDnsCache(host));
252272

253-
// touch dns cache with external other host operation
254-
getIpByName("www.bing.com");
273+
//////////////////////////////////////////////////
274+
// 3. touch dns cache with external other host operation
275+
//////////////////////////////////////////////////
276+
getIpByName(host2);
255277
assertNull(DnsCacheManipulator.getDnsCache(host));
256278

257-
// relookup
279+
//////////////////////////////////////////////////
280+
// 4. relookup
281+
//////////////////////////////////////////////////
258282
getIpByName(host);
259-
final DnsCacheEntry relookup = DnsCacheManipulator.getDnsCache(host);
260283
final long relookupTick = currentTimeMillis();
261-
assertBetween(relookup.getExpiration().getTime(), relookupTick, relookupTick + 2020);
284+
// get dns cache after expire
285+
final DnsCacheEntry relookup = DnsCacheManipulator.getDnsCache(host);
286+
assertBetween(relookup.getExpiration().getTime(),
287+
relookupTick, relookupTick + 2020);
288+
}
289+
290+
static void skipOSLookupTimeAfterThenClear(String... domains) throws UnknownHostException {
291+
for (String domain : domains) {
292+
// trigger dns cache by lookup and clear, skip OS lookup time after
293+
getIpByName(domain);
294+
}
295+
DnsCacheManipulator.clearDnsCache();
262296
}
263297

264298
@Test
265299
public void test_setNegativeDnsCachePolicy() throws Exception {
266300
DnsCacheManipulator.setDnsNegativeCachePolicy(2);
267301
assertEquals(2, DnsCacheManipulator.getDnsNegativeCachePolicy());
268302

269-
try {
270-
getIpByName(DOMAIN_NOT_EXISTED);
271-
fail();
272-
} catch (UnknownHostException expected) {
273-
assertTrue(true);
274-
}
303+
//////////////////////////////////////////////////
304+
// 0. trigger dns cache by lookup
305+
//////////////////////////////////////////////////
306+
lookupNotExisted(DOMAIN_NOT_EXISTED);
275307
final long tick = currentTimeMillis();
308+
assertOnlyNegativeCache(tick, tick + 2020);
276309

277-
final List<DnsCacheEntry> negativeCache = DnsCacheManipulator.getWholeDnsCache().getNegativeCache();
278-
assertEquals(1, negativeCache.size());
279-
final DnsCacheEntry dnsCache = negativeCache.get(0);
280-
assertBetween(dnsCache.getExpiration().getTime(), tick, tick + 2020);
281-
310+
//////////////////////////////////////////////////
311+
// 1. lookup before expire
312+
//////////////////////////////////////////////////
282313
sleep(1000);
283-
try {
284-
getIpByName(DOMAIN_NOT_EXISTED);
285-
fail();
286-
} catch (UnknownHostException expected) {
287-
assertTrue(true);
288-
}
289-
assertEquals(dnsCache, DnsCacheManipulator.getWholeDnsCache().getNegativeCache().get(0));
314+
lookupNotExisted(DOMAIN_NOT_EXISTED);
315+
// get dns cache before expire
316+
assertOnlyNegativeCache(tick, tick + 2020);
290317

291-
sleep(1001);
318+
//////////////////////////////////////////////////
319+
// 2. get dns cache after expire
320+
//////////////////////////////////////////////////
321+
sleep(1020);
322+
// get dns cache before expire
323+
assertOnlyNegativeCache(tick, tick + 2020);
324+
325+
//////////////////////////////////////////////////
326+
// 3. touch dns cache with external other host operation
327+
//////////////////////////////////////////////////
328+
getIpByName("bing.com");
329+
assertEquals(0, DnsCacheManipulator.getWholeDnsCache().getNegativeCache().size());
330+
331+
//////////////////////////////////////////////////
332+
// 4. relookup
333+
//////////////////////////////////////////////////
334+
lookupNotExisted(DOMAIN_NOT_EXISTED);
335+
final long relookupTick = currentTimeMillis();
336+
assertOnlyNegativeCache(relookupTick, relookupTick + 2020);
337+
}
338+
339+
static void lookupNotExisted(String domain) {
292340
try {
293-
getIpByName(DOMAIN_NOT_EXISTED);
341+
getIpByName(domain);
294342
fail();
295343
} catch (UnknownHostException expected) {
296344
assertTrue(true);
297345
}
346+
}
298347

299-
final long relookupTick = currentTimeMillis();
300-
final List<DnsCacheEntry> relookupNegativeCache = DnsCacheManipulator.getWholeDnsCache().getNegativeCache();
301-
assertEquals(1, relookupNegativeCache.size());
302-
final DnsCacheEntry relookup = relookupNegativeCache.get(0);
303-
assertBetween(relookup.getExpiration().getTime(), relookupTick, relookupTick + 2020);
348+
static void assertOnlyNegativeCache(long start, long end) {
349+
final List<DnsCacheEntry> negativeCache = DnsCacheManipulator.getWholeDnsCache().getNegativeCache();
350+
assertEquals(1, negativeCache.size());
351+
final DnsCacheEntry first = negativeCache.get(0);
352+
assertBetween(first.getExpiration().getTime(), start, end);
304353
}
305354

306355
static void assertEqualsIgnoreHostCase(DnsCacheEntry expected, DnsCacheEntry actual) {

0 commit comments

Comments
 (0)