From 9f6aaaf331ac695f158af647e22ef38dc9e8e290 Mon Sep 17 00:00:00 2001 From: Roger Riggs Date: Tue, 13 Aug 2024 14:47:18 +0000 Subject: [PATCH] 8338252: [lworld] jdk/internal/ReferencedKeyTest fix merge and robustness --- .../jdk/internal/util/ReferencedKeyTest.java | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/test/jdk/jdk/internal/util/ReferencedKeyTest.java b/test/jdk/jdk/internal/util/ReferencedKeyTest.java index e3c477f57f4..fdf1bcf7315 100644 --- a/test/jdk/jdk/internal/util/ReferencedKeyTest.java +++ b/test/jdk/jdk/internal/util/ReferencedKeyTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8285932 8310913 8336390 + * @bug 8285932 8310913 8336390 8338060 8338252 * @summary Test features provided by the ReferencedKeyMap/ReferencedKeySet classes. * @modules java.base/jdk.internal.util * @compile --patch-module java.base=${test.src} ReferencedKeyTest.java @@ -36,19 +36,28 @@ import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BooleanSupplier; +import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.IntStream; public class ReferencedKeyTest { - static String BASE_KEY = "BASEKEY-"; + private static String BASE_KEY = "BASEKEY-"; - static String genKey(int i) { + // Return a String (identity object) that can be a key in WeakHashMap. + private static String genKey(int i) { return BASE_KEY + i; } + // Return a String of the letter 'a' plus the integer (0..0xffff) + private static String genValue(int i) { + return String.valueOf((char) ('a' + i)); + } + public static void main(String[] args) { mapTest(false, HashMap::new); mapTest(true, HashMap::new); @@ -69,26 +78,30 @@ static void assertTrue(boolean test, String message) { static void mapTest(boolean isSoft, Supplier, String>> supplier) { Map map = ReferencedKeyMap.create(isSoft, supplier); - populate(map); + var strongKeys = populate(map); // Retain references to the keys + methods(map); + Reference.reachabilityFence(strongKeys); + + strongKeys = null; // drop strong key references if (!isSoft) { if (!collect(() -> map.isEmpty())) { throw new RuntimeException("WeakReference map not collecting!"); } } - populate(map); - methods(map); } static void setTest(boolean isSoft, Supplier, ReferenceKey>> supplier) { ReferencedKeySet set = ReferencedKeySet.create(isSoft, supplier); - populate(set); + var strongKeys = populate(set); // Retain references to the keys + methods(set); + Reference.reachabilityFence(strongKeys); + + strongKeys = null; // drop strong key references if (!isSoft) { if (!collect(() -> set.isEmpty())) { throw new RuntimeException("WeakReference set not collecting!"); } } - populate(set); - methods(set); } static void methods(Map map) { @@ -132,8 +145,8 @@ static void methods(ReferencedKeySet set) { assertTrue(intern2 != null, "intern failed"); assertTrue(element3 == intern3, "intern failed"); - Long value1 = Long.valueOf(BASE_KEY + 999); - Long value2 = Long.valueOf(BASE_KEY + 999); + String value1 = genKey(999); + String value2 = genKey(999); assertTrue(set.add(value1), "key not added"); assertTrue(!set.add(value1), "key added after second attempt"); assertTrue(!set.add(value2), "key should not have been added"); @@ -168,18 +181,23 @@ static boolean collect(BooleanSupplier booleanSupplier) { return booleanSupplier.getAsBoolean(); } - static void populate(Map map) { - for (int i = 0; i < 26; i++) { - String key = genKey(i); - String value = String.valueOf((char) ('a' + i)); - map.put(key, value); + static List populate(Map map) { + var keyRefs = genStrings(0, 26, ReferencedKeyTest::genKey); + var valueRefs = genStrings(0, 26, ReferencedKeyTest::genValue); + for (int i = 0; i < keyRefs.size(); i++) { + map.put(keyRefs.get(i), valueRefs.get(i)); } + return keyRefs; } - static void populate(Set set) { - for (int i = 0; i < 26; i++) { - String value = genKey(i); - set.add(value); - } + static List populate(Set set) { + var keyRefs = genStrings(0, 26, ReferencedKeyTest::genKey); + set.addAll(keyRefs); + return keyRefs; + } + + // Generate a List of consecutive strings using a function int -> String + static List genStrings(int min, int maxExclusive, Function genString) { + return IntStream.range(min, maxExclusive).mapToObj(i -> genString.apply(i)).toList(); } }