Class 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.
    • 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.
      • builtinFilters

        private final java.util.List<ThreadFilter> builtinFilters
        Built-in filters.
      • suiteFilters

        private ThreadFilter suiteFilters
        User filter (compound).
      • 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

      • 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)
        A Statement for wrapping suite-level execution.
      • forTest

        org.junit.runners.model.Statement forTest​(org.junit.runners.model.Statement s,
                                                  RandomizedRunner.TestCandidate c)
        A Statement 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 of ThreadFilters for a suite.
      • emptyStack

        private static <T extends java.lang.Throwable> T emptyStack​(T t)
        Clears a Throwable'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 all ThreadLeakGroup applicable threads, with stack traces, for analysis.
      • getThreads

        private java.util.HashSet<java.lang.Thread> getThreads​(ThreadFilter... filters)
        Returns all ThreadLeakGroup 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 the RunNotifier 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.