Skip to content

Commit

Permalink
8286045: Use ForceGC for cleaner test cases
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs
  • Loading branch information
XueleiFan committed May 26, 2022
1 parent e44465d commit 7eb1559
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
Expand Up @@ -25,35 +25,32 @@
* @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
PasswordCallback passwordCallback =
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");
}
Expand Down
19 changes: 8 additions & 11 deletions test/jdk/sun/security/jgss/GssContextCleanup.java
Expand Up @@ -26,18 +26,20 @@
* @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
*/

import org.ietf.jgss.GSSContext;
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<GSSContext, ?> 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");
Expand All @@ -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<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");
}
}
Expand Down
27 changes: 12 additions & 15 deletions test/jdk/sun/security/jgss/GssNameCleanup.java
Expand Up @@ -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");
Expand All @@ -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<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");
}
}
}

3 comments on commit 7eb1559

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 7eb1559 Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gnu-andrew Could not automatically backport 7eb15593 to openjdk/jdk17u-dev due to conflicts in the following files:

  • test/jdk/sun/security/jgss/GssContextCleanup.java
  • test/jdk/sun/security/jgss/GssNameCleanup.java

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u-dev. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u-dev.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b gnu-andrew-backport-7eb15593

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 7eb15593e18a923bbc18c8d596cff87d87019640

# Backport the commit
$ git cherry-pick --no-commit 7eb15593e18a923bbc18c8d596cff87d87019640
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 7eb15593e18a923bbc18c8d596cff87d87019640'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u-dev with the title Backport 7eb15593e18a923bbc18c8d596cff87d87019640.

Please sign in to comment.