SessionCloseOnGCTest#testCloseOneSessionOnGC: enforce that Finalizer will be called after garbage collector

This commit is contained in:
Erich Duda 2015-10-16 12:15:34 +02:00 committed by Clebert Suconic
parent 487d976760
commit 7970b6e6f8
1 changed files with 20 additions and 3 deletions

View File

@ -525,10 +525,12 @@ public abstract class ActiveMQTestBase extends Assert {
public static void forceGC() {
log.info("#test forceGC");
WeakReference<Object> dumbReference = new WeakReference<Object>(new Object());
AtomicInteger finalized = new AtomicInteger(0);
WeakReference<DumbReference> dumbReference = new WeakReference<>(new DumbReference(finalized));
// A loop that will wait GC, using the minimal time as possible
while (dumbReference.get() != null) {
while (!(dumbReference.get() == null && finalized.get() == 1)) {
System.gc();
System.runFinalization();
try {
Thread.sleep(100);
}
@ -2535,4 +2537,19 @@ public abstract class ActiveMQTestBase extends Assert {
public static void waitForLatch(CountDownLatch latch) throws InterruptedException {
assertTrue("Latch has got to return within a minute", latch.await(1, TimeUnit.MINUTES));
}
protected static class DumbReference {
private AtomicInteger finalized;
public DumbReference(AtomicInteger finalized) {
this.finalized = finalized;
}
public void finalize() throws Throwable {
System.out.println("FINALIZE");
finalized.incrementAndGet();
super.finalize();
}
}
}