8286045: Use ForceGC for cleaner test cases

Reviewed-by: rriggs
This commit is contained in:
Xue-Lei Andrew Fan 2022-05-26 17:59:28 +00:00
parent e44465d4d6
commit 7eb15593e1
3 changed files with 29 additions and 38 deletions
test/jdk
javax/security/auth/callback/PasswordCallback
sun/security/jgss

@ -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<PasswordCallback, ?> weakHashMap =
new WeakHashMap<>();
public static void main(String[] args) throws Exception {
// Create an object
@ -40,20 +42,15 @@ public final class CheckCleanerBound {
new PasswordCallback("Password: ", false);
passwordCallback.setPassword("ThisIsAPassword".toCharArray());
weakHashMap.put(passwordCallback, null);
WeakReference<PasswordCallback> 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");
}

@ -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.GSSContext;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSManager;
import java.util.WeakHashMap;
import java.lang.ref.WeakReference;
import jdk.test.lib.util.ForceGC;
public final class GssContextCleanup {
private final static WeakHashMap<GSSContext, ?> whm = new WeakHashMap<>();
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 final class GssContextCleanup {
// Create an object
GSSManager manager = GSSManager.getInstance();
GSSContext context = manager.createContext((GSSCredential)null);
whm.put(context, null);
WeakReference<GSSContext> 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");
}
}

@ -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<GSSName, ?> 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 final class GssNameCleanup {
try {
GSSName name =
manager.createName("u1", GSSName.NT_USER_NAME);
whm.put(name, null);
WeakReference<GSSName> 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");
}
}
}