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 class
A dummy class serving as the source of defaults for annotations.private static class
private static class
Runs aStatement
and keeps any exception and completion flag.private class
Sub-notifier that controls passing events back in case of timeouts.private static class
private static class
Timeout 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 int
How many attempts to interrupt and then kill a runaway thread before giving up?private final int
How long to wait between attempts to kill a runaway thread (millis).private static final Logger
Shared LOGGER.private final Object
Atomic section for passing notifier events.private final RandomizedRunner
The governing runner.private final ThreadLeakControl.SubNotifier
private ThreadFilter
User filter (compound).private AtomicBoolean
Suite timeout.private ThreadLeakControl.TimeoutValue
Suite timeout.private final org.junit.runner.notification.RunNotifier
Target notifier.private ThreadLeakControl.TimeoutValue
Test timeout.(package private) ThreadLeakGroup
Thread leak detection group. -
Constructor Summary
ConstructorsConstructorDescriptionThreadLeakControl
(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner) -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.private static void
Check on zombie threads status.private int
Determine timeout for a single test method (candidate).private int
determineTimeout
(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) boolean
forkTimeoutingTask
(ThreadLeakControl.StatementRunner r, int timeout, List<Throwable> errors) Fork or not depending on the timeout value.private String
formatThreadStacks
(Map<Thread, StackTraceElement[]> threads) Dump threads and their current stack trace.private String
Dump thread state.(package private) org.junit.runners.model.Statement
forSuite
(org.junit.runners.model.Statement s, org.junit.runner.Description suiteDescription) AStatement
for wrapping suite-level execution.(package private) org.junit.runners.model.Statement
forTest
(org.junit.runners.model.Statement s, RandomizedRunner.TestCandidate c) AStatement
for wrapping test-level execution.private static StackTraceElement[]
getThreads
(ThreadFilter... filters) Returns allThreadLeakGroup
threads for analysis.private HashMap
<Thread, StackTraceElement[]> getThreadsWithTraces
(ThreadFilter... filters) Returns allThreadLeakGroup
applicable threads, with stack traces, for analysis.private ThreadFilter
instantiateFilters
(List<Throwable> errors, Class<?> suiteClass) Instantiate a full set ofThreadFilter
s for a suite.(package private) boolean
(package private) org.junit.runner.notification.RunNotifier
notifier()
Return theRunNotifier
that should be used by any sub-statements running actual instance-scope tests.private static ThreadFilter
or
(ThreadFilter... filters) protected void
processUncaught
(List<Throwable> errors, List<RandomizedRunner.UncaughtException> uncaughtList) Process uncaught exceptions.refilter
(Set<Thread> in, ThreadFilter f) Refilter a set of threadsprivate String
threadNames
(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
This is the assumed set of threads without leaks. -
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) AStatement
for wrapping suite-level execution. -
forTest
org.junit.runners.model.Statement forTest(org.junit.runners.model.Statement s, RandomizedRunner.TestCandidate c) AStatement
for wrapping test-level execution. -
refilter
Refilter a set of threads -
instantiateFilters
Instantiate a full set ofThreadFilter
s for a suite. -
emptyStack
Clears aThrowable
's stack. -
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 allThreadLeakGroup
applicable threads, with stack traces, for analysis. -
getThreads
Returns allThreadLeakGroup
threads for analysis. -
tryToInterruptAll
Attempt to interrupt all threads in the given set. -
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 theRunNotifier
that 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.
-