Class ThreadLeakControl

java.lang.Object
com.carrotsearch.randomizedtesting.ThreadLeakControl

class ThreadLeakControl extends 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 Details

    • LOGGER

      private static final 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 Set<Thread> expectedSuiteState
      This is the assumed set of threads without leaks.
    • notifierLock

      private final Object notifierLock
      Atomic section for passing notifier events.
    • subNotifier

      private final ThreadLeakControl.SubNotifier subNotifier
      See Also:
    • testTimeout

      private ThreadLeakControl.TimeoutValue testTimeout
      Test timeout.
    • suiteTimeout

      private ThreadLeakControl.TimeoutValue suiteTimeout
      Suite timeout.
    • builtinFilters

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

      private ThreadFilter suiteFilters
      User filter (compound).
    • runner

      private final RandomizedRunner runner
      The governing runner.
    • suiteTimedOut

      private AtomicBoolean suiteTimedOut
      Suite timeout.
    • threadLeakGroup

      ThreadLeakGroup threadLeakGroup
      Thread leak detection group.
  • Constructor Details

    • ThreadLeakControl

      ThreadLeakControl(org.junit.runner.notification.RunNotifier notifier, RandomizedRunner runner)
  • Method Details

    • 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)
      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 Set<Thread> refilter(Set<Thread> in, ThreadFilter f)
      Refilter a set of threads
    • instantiateFilters

      private ThreadFilter instantiateFilters(List<Throwable> errors, Class<?> suiteClass)
      Instantiate a full set of ThreadFilters for a suite.
    • emptyStack

      private static <T extends Throwable> T emptyStack(T t)
      Clears a Throwable'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

      private String formatThreadStacks(Map<Thread,StackTraceElement[]> threads)
      Dump threads and their current stack trace.
    • threadNames

      private String threadNames(Collection<Thread> threads)
      Collect thread names.
    • formatThreadStacksFull

      private String formatThreadStacksFull()
      Dump thread state.
    • getStackTrace

      private static StackTraceElement[] getStackTrace(Thread t)
    • getThreadsWithTraces

      private HashMap<Thread,StackTraceElement[]> getThreadsWithTraces(ThreadFilter... filters)
      Returns all ThreadLeakGroup applicable threads, with stack traces, for analysis.
    • getThreads

      private HashSet<Thread> getThreads(ThreadFilter... filters)
      Returns all ThreadLeakGroup threads for analysis.
    • tryToInterruptAll

      private Set<Thread> tryToInterruptAll(List<Throwable> errors, Set<Thread> threads)
      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 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(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 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.