Class ThreadLeakControl
- java.lang.Object
-
- com.carrotsearch.randomizedtesting.ThreadLeakControl
-
class ThreadLeakControl extends java.lang.ObjectEverything 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 classThreadLeakControl.DefaultAnnotationValuesA dummy class serving as the source of defaults for annotations.private static classThreadLeakControl.KnownSystemThreadprivate static classThreadLeakControl.StatementRunnerRuns aStatementand keeps any exception and completion flag.private classThreadLeakControl.SubNotifierSub-notifier that controls passing events back in case of timeouts.private static classThreadLeakControl.ThisThreadFilterprivate static classThreadLeakControl.TimeoutValueTimeout parsing code and logic.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ThreadFilter>builtinFiltersBuilt-in filters.private java.util.Set<java.lang.Thread>expectedSuiteStateThis is the assumed set of threads without leaks.private intkillAttemptsHow many attempts to interrupt and then kill a runaway thread before giving up?private intkillWaitHow long to wait between attempts to kill a runaway thread (millis).private static java.util.logging.LoggerLOGGERShared LOGGER.private java.lang.ObjectnotifierLockAtomic section for passing notifier events.private RandomizedRunnerrunnerThe governing runner.private ThreadLeakControl.SubNotifiersubNotifierprivate ThreadFiltersuiteFiltersUser filter (compound).private java.util.concurrent.atomic.AtomicBooleansuiteTimedOutSuite timeout.private ThreadLeakControl.TimeoutValuesuiteTimeoutSuite timeout.private org.junit.runner.notification.RunNotifiertargetNotifierTarget notifier.private ThreadLeakControl.TimeoutValuetestTimeoutTest timeout.(package private) ThreadLeakGroupthreadLeakGroupThread 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 voidcheckThreadLeaks(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 voidcheckZombies()Check on zombie threads status.private intdetermineTimeout(RandomizedRunner.TestCandidate c)Determine timeout for a single test method (candidate).private intdetermineTimeout(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) booleanforkTimeoutingTask(ThreadLeakControl.StatementRunner r, int timeout, java.util.List<java.lang.Throwable> errors)Fork or not depending on the timeout value.private java.lang.StringformatThreadStacks(java.util.Map<java.lang.Thread,java.lang.StackTraceElement[]> threads)Dump threads and their current stack trace.private java.lang.StringformatThreadStacksFull()Dump 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 java.lang.StackTraceElement[]getStackTrace(java.lang.Thread t)private java.util.HashSet<java.lang.Thread>getThreads(ThreadFilter... filters)Returns allThreadLeakGroupthreads for analysis.private java.util.HashMap<java.lang.Thread,java.lang.StackTraceElement[]>getThreadsWithTraces(ThreadFilter... filters)Returns allThreadLeakGroupapplicable threads, with stack traces, for analysis.private ThreadFilterinstantiateFilters(java.util.List<java.lang.Throwable> errors, java.lang.Class<?> suiteClass)Instantiate a full set ofThreadFilters for a suite.(package private) booleanisTimedOut()(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(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.StringthreadNames(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.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
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 ofThreadFilters 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 allThreadLeakGroupapplicable threads, with stack traces, for analysis.
-
getThreads
private java.util.HashSet<java.lang.Thread> getThreads(ThreadFilter... filters)
Returns allThreadLeakGroupthreads 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 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
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.
-
-