66
77import java .net .InetAddress ;
88import java .net .UnknownHostException ;
9- import java .util .Arrays ;
109import java .util .Date ;
1110import java .util .HashSet ;
1211import java .util .List ;
1312import java .util .Set ;
1413
1514import static com .alibaba .dcm .Util .getIpByName ;
15+ import static com .alibaba .dcm .internal .TestTimeUtil .NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS ;
1616import static java .lang .System .currentTimeMillis ;
1717import static java .lang .Thread .sleep ;
1818import static org .junit .Assert .assertArrayEquals ;
@@ -108,19 +108,14 @@ public void test_setDnsCache_getAllDnsCache() {
108108 DnsCacheManipulator .setDnsCache (host , IP3 );
109109
110110 final List <DnsCacheEntry > allDnsCacheEntries = DnsCacheManipulator .getAllDnsCache ();
111- @ SuppressWarnings ("ArraysAsListWithZeroOrOneArgument" )
112- final List <DnsCacheEntry > expected = Arrays .asList (
113- new DnsCacheEntry (host .toLowerCase (), new String []{IP3 }, new Date (Long .MAX_VALUE )));
111+ assertEquals (1 , allDnsCacheEntries .size ());
114112
115- assertEquals (expected .size (), allDnsCacheEntries .size ());
113+ final DnsCacheEntry expected = new DnsCacheEntry (
114+ host .toLowerCase (), new String []{IP3 }, new Date (Long .MAX_VALUE ));
116115
117- DnsCacheEntry expectedDnsCacheEntry = expected .get (0 );
118- DnsCacheEntry dnsCacheEntry = allDnsCacheEntries .get (0 );
119- assertEqualsIgnoreCase (expectedDnsCacheEntry .getHost (), dnsCacheEntry .getHost ());
120- assertEquals (expectedDnsCacheEntry .getIp (), dnsCacheEntry .getIp ());
116+ assertEqualsIgnoreHostCase (expected , allDnsCacheEntries .get (0 ));
121117
122- long now = currentTimeMillis ();
123- assertEquals (expectedDnsCacheEntry .getExpiration ().getTime () - now > 315360000000L , dnsCacheEntry .getExpiration ().getTime () - now > 315360000000L );
118+ // Check NegativeCache
124119 assertTrue (DnsCacheManipulator .getWholeDnsCache ().getNegativeCache ().isEmpty ());
125120 }
126121
@@ -211,26 +206,21 @@ public void test_setNotExistedDomain_canExpire_thenReLookupAndNotExisted() throw
211206
212207 @ Test
213208 public void test_multi_ips_in_config_file () {
214- long now = currentTimeMillis ();
215209 DnsCacheManipulator .loadDnsCacheConfig ("dns-cache-multi-ips.properties" );
216210
217211 final String host = "www.hello-multi-ips.com" ;
218- DnsCacheEntry entry = new DnsCacheEntry (host ,
212+ DnsCacheEntry expected = new DnsCacheEntry (host ,
219213 new String []{"42.42.41.1" , "42.42.41.2" }, new Date (Long .MAX_VALUE ));
220- DnsCacheEntry dnsCache = DnsCacheManipulator .getDnsCache (host );
221- assertEquals (entry .getHost (), dnsCache .getHost ());
222- assertEquals (entry .getIp (), dnsCache .getIp ());
223- assertEquals (entry .getExpiration ().getTime () - now > 315360000000L , dnsCache .getExpiration ().getTime () - now > 315360000000L );
224214
215+ final DnsCacheEntry actual = DnsCacheManipulator .getDnsCache (host );
216+ assertEqualsIgnoreHostCase (expected , actual );
225217
226218 final String hostLoose = "www.hello-multi-ips-loose.com" ;
227- DnsCacheEntry entryLoose = new DnsCacheEntry (hostLoose ,
219+ DnsCacheEntry expectedLoose = new DnsCacheEntry (hostLoose ,
228220 new String []{"42.42.41.1" , "42.42.41.2" , "42.42.41.3" , "42.42.41.4" }, new Date (Long .MAX_VALUE ));
229- DnsCacheEntry dnsCacheLoose = DnsCacheManipulator .getDnsCache (hostLoose );
230- assertEquals (entryLoose .getHost (), dnsCacheLoose .getHost ());
231- assertEquals (entryLoose .getIp (), dnsCacheLoose .getIp ());
232- assertEquals (entryLoose .getExpiration ().getTime () - now > 315360000000L , dnsCacheLoose .getExpiration ().getTime () - now > 315360000000L );
233221
222+ DnsCacheEntry actualLoose = DnsCacheManipulator .getDnsCache (hostLoose );
223+ assertEqualsIgnoreHostCase (expectedLoose , actualLoose );
234224 }
235225
236226 @ Test
@@ -314,7 +304,23 @@ public void test_setNegativeDnsCachePolicy() throws Exception {
314304 assertBetween (relookup .getExpiration ().getTime (), relookupTick , relookupTick + 2020 );
315305 }
316306
317- static void assertEqualsIgnoreCase (String expected , String actual ) {
307+ static void assertEqualsIgnoreHostCase (DnsCacheEntry expected , DnsCacheEntry actual ) {
308+ assertEqualsIgnoreCase (expected .getHost (), actual .getHost ());
309+ assertArrayEquals (expected .getIps (), actual .getIps ());
310+
311+ final long expectedExpiration = expected .getExpiration ().getTime ();
312+ final long actualExpiration = actual .getExpiration ().getTime ();
313+ if (expectedExpiration == Long .MAX_VALUE ) {
314+ // hard code test logic for jdk 9+
315+ if (actualExpiration != Long .MAX_VALUE ) {
316+ assertEqualsWithTolerance (NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS , actualExpiration , 5 );
317+ }
318+ } else {
319+ assertEquals (expectedExpiration , actualExpiration );
320+ }
321+ }
322+
323+ static void assertEqualsIgnoreCase (String expected , String actual ) {
318324 assertEquals (expected .toLowerCase (), actual .toLowerCase ());
319325 }
320326
@@ -324,4 +330,8 @@ static void assertBetween(long actual, long start, long end) {
324330 fail (start + " <= " + actual + " <= " + end + ", failed!" );
325331 }
326332 }
333+
334+ static void assertEqualsWithTolerance (long expected , long actual , long tolerance ) {
335+ assertBetween (actual , expected - tolerance , expected + tolerance );
336+ }
327337}
0 commit comments