From 7eb15593e18a923bbc18c8d596cff87d87019640 Mon Sep 17 00:00:00 2001 From: Xue-Lei Andrew Fan Date: Thu, 26 May 2022 17:59:28 +0000 Subject: [PATCH] 8286045: Use ForceGC for cleaner test cases Reviewed-by: rriggs --- .../PasswordCallback/CheckCleanerBound.java | 21 +++++++-------- .../sun/security/jgss/GssContextCleanup.java | 19 ++++++------- .../jdk/sun/security/jgss/GssNameCleanup.java | 27 +++++++++---------- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/test/jdk/javax/security/auth/callback/PasswordCallback/CheckCleanerBound.java b/test/jdk/javax/security/auth/callback/PasswordCallback/CheckCleanerBound.java index 5d8a5cfea2784..1a2e0981c8253 100644 --- a/test/jdk/javax/security/auth/callback/PasswordCallback/CheckCleanerBound.java +++ b/test/jdk/javax/security/auth/callback/PasswordCallback/CheckCleanerBound.java @@ -25,14 +25,16 @@ * @test * @bug 8284910 * @summary Check that the cleaner is not bound to the PasswordCallback object + * @library /test/lib/ + * @build jdk.test.lib.util.ForceGC + * @run main/othervm CheckCleanerBound */ import javax.security.auth.callback.PasswordCallback; -import java.util.WeakHashMap; +import java.lang.ref.WeakReference; +import jdk.test.lib.util.ForceGC; public final class CheckCleanerBound { - private final static WeakHashMap weakHashMap = - new WeakHashMap<>(); public static void main(String[] args) throws Exception { // Create an object @@ -40,20 +42,15 @@ public static void main(String[] args) throws Exception { new PasswordCallback("Password: ", false); passwordCallback.setPassword("ThisIsAPassword".toCharArray()); - weakHashMap.put(passwordCallback, null); + WeakReference weakRef = + new WeakReference<>(passwordCallback); passwordCallback = null; - // Check if the PasswordCallback object could be collected. - // Wait to trigger the cleanup. - for (int i = 0; i < 10 && weakHashMap.size() != 0; i++) { - System.gc(); - Thread.sleep(100); - } - // Check if the object has been collected. The collection will not // happen if the cleaner implementation in PasswordCallback is bound // to the PasswordCallback object. - if (weakHashMap.size() > 0) { + ForceGC gc = new ForceGC(); + if (!gc.await(() -> weakRef.get() == null)) { throw new RuntimeException( "PasswordCallback object is not released"); } diff --git a/test/jdk/sun/security/jgss/GssContextCleanup.java b/test/jdk/sun/security/jgss/GssContextCleanup.java index 6bff6780bf5f2..d54a829a74456 100644 --- a/test/jdk/sun/security/jgss/GssContextCleanup.java +++ b/test/jdk/sun/security/jgss/GssContextCleanup.java @@ -26,6 +26,8 @@ * @bug 8284490 * @summary Remove finalizer method in java.security.jgss * @key intermittent + * @library /test/lib/ + * @build jdk.test.lib.util.ForceGC * @run main/othervm GssContextCleanup */ @@ -33,11 +35,11 @@ import org.ietf.jgss.GSSCredential; import org.ietf.jgss.GSSManager; -import java.util.WeakHashMap; +import java.lang.ref.WeakReference; -public final class GssContextCleanup { - private final static WeakHashMap whm = new WeakHashMap<>(); +import jdk.test.lib.util.ForceGC; +public final class GssContextCleanup { public static void main(String[] args) throws Exception { // Enable debug log so that the failure analysis could be easier. System.setProperty("sun.security.nativegss.debug", "true"); @@ -48,17 +50,12 @@ public static void main(String[] args) throws Exception { // Create an object GSSManager manager = GSSManager.getInstance(); GSSContext context = manager.createContext((GSSCredential)null); - whm.put(context, null); + WeakReference weakRef = new WeakReference<>(context); context = null; - // Wait to trigger the cleanup. - for (int i = 0; i < 10 && whm.size() > 0; i++) { - System.gc(); - Thread.sleep(100); - } - // Check if the object has been collected. - if (whm.size() > 0) { + ForceGC gc = new ForceGC(); + if (!gc.await(() -> weakRef.get() == null)) { throw new RuntimeException("GSSContext object is not released"); } } diff --git a/test/jdk/sun/security/jgss/GssNameCleanup.java b/test/jdk/sun/security/jgss/GssNameCleanup.java index c82879edca696..10a3c20858e3b 100644 --- a/test/jdk/sun/security/jgss/GssNameCleanup.java +++ b/test/jdk/sun/security/jgss/GssNameCleanup.java @@ -26,17 +26,19 @@ * @bug 8284490 * @summary Remove finalizer method in java.security.jgss * @key intermittent + * @library /test/lib/ + * @build jdk.test.lib.util.ForceGC * @run main/othervm GssNameCleanup */ -import java.util.WeakHashMap; +import java.lang.ref.WeakReference; import org.ietf.jgss.GSSManager; import org.ietf.jgss.GSSName; import org.ietf.jgss.GSSException; -public final class GssNameCleanup { - private final static WeakHashMap whm = new WeakHashMap<>(); +import jdk.test.lib.util.ForceGC; +public final class GssNameCleanup { public static void main(String[] args) throws Exception { // Enable debug log so that the failure analysis could be easier. System.setProperty("sun.security.nativegss.debug", "true"); @@ -49,25 +51,20 @@ public static void main(String[] args) throws Exception { try { GSSName name = manager.createName("u1", GSSName.NT_USER_NAME); - whm.put(name, null); + WeakReference weakRef = new WeakReference<>(name); name = null; + + // Check if the object has been collected. + ForceGC gc = new ForceGC(); + if (!gc.await(() -> weakRef.get() == null)) { + throw new RuntimeException("GSSName object is not released"); + } } catch (GSSException gsse) { // createName() could fail if the local default realm // cannot be located. Just ignore the test case for // such circumstances. System.out.println("Ignore this test case: " + gsse); } - - // Wait to trigger the cleanup. - for (int i = 0; i < 10 && whm.size() > 0; i++) { - System.gc(); - Thread.sleep(100); - } - - // Check if the object has been collected. - if (whm.size() > 0) { - throw new RuntimeException("GSSName object is not released"); - } } }