Improvements on Thread check
This commit is contained in:
parent
8b6d3a65be
commit
2d3061d9b6
|
@ -51,84 +51,78 @@ 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() {
|
||||||
if (enabled) {
|
try {
|
||||||
StringBuffer buffer = null;
|
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;
|
if (failed) {
|
||||||
while (failed && timeout > System.currentTimeMillis()) {
|
failedOnce = true;
|
||||||
buffer = new StringBuffer();
|
ActiveMQTestBase.forceGC();
|
||||||
|
try {
|
||||||
failed = checkThread(buffer);
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
catch (Throwable e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (failed) {
|
if (failed) {
|
||||||
failedOnce = true;
|
Assert.fail("Thread leaked");
|
||||||
ActiveMQTestBase.forceGC();
|
|
||||||
try {
|
|
||||||
Thread.sleep(500);
|
|
||||||
}
|
|
||||||
catch (Throwable e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("There are still threads running, trying again");
|
|
||||||
System.out.println(buffer);
|
|
||||||
}
|
}
|
||||||
|
else if (failedOnce) {
|
||||||
|
System.out.println("******************** Threads cleared after retries ********************");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (failed) {
|
enabled = true;
|
||||||
System.out.println("Thread leaked on test \n" +
|
|
||||||
buffer);
|
|
||||||
System.out.println("Thread leakage! Failure!!!");
|
|
||||||
|
|
||||||
Assert.fail("Thread leaked");
|
|
||||||
}
|
}
|
||||||
else if (failedOnce) {
|
|
||||||
System.out.println("******************** Threads cleared after retries ********************");
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
finally {
|
||||||
enabled = true;
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue