Class ThreadLeakControl
java.lang.Object
com.carrotsearch.randomizedtesting.ThreadLeakControl
Everything corresponding to thread leak control. This is very, very fragile to changes
because of how threads interact and where they can be spun off.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA dummy class serving as the source of defaults for annotations.private static classprivate static classRuns aStatementand keeps any exception and completion flag.private classSub-notifier that controls passing events back in case of timeouts.private static classprivate static classTimeout parsing code and logic. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ThreadFilter> Built-in filters.This is the assumed set of threads without leaks.private final intHow many attempts to interrupt and then kill a runaway thread before giving up?private final intHow long to wait between attempts to kill a runaway thread (millis).private static final LoggerShared LOGGER.private final ObjectAtomic section for passing notifier events.private final RandomizedRunnerThe governing runner.private final ThreadLeakControl.SubNotifierprivate ThreadFilterUser filter (compound).private AtomicBooleanSuite timeout.private ThreadLeakControl.TimeoutValueSuite timeout.private final org.junit.runner.notification.RunNotifierTarget notifier.private ThreadLeakControl.TimeoutValueTest timeout.(package private) ThreadLeakGroupThread leak detection group. -
Constructor Summary
ConstructorsConstructorDescriptionThreadLeakControl(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckThreadLeaks(Set<Thread> expectedState, List<Throwable> errors, LifecycleScope scope, org.junit.runner.Description description, AnnotatedElement... annotationChain) Perform a thread leak check at the given scope.private static voidCheck on zombie threads status.private intDetermine timeout for a single test method (candidate).private intdetermineTimeout(Class<?> suiteClass) Determine timeout for a suite.private static <T extends Throwable>
TemptyStack(T t) Clears aThrowable's stack.private static <T extends Annotation>
TfirstAnnotated(Class<T> clazz, AnnotatedElement... elements) Returns an annotation's instance declared on any annotated element (first one wins) or the default value if not present on any of them.(package private) booleanforkTimeoutingTask(ThreadLeakControl.StatementRunner r, int timeout, List<Throwable> errors) Fork or not depending on the timeout value.private StringformatThreadStacks(Map<Thread, StackTraceElement[]> threads) Dump threads and their current stack trace.private StringDump thread state.(package private) org.junit.runners.model.StatementforSuite(org.junit.runners.model.Statement s, org.junit.runner.Description suiteDescription) AStatementfor wrapping suite-level execution.(package private) org.junit.runners.model.StatementforTest(org.junit.runners.model.Statement s, RandomizedRunner.TestCandidate c) AStatementfor wrapping test-level execution.private static StackTraceElement[]getThreads(ThreadFilter... filters) Returns allThreadLeakGroupthreads for analysis.private HashMap<Thread, StackTraceElement[]> getThreadsWithTraces(ThreadFilter... filters) Returns allThreadLeakGroupapplicable threads, with stack traces, for analysis.private ThreadFilterinstantiateFilters(List<Throwable> errors, Class<?> suiteClass) Instantiate a full set ofThreadFilters for a suite.(package private) boolean(package private) org.junit.runner.notification.RunNotifiernotifier()Return theRunNotifierthat should be used by any sub-statements running actual instance-scope tests.private static ThreadFilteror(ThreadFilter... filters) protected voidprocessUncaught(List<Throwable> errors, List<RandomizedRunner.UncaughtException> uncaughtList) Process uncaught exceptions.refilter(Set<Thread> in, ThreadFilter f) Refilter a set of threadsprivate StringthreadNames(Collection<Thread> threads) Collect thread names.tryToInterruptAll(List<Throwable> errors, Set<Thread> threads) Attempt to interrupt all threads in the given set.
-
Field Details
-
LOGGER
Shared LOGGER. -
killAttempts
private final int killAttemptsHow many attempts to interrupt and then kill a runaway thread before giving up? -
killWait
private final int killWaitHow long to wait between attempts to kill a runaway thread (millis). -
targetNotifier
private final org.junit.runner.notification.RunNotifier targetNotifierTarget notifier. -
expectedSuiteState
-
notifierLock
Atomic section for passing notifier events. -
subNotifier
- See Also:
-
testTimeout
Test timeout. -
suiteTimeout
Suite timeout. -
builtinFilters
Built-in filters. -
suiteFilters
User filter (compound). -
runner
The governing runner. -
suiteTimedOut
Suite timeout. -
threadLeakGroup
ThreadLeakGroup threadLeakGroupThread leak detection group.
-
-
Constructor Details
-
ThreadLeakControl
ThreadLeakControl(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner)
-
-
Method Details
-
or
-
checkZombies
private static void checkZombies() throws org.junit.internal.AssumptionViolatedExceptionCheck on zombie threads status.- Throws:
org.junit.internal.AssumptionViolatedException
-
forSuite
org.junit.runners.model.Statement forSuite(org.junit.runners.model.Statement s, org.junit.runner.Description suiteDescription) AStatementfor wrapping suite-level execution. -
forTest
org.junit.runners.model.Statement forTest(org.junit.runners.model.Statement s, RandomizedRunner.TestCandidate c) AStatementfor wrapping test-level execution. -
refilter
Refilter a set of threads -
instantiateFilters
Instantiate a full set ofThreadFilters for a suite. -
emptyStack
-
processUncaught
protected void processUncaught(List<Throwable> errors, List<RandomizedRunner.UncaughtException> uncaughtList) Process uncaught exceptions. -
checkThreadLeaks
protected void checkThreadLeaks(Set<Thread> expectedState, List<Throwable> errors, LifecycleScope scope, org.junit.runner.Description description, AnnotatedElement... annotationChain) Perform a thread leak check at the given scope. -
formatThreadStacks
Dump threads and their current stack trace. -
threadNames
Collect thread names. -
formatThreadStacksFull
Dump thread state. -
getStackTrace
-
getThreadsWithTraces
Returns allThreadLeakGroupapplicable threads, with stack traces, for analysis. -
getThreads
Returns allThreadLeakGroupthreads for analysis. -
tryToInterruptAll
-
forkTimeoutingTask
boolean forkTimeoutingTask(ThreadLeakControl.StatementRunner r, int timeout, List<Throwable> errors) throws InterruptedException Fork or not depending on the timeout value.- Throws:
InterruptedException
-
isTimedOut
boolean isTimedOut() -
notifier
org.junit.runner.notification.RunNotifier notifier()Return theRunNotifierthat should be used by any sub-statements running actual instance-scope tests. We need this because we need to prevent spurious notifications after suite timeouts. -
determineTimeout
Determine timeout for a suite.- Returns:
- Returns timeout in milliseconds or 0 if the test should run until finished (possibly blocking forever).
-
determineTimeout
Determine timeout for a single test method (candidate).- Returns:
- Returns timeout in milliseconds or 0 if the test should run until finished (possibly blocking forever).
-
firstAnnotated
private static <T extends Annotation> T firstAnnotated(Class<T> clazz, AnnotatedElement... elements) Returns an annotation's instance declared on any annotated element (first one wins) or the default value if not present on any of them.
-