8258007: Add instrumentation to NativeLibraryTest

Reviewed-by: mchung, naoto
This commit is contained in:
Brent Christian 2020-12-19 00:17:02 +00:00
parent c04c7e1f10
commit e680ebeb2f
2 changed files with 13 additions and 4 deletions

View File

@ -60,6 +60,7 @@ public class NativeLibraryTest {
setup(); setup();
for (int count=1; count <= 5; count++) { for (int count=1; count <= 5; count++) {
System.out.println("count: " + count);
// create a class loader and load a native library // create a class loader and load a native library
runTest(); runTest();
// Unload the class loader and native library, and give the Cleaner // Unload the class loader and native library, and give the Cleaner

View File

@ -34,15 +34,18 @@ import java.util.function.BooleanSupplier;
public class ForceGC { public class ForceGC {
private final CountDownLatch cleanerInvoked = new CountDownLatch(1); private final CountDownLatch cleanerInvoked = new CountDownLatch(1);
private final Cleaner cleaner = Cleaner.create(); private final Cleaner cleaner = Cleaner.create();
private Object o;
public ForceGC() { public ForceGC() {
cleaner.register(new Object(), () -> cleanerInvoked.countDown()); this.o = new Object();
cleaner.register(o, () -> cleanerInvoked.countDown());
} }
private void doit() { private void doit(int iter) {
try { try {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
System.gc(); System.gc();
System.out.println("gc " + i); System.out.println("doit() iter: " + iter + ", gc " + i);
if (cleanerInvoked.await(1L, TimeUnit.SECONDS)) { if (cleanerInvoked.await(1L, TimeUnit.SECONDS)) {
return; return;
} }
@ -62,9 +65,14 @@ public class ForceGC {
* @throws InterruptedException if the current thread is interrupted while waiting * @throws InterruptedException if the current thread is interrupted while waiting
*/ */
public boolean await(BooleanSupplier s) { public boolean await(BooleanSupplier s) {
o = null; // Keep reference to Object until now, to ensure the Cleaner
// doesn't count down the latch before await() is called.
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
if (s.getAsBoolean()) return true; if (s.getAsBoolean()) return true;
doit(); doit(i);
try { Thread.sleep(1000); } catch (InterruptedException e) {
throw new AssertionError("unexpected interrupted sleep", e);
}
} }
return false; return false;
} }