|
12 | 12 | import static com.alibaba.dcm.internal.TestTimeUtil.NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS; |
13 | 13 | import static java.lang.System.currentTimeMillis; |
14 | 14 | 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.*; |
21 | 16 |
|
22 | 17 | /** |
23 | 18 | * @author Jerry Lee (oldratlee at gmail dot com) |
@@ -111,8 +106,17 @@ public void test_setDnsCache_getAllDnsCache() { |
111 | 106 |
|
112 | 107 | final DnsCacheEntry expected = new DnsCacheEntry( |
113 | 108 | host.toLowerCase(), new String[]{IP3}, new Date(Long.MAX_VALUE)); |
| 109 | + DnsCacheEntry actual = allDnsCacheEntries.get(0); |
114 | 110 |
|
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); |
116 | 120 |
|
117 | 121 | // Check NegativeCache |
118 | 122 | assertTrue(DnsCacheManipulator.getWholeDnsCache().getNegativeCache().isEmpty()); |
@@ -231,76 +235,121 @@ public void test_nullSafeForGetDnsCache() { |
231 | 235 | @Test |
232 | 236 | public void test_setDnsCachePolicy() throws Exception { |
233 | 237 | 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 | + |
234 | 246 | DnsCacheManipulator.setDnsCachePolicy(2); |
235 | 247 | assertEquals(2, DnsCacheManipulator.getDnsCachePolicy()); |
236 | 248 |
|
| 249 | + ////////////////////////////////////////////////// |
| 250 | + // 0. trigger dns cache by lookup |
| 251 | + ////////////////////////////////////////////////// |
237 | 252 | getIpByName(host); |
238 | 253 | final long tick = currentTimeMillis(); |
| 254 | + DnsCacheEntry dnsCacheEntry = DnsCacheManipulator.getDnsCache(host); |
| 255 | + assertBetween(dnsCacheEntry.getExpiration().getTime(), |
| 256 | + tick, tick + 2020); |
239 | 257 |
|
| 258 | + ////////////////////////////////////////////////// |
| 259 | + // 1. lookup before expire |
| 260 | + ////////////////////////////////////////////////// |
240 | 261 | sleep(1000); |
241 | 262 | getIpByName(host); |
| 263 | + // get dns cache before expire |
| 264 | + assertEquals(dnsCacheEntry, DnsCacheManipulator.getDnsCache(host)); |
242 | 265 |
|
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 | + ////////////////////////////////////////////////// |
246 | 269 | sleep(1020); |
247 | | - |
248 | 270 | // 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)); |
252 | 272 |
|
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); |
255 | 277 | assertNull(DnsCacheManipulator.getDnsCache(host)); |
256 | 278 |
|
257 | | - // relookup |
| 279 | + ////////////////////////////////////////////////// |
| 280 | + // 4. relookup |
| 281 | + ////////////////////////////////////////////////// |
258 | 282 | getIpByName(host); |
259 | | - final DnsCacheEntry relookup = DnsCacheManipulator.getDnsCache(host); |
260 | 283 | 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(); |
262 | 296 | } |
263 | 297 |
|
264 | 298 | @Test |
265 | 299 | public void test_setNegativeDnsCachePolicy() throws Exception { |
266 | 300 | DnsCacheManipulator.setDnsNegativeCachePolicy(2); |
267 | 301 | assertEquals(2, DnsCacheManipulator.getDnsNegativeCachePolicy()); |
268 | 302 |
|
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); |
275 | 307 | final long tick = currentTimeMillis(); |
| 308 | + assertOnlyNegativeCache(tick, tick + 2020); |
276 | 309 |
|
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 | + ////////////////////////////////////////////////// |
282 | 313 | 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); |
290 | 317 |
|
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) { |
292 | 340 | try { |
293 | | - getIpByName(DOMAIN_NOT_EXISTED); |
| 341 | + getIpByName(domain); |
294 | 342 | fail(); |
295 | 343 | } catch (UnknownHostException expected) { |
296 | 344 | assertTrue(true); |
297 | 345 | } |
| 346 | + } |
298 | 347 |
|
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); |
304 | 353 | } |
305 | 354 |
|
306 | 355 | static void assertEqualsIgnoreHostCase(DnsCacheEntry expected, DnsCacheEntry actual) { |
|
0 commit comments