Improvements on Thread check

This commit is contained in:
Clebert Suconic 2016-01-12 15:10:48 -05:00
parent 8b6d3a65be
commit 2d3061d9b6
3 changed files with 42 additions and 50 deletions

View File

@ -51,18 +51,15 @@ public class ThreadLeakCheckRule extends ExternalResource {
* Override to tear down your specific external resource. * Override to tear down your specific external resource.
*/ */
protected void after() { protected void after() {
try {
if (enabled) { if (enabled) {
StringBuffer buffer = null;
boolean failed = true; boolean failed = true;
boolean failedOnce = false; boolean failedOnce = false;
long timeout = System.currentTimeMillis() + 60000; long timeout = System.currentTimeMillis() + 60000;
while (failed && timeout > System.currentTimeMillis()) { while (failed && timeout > System.currentTimeMillis()) {
buffer = new StringBuffer(); failed = checkThread();
failed = checkThread(buffer);
if (failed) { if (failed) {
failedOnce = true; failedOnce = true;
@ -72,17 +69,10 @@ public class ThreadLeakCheckRule extends ExternalResource {
} }
catch (Throwable e) { catch (Throwable e) {
} }
System.out.println("There are still threads running, trying again");
System.out.println(buffer);
} }
} }
if (failed) { if (failed) {
System.out.println("Thread leaked on test \n" +
buffer);
System.out.println("Thread leakage! Failure!!!");
Assert.fail("Thread leaked"); Assert.fail("Thread leaked");
} }
else if (failedOnce) { else if (failedOnce) {
@ -90,45 +80,49 @@ public class ThreadLeakCheckRule extends ExternalResource {
System.out.println(); System.out.println();
} }
} }
else { else {
enabled = true; enabled = true;
} }
}
finally {
// clearing just to help GC
previousThreads = null;
}
} }
private boolean checkThread() {
/**
* @param buffer
* @return
*/
private boolean checkThread(StringBuffer buffer) {
boolean failedThread = false; boolean failedThread = false;
Map<Thread, StackTraceElement[]> postThreads = Thread.getAllStackTraces(); Map<Thread, StackTraceElement[]> postThreads = Thread.getAllStackTraces();
if (postThreads != null && previousThreads != null && postThreads.size() > previousThreads.size()) { if (postThreads != null && previousThreads != null && postThreads.size() > previousThreads.size()) {
buffer.append("*********************************************************************************\n");
buffer.append("LEAKING THREADS\n");
for (Thread aliveThread : postThreads.keySet()) { 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; failedThread = true;
buffer.append("=============================================================================\n"); System.out.println("=============================================================================");
buffer.append("Thread " + aliveThread + " is still alive with the following stackTrace:\n"); System.out.println("Thread " + aliveThread + " is still alive with the following stackTrace:");
StackTraceElement[] elements = postThreads.get(aliveThread); StackTraceElement[] elements = postThreads.get(aliveThread);
for (StackTraceElement el : elements) { for (StackTraceElement el : elements) {
buffer.append(el + "\n"); System.out.println(el);
} }
} }
} }
buffer.append("*********************************************************************************\n"); if (failedThread) {
System.out.println("*********************************************************************************");
} }
}
return failedThread; return failedThread;
} }

View File

@ -1393,7 +1393,6 @@ public class FailoverTest extends FailoverTestBase {
@Test @Test
public void testCreateNewFactoryAfterFailover() throws Exception { public void testCreateNewFactoryAfterFailover() throws Exception {
this.disableCheckThread();
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true); locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true);
sf = createSessionFactoryAndWaitForTopology(locator, 2); sf = createSessionFactoryAndWaitForTopology(locator, 2);

View File

@ -246,7 +246,6 @@ public class LiveToLiveFailoverTest extends FailoverTest {
@Override @Override
@Test @Test
public void testCreateNewFactoryAfterFailover() throws Exception { public void testCreateNewFactoryAfterFailover() throws Exception {
this.disableCheckThread();
locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true); locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true);
sf = createSessionFactoryAndWaitForTopology(locator, 2); sf = createSessionFactoryAndWaitForTopology(locator, 2);