From 7970b6e6f8f6b41f35e3a3672b4603a0a7ab01b5 Mon Sep 17 00:00:00 2001 From: Erich Duda Date: Fri, 16 Oct 2015 12:15:34 +0200 Subject: [PATCH] SessionCloseOnGCTest#testCloseOneSessionOnGC: enforce that Finalizer will be called after garbage collector --- .../artemis/tests/util/ActiveMQTestBase.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java index 7cbe2f072e..7e358a91ba 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ActiveMQTestBase.java @@ -525,10 +525,12 @@ public abstract class ActiveMQTestBase extends Assert { public static void forceGC() { log.info("#test forceGC"); - WeakReference dumbReference = new WeakReference(new Object()); + AtomicInteger finalized = new AtomicInteger(0); + WeakReference 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(); + } + } +} \ No newline at end of file