Skip to content

Commit 26ef165

Browse files
committed
! fix ut fail: test logic distinguish java8- and java9+
1 parent f2393e3 commit 26ef165

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJdk8Minus.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,27 @@ private static boolean isDnsCacheEntryExpired(String host) {
179179

180180
public static DnsCache listInetAddressCache()
181181
throws NoSuchFieldException, IllegalAccessException {
182-
183182
final Map<String, Object> cache;
184183
final Map<String, Object> negativeCache;
185184
synchronized (getAddressCacheFieldOfInetAddress()) {
186185
cache = new HashMap<String, Object>(getCacheFiledOfAddressCacheFiledOfInetAddress());
187186
negativeCache = new HashMap<String, Object>(getCacheFiledOfNegativeCacheFiledOfInetAddress());
188187
}
189188

190-
List<DnsCacheEntry> retCache = new ArrayList<DnsCacheEntry>();
189+
return new DnsCache(convert(cache), convert(negativeCache));
190+
}
191+
192+
private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws IllegalAccessException {
193+
final List<DnsCacheEntry> ret = new ArrayList<DnsCacheEntry>();
191194
for (Map.Entry<String, Object> entry : cache.entrySet()) {
192195
final String host = entry.getKey();
193-
194196
if (isDnsCacheEntryExpired(host)) { // exclude expired entries!
195197
continue;
196198
}
197-
retCache.add(inetAddress$CacheEntry2DnsCacheEntry(host, entry.getValue()));
198-
}
199-
List<DnsCacheEntry> retNegativeCache = new ArrayList<DnsCacheEntry>();
200-
for (Map.Entry<String, Object> entry : negativeCache.entrySet()) {
201-
final String host = entry.getKey();
202-
retNegativeCache.add(inetAddress$CacheEntry2DnsCacheEntry(host, entry.getValue()));
199+
200+
ret.add(inetAddress$CacheEntry2DnsCacheEntry(host, entry.getValue()));
203201
}
204-
return new DnsCache(retCache, retNegativeCache);
202+
return ret;
205203
}
206204

207205

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.*;
1010

1111
import static com.alibaba.dcm.Util.getIpByName;
12+
import static com.alibaba.dcm.internal.JavaVersionUtil.isJdkAtMost8;
1213
import static com.alibaba.dcm.internal.TestTimeUtil.NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS;
1314
import static java.lang.System.currentTimeMillis;
1415
import static java.lang.Thread.sleep;
@@ -326,7 +327,11 @@ public void test_setNegativeDnsCachePolicy() throws Exception {
326327
// 3. touch dns cache with external other host operation
327328
//////////////////////////////////////////////////
328329
getIpByName("bing.com");
329-
assertEquals(0, DnsCacheManipulator.getWholeDnsCache().getNegativeCache().size());
330+
if (isJdkAtMost8()) {
331+
assertOnlyNegativeCache(tick, tick + 2020);
332+
} else {
333+
assertTrue(DnsCacheManipulator.getWholeDnsCache().getNegativeCache().isEmpty());
334+
}
330335

331336
//////////////////////////////////////////////////
332337
// 4. relookup
@@ -358,9 +363,12 @@ static void assertEqualsIgnoreHostCase(DnsCacheEntry expected, DnsCacheEntry act
358363

359364
final long expectedExpiration = expected.getExpiration().getTime();
360365
final long actualExpiration = actual.getExpiration().getTime();
366+
361367
if (expectedExpiration == Long.MAX_VALUE) {
362-
// hard code test logic for jdk 9+
363-
if (actualExpiration != Long.MAX_VALUE) {
368+
if (isJdkAtMost8()) {
369+
assertEquals(expectedExpiration, actualExpiration);
370+
} else {
371+
// hard code test logic for jdk 9+
364372
assertEqualsWithTolerance(NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS, actualExpiration, 5);
365373
}
366374
} else {

0 commit comments

Comments
 (0)