-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: add NeonBeeTestExecutionListener to check for stale threads
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/test/java/io/neonbee/NeonBeeTestExecutionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.neonbee; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.platform.engine.TestExecutionResult; | ||
import org.junit.platform.launcher.TestExecutionListener; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link NeonBeeTestExecutionListener} checks for any stale Vert.x threads after test execution. Generally after a | ||
* test finishes to execute it must clean up all Vert.x resources. If not this listener will print an error to the logs. | ||
*/ | ||
public class NeonBeeTestExecutionListener implements TestExecutionListener { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TestExecutionListener.class); | ||
|
||
private boolean parallelExecution; | ||
|
||
@Override | ||
public void testPlanExecutionStarted(TestPlan testPlan) { | ||
parallelExecution = "true".equalsIgnoreCase(System.getProperty("junit.jupiter.execution.parallel.enabled")); | ||
if (parallelExecution) { | ||
LOGGER.warn("Cannot check for stale threads when running JUnit in parallel execution mode"); | ||
} | ||
} | ||
|
||
@Override | ||
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { | ||
if (!parallelExecution) { | ||
checkForStaleThreads("Vert.x", "vert.x-"); | ||
checkForStaleThreads("Hazelcast", "hz."); | ||
checkForStaleThreads("WatchService", "FileSystemWatch"); | ||
} | ||
} | ||
|
||
private static void checkForStaleThreads(String title, String namePrefix) { | ||
LOGGER.info("Checking for stale {} threads with '{}' prefix", title, namePrefix); | ||
Optional<Thread> staleThread = findStaleThread(namePrefix); | ||
if (staleThread.isPresent() && LOGGER.isErrorEnabled()) { | ||
LOGGER.error("Stale {} thread(s) detected!! Not closing the thread {} " | ||
+ "could result in the test runner not signaling completion", title, staleThread.get()); | ||
} | ||
} | ||
|
||
private static Optional<Thread> findStaleThread(String namePrefix) { | ||
return Thread.getAllStackTraces().keySet().stream().filter(thread -> thread.getName().startsWith(namePrefix)) | ||
.findAny(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.neonbee.NeonBeeTestExecutionListener |