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.
*/
protected void after() {
try {
if (enabled) {
StringBuffer buffer = null;
boolean failed = true;
boolean failedOnce = false;
long timeout = System.currentTimeMillis() + 60000;
while (failed && timeout > System.currentTimeMillis()) {
buffer = new StringBuffer();
failed = checkThread(buffer);
failed = checkThread();
if (failed) {
failedOnce = true;
@ -72,17 +69,10 @@ public class ThreadLeakCheckRule extends ExternalResource {
}
catch (Throwable e) {
}
System.out.println("There are still threads running, trying again");
System.out.println(buffer);
}
}
if (failed) {
System.out.println("Thread leaked on test \n" +
buffer);
System.out.println("Thread leakage! Failure!!!");
Assert.fail("Thread leaked");
}
else if (failedOnce) {
@ -90,45 +80,49 @@ public class ThreadLeakCheckRule extends ExternalResource {
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<Thread, StackTraceElement[]> 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;
}

View File

@ -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);

View File

@ -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);