From 2d3061d9b69de61cb89ea9cc3e5898e32caf7198 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Tue, 12 Jan 2016 15:10:48 -0500 Subject: [PATCH] Improvements on Thread check --- .../tests/util/ThreadLeakCheckRule.java | 90 +++++++++---------- .../cluster/failover/FailoverTest.java | 1 - .../failover/LiveToLiveFailoverTest.java | 1 - 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ThreadLeakCheckRule.java b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ThreadLeakCheckRule.java index 3f371a88e2..e3ef4c7ff7 100644 --- a/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ThreadLeakCheckRule.java +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/tests/util/ThreadLeakCheckRule.java @@ -51,84 +51,78 @@ public class ThreadLeakCheckRule extends ExternalResource { * Override to tear down your specific external resource. */ protected void after() { - if (enabled) { - StringBuffer buffer = null; + try { + if (enabled) { + boolean failed = true; - boolean failed = true; + boolean failedOnce = false; - boolean failedOnce = false; + long timeout = System.currentTimeMillis() + 60000; + while (failed && timeout > System.currentTimeMillis()) { + failed = checkThread(); - long timeout = System.currentTimeMillis() + 60000; - while (failed && timeout > System.currentTimeMillis()) { - buffer = new StringBuffer(); - - failed = checkThread(buffer); + if (failed) { + failedOnce = true; + ActiveMQTestBase.forceGC(); + try { + Thread.sleep(500); + } + catch (Throwable e) { + } + } + } if (failed) { - failedOnce = true; - ActiveMQTestBase.forceGC(); - try { - Thread.sleep(500); - } - catch (Throwable e) { - } - - System.out.println("There are still threads running, trying again"); - System.out.println(buffer); + Assert.fail("Thread leaked"); } + else if (failedOnce) { + System.out.println("******************** Threads cleared after retries ********************"); + System.out.println(); + } + } - - if (failed) { - System.out.println("Thread leaked on test \n" + - buffer); - System.out.println("Thread leakage! Failure!!!"); - - Assert.fail("Thread leaked"); + else { + enabled = true; } - else if (failedOnce) { - System.out.println("******************** Threads cleared after retries ********************"); - System.out.println(); - } - - } - else { - enabled = true; + finally { + // clearing just to help GC + previousThreads = null; } } - - /** - * @param buffer - * @return - */ - private boolean checkThread(StringBuffer buffer) { + private boolean checkThread() { boolean failedThread = false; Map postThreads = Thread.getAllStackTraces(); if (postThreads != null && previousThreads != null && postThreads.size() > previousThreads.size()) { - buffer.append("*********************************************************************************\n"); - buffer.append("LEAKING THREADS\n"); for (Thread aliveThread : postThreads.keySet()) { - if (!isExpectedThread(aliveThread) && !previousThreads.containsKey(aliveThread)) { + if (aliveThread.isAlive() && !isExpectedThread(aliveThread) && !previousThreads.containsKey(aliveThread)) { + if (!failedThread) { + System.out.println("*********************************************************************************"); + System.out.println("LEAKING THREADS"); + } failedThread = true; - buffer.append("=============================================================================\n"); - buffer.append("Thread " + aliveThread + " is still alive with the following stackTrace:\n"); + System.out.println("============================================================================="); + System.out.println("Thread " + aliveThread + " is still alive with the following stackTrace:"); StackTraceElement[] elements = postThreads.get(aliveThread); for (StackTraceElement el : elements) { - buffer.append(el + "\n"); + System.out.println(el); } } } - buffer.append("*********************************************************************************\n"); - + if (failedThread) { + System.out.println("*********************************************************************************"); + } } + + return failedThread; } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTest.java index 28012eb7f4..8b78f3d178 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/FailoverTest.java @@ -1393,7 +1393,6 @@ public class FailoverTest extends FailoverTestBase { @Test public void testCreateNewFactoryAfterFailover() throws Exception { - this.disableCheckThread(); locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true); sf = createSessionFactoryAndWaitForTopology(locator, 2); diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/LiveToLiveFailoverTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/LiveToLiveFailoverTest.java index d0876b3aab..68f65a4369 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/LiveToLiveFailoverTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cluster/failover/LiveToLiveFailoverTest.java @@ -246,7 +246,6 @@ public class LiveToLiveFailoverTest extends FailoverTest { @Override @Test public void testCreateNewFactoryAfterFailover() throws Exception { - this.disableCheckThread(); locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true); sf = createSessionFactoryAndWaitForTopology(locator, 2);