Class ThreadLeakControl
- java.lang.Object
-
- com.carrotsearch.randomizedtesting.ThreadLeakControl
-
class ThreadLeakControl extends java.lang.Object
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 Classes Modifier and Type Class Description private static class
ThreadLeakControl.DefaultAnnotationValues
A dummy class serving as the source of defaults for annotations.private static class
ThreadLeakControl.KnownSystemThread
private static class
ThreadLeakControl.StatementRunner
Runs aStatement
and keeps any exception and completion flag.private class
ThreadLeakControl.SubNotifier
Sub-notifier that controls passing events back in case of timeouts.private static class
ThreadLeakControl.ThisThreadFilter
private static class
ThreadLeakControl.TimeoutValue
Timeout parsing code and logic.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ThreadFilter>
builtinFilters
Built-in filters.private java.util.Set<java.lang.Thread>
expectedSuiteState
This is the assumed set of threads without leaks.private int
killAttempts
How many attempts to interrupt and then kill a runaway thread before giving up?private int
killWait
How long to wait between attempts to kill a runaway thread (millis).private static java.util.logging.Logger
LOGGER
Shared LOGGER.private java.lang.Object
notifierLock
Atomic section for passing notifier events.private RandomizedRunner
runner
The governing runner.private ThreadLeakControl.SubNotifier
subNotifier
private ThreadFilter
suiteFilters
User filter (compound).private java.util.concurrent.atomic.AtomicBoolean
suiteTimedOut
Suite timeout.private ThreadLeakControl.TimeoutValue
suiteTimeout
Suite timeout.private org.junit.runner.notification.RunNotifier
targetNotifier
Target notifier.private ThreadLeakControl.TimeoutValue
testTimeout
Test timeout.(package private) ThreadLeakGroup
threadLeakGroup
Thread leak detection group.
-
Constructor Summary
Constructors Constructor Description ThreadLeakControl(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkThreadLeaks(java.util.Set<java.lang.Thread> expectedState, java.util.List<java.lang.Throwable> errors, LifecycleScope scope, org.junit.runner.Description description, java.lang.reflect.AnnotatedElement... annotationChain)
Perform a thread leak check at the given scope.private static void
checkZombies()
Check on zombie threads status.private int
determineTimeout(RandomizedRunner.TestCandidate c)
Determine timeout for a single test method (candidate).private int
determineTimeout(java.lang.Class<?> suiteClass)
Determine timeout for a suite.private static <T extends java.lang.Throwable>
TemptyStack(T t)
Clears aThrowable
's stack.private static <T extends java.lang.annotation.Annotation>
TfirstAnnotated(java.lang.Class<T> clazz, java.lang.reflect.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, java.util.List<java.lang.Throwable> errors)
Fork or not depending on the timeout value.private java.lang.String
formatThreadStacks(java.util.Map<java.lang.Thread,java.lang.StackTraceElement[]> threads)
Dump threads and their current stack trace.private java.lang.String
formatThreadStacksFull()
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 java.lang.StackTraceElement[]
getStackTrace(java.lang.Thread t)
private java.util.HashSet<java.lang.Thread>
getThreads(ThreadFilter... filters)
Returns allThreadLeakGroup
threads for analysis.private java.util.HashMap<java.lang.Thread,java.lang.StackTraceElement[]>
getThreadsWithTraces(ThreadFilter... filters)
Returns allThreadLeakGroup
applicable threads, with stack traces, for analysis.private ThreadFilter
instantiateFilters(java.util.List<java.lang.Throwable> errors, java.lang.Class<?> suiteClass)
Instantiate a full set ofThreadFilter
s for a suite.(package private) boolean
isTimedOut()
(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(java.util.List<java.lang.Throwable> errors, java.util.List<RandomizedRunner.UncaughtException> uncaughtList)
Process uncaught exceptions.protected java.util.Set<java.lang.Thread>
refilter(java.util.Set<java.lang.Thread> in, ThreadFilter f)
Refilter a set of threadsprivate java.lang.String
threadNames(java.util.Collection<java.lang.Thread> threads)
Collect thread names.private java.util.Set<java.lang.Thread>
tryToInterruptAll(java.util.List<java.lang.Throwable> errors, java.util.Set<java.lang.Thread> threads)
Attempt to interrupt all threads in the given set.
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
Shared LOGGER.
-
killAttempts
private final int killAttempts
How many attempts to interrupt and then kill a runaway thread before giving up?
-
killWait
private final int killWait
How long to wait between attempts to kill a runaway thread (millis).
-
targetNotifier
private final org.junit.runner.notification.RunNotifier targetNotifier
Target notifier.
-
expectedSuiteState
private final java.util.Set<java.lang.Thread> expectedSuiteState
This is the assumed set of threads without leaks.
-
notifierLock
private final java.lang.Object notifierLock
Atomic section for passing notifier events.
-
subNotifier
private final ThreadLeakControl.SubNotifier subNotifier
- See Also:
ThreadLeakControl.SubNotifier
-
testTimeout
private ThreadLeakControl.TimeoutValue testTimeout
Test timeout.
-
suiteTimeout
private ThreadLeakControl.TimeoutValue suiteTimeout
Suite timeout.
-
builtinFilters
private final java.util.List<ThreadFilter> builtinFilters
Built-in filters.
-
suiteFilters
private ThreadFilter suiteFilters
User filter (compound).
-
runner
private final RandomizedRunner runner
The governing runner.
-
suiteTimedOut
private java.util.concurrent.atomic.AtomicBoolean suiteTimedOut
Suite timeout.
-
threadLeakGroup
ThreadLeakGroup threadLeakGroup
Thread leak detection group.
-
-
Constructor Detail
-
ThreadLeakControl
ThreadLeakControl(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner)
-
-
Method Detail
-
or
private static ThreadFilter or(ThreadFilter... filters)
-
checkZombies
private static void checkZombies() throws org.junit.internal.AssumptionViolatedException
Check 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
protected java.util.Set<java.lang.Thread> refilter(java.util.Set<java.lang.Thread> in, ThreadFilter f)
Refilter a set of threads
-
instantiateFilters
private ThreadFilter instantiateFilters(java.util.List<java.lang.Throwable> errors, java.lang.Class<?> suiteClass)
Instantiate a full set ofThreadFilter
s for a suite.
-
emptyStack
private static <T extends java.lang.Throwable> T emptyStack(T t)
Clears aThrowable
's stack.
-
processUncaught
protected void processUncaught(java.util.List<java.lang.Throwable> errors, java.util.List<RandomizedRunner.UncaughtException> uncaughtList)
Process uncaught exceptions.
-
checkThreadLeaks
protected void checkThreadLeaks(java.util.Set<java.lang.Thread> expectedState, java.util.List<java.lang.Throwable> errors, LifecycleScope scope, org.junit.runner.Description description, java.lang.reflect.AnnotatedElement... annotationChain)
Perform a thread leak check at the given scope.
-
formatThreadStacks
private java.lang.String formatThreadStacks(java.util.Map<java.lang.Thread,java.lang.StackTraceElement[]> threads)
Dump threads and their current stack trace.
-
threadNames
private java.lang.String threadNames(java.util.Collection<java.lang.Thread> threads)
Collect thread names.
-
formatThreadStacksFull
private java.lang.String formatThreadStacksFull()
Dump thread state.
-
getStackTrace
private static java.lang.StackTraceElement[] getStackTrace(java.lang.Thread t)
-
getThreadsWithTraces
private java.util.HashMap<java.lang.Thread,java.lang.StackTraceElement[]> getThreadsWithTraces(ThreadFilter... filters)
Returns allThreadLeakGroup
applicable threads, with stack traces, for analysis.
-
getThreads
private java.util.HashSet<java.lang.Thread> getThreads(ThreadFilter... filters)
Returns allThreadLeakGroup
threads for analysis.
-
tryToInterruptAll
private java.util.Set<java.lang.Thread> tryToInterruptAll(java.util.List<java.lang.Throwable> errors, java.util.Set<java.lang.Thread> threads)
Attempt to interrupt all threads in the given set.
-
forkTimeoutingTask
boolean forkTimeoutingTask(ThreadLeakControl.StatementRunner r, int timeout, java.util.List<java.lang.Throwable> errors) throws java.lang.InterruptedException
Fork or not depending on the timeout value.- Throws:
java.lang.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
private int determineTimeout(java.lang.Class<?> suiteClass)
Determine timeout for a suite.- Returns:
- Returns timeout in milliseconds or 0 if the test should run until finished (possibly blocking forever).
-
determineTimeout
private int determineTimeout(RandomizedRunner.TestCandidate c)
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 java.lang.annotation.Annotation> T firstAnnotated(java.lang.Class<T> clazz, java.lang.reflect.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.
-
-