8291651: CleanerTest.java fails with "Cleanable was cleaned"

Reviewed-by: jpai, dfuchs
This commit is contained in:
Roger Riggs 2022-09-01 14:02:06 +00:00
parent bd674dc55e
commit 2d10d4f54e

View File

@ -25,10 +25,7 @@ import java.lang.ref.Cleaner;
import java.lang.ref.Reference;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@ -59,9 +56,10 @@ import org.testng.annotations.Test;
* @modules java.base/jdk.internal.misc
* java.base/jdk.internal.ref
* java.management
* @compile --enable-preview -source ${jdk.version} CleanerTest.java
* @enablePreview
* @compile -source ${jdk.version} CleanerTest.java
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run testng/othervm --enable-preview
* @run testng/othervm
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
* -verbose:gc CleanerTest
*/
@ -105,7 +103,7 @@ public class CleanerTest {
CleanableCase s = setupPhantom(COMMON, cleaner);
cleaner = null;
checkCleaned(s.getSemaphore(), true, "Cleaner was cleaned:");
checkCleaned(s.getSemaphore(), true, "Cleaner was cleaned");
}
/**
@ -140,7 +138,7 @@ public class CleanerTest {
CleanableCase s = setupPhantom(COMMON, cleaner);
cleaner = null;
checkCleaned(s.getSemaphore(), true, "Cleaner was cleaned:");
checkCleaned(s.getSemaphore(), true, "Cleaner was cleaned");
}
/**
@ -222,9 +220,9 @@ public class CleanerTest {
checkCleaned(test.getSemaphore(),
r == CleanableCase.EV_CLEAN,
"Cleanable was cleaned:");
"Cleanable was cleaned");
checkCleaned(cc.getSemaphore(), true,
"The reference to the Cleanable was freed:");
"The reference to the Cleanable was freed");
}
/**
@ -283,8 +281,8 @@ public class CleanerTest {
* Use a larger number of cycles to wait for an expected cleaning to occur.
*
* @param semaphore a Semaphore
* @param expectCleaned true if cleaning should occur
* @param msg a message to explain the error
* @param expectCleaned true if cleaning the function should have been run, otherwise not run
* @param msg a message describing the cleaning function expected to be run or not run
*/
static void checkCleaned(Semaphore semaphore, boolean expectCleaned, String msg) {
long max_cycles = expectCleaned ? 10 : 3;
@ -294,9 +292,10 @@ public class CleanerTest {
whitebox.fullGC();
try {
if (semaphore.tryAcquire(Utils.adjustTimeout(10L), TimeUnit.MILLISECONDS)) {
if (semaphore.tryAcquire(Utils.adjustTimeout(200L), TimeUnit.MILLISECONDS)) {
System.out.printf(" Cleanable cleaned in cycle: %d%n", cycle);
Assert.assertEquals(true, expectCleaned, msg);
if (!expectCleaned)
Assert.fail("Should not have been run: " + msg);
return;
}
} catch (InterruptedException ie) {
@ -304,7 +303,8 @@ public class CleanerTest {
}
}
// Object has not been cleaned
Assert.assertEquals(false, expectCleaned, msg);
if (expectCleaned)
Assert.fail("Should have been run: " + msg);
}
/**
@ -552,7 +552,7 @@ public class CleanerTest {
}
obj = null;
checkCleaned(s1, true, "reference was cleaned:");
checkCleaned(s1, true, "reference was cleaned");
cleaner = null;
}
@ -567,6 +567,6 @@ public class CleanerTest {
CleanableCase s = setupPhantom(cleaner, obj);
obj = null;
checkCleaned(s.getSemaphore(), true,
"Object was cleaned using CleanerFactor.cleaner():");
"Object cleaned using internal CleanerFactory.cleaner()");
}
}