Class FailOnTimeout

java.lang.Object
org.junit.runners.model.Statement
org.junit.internal.runners.statements.FailOnTimeout

public class FailOnTimeout extends Statement
  • Field Details

    • originalStatement

      private final Statement originalStatement
    • timeUnit

      private final TimeUnit timeUnit
    • timeout

      private final long timeout
    • lookForStuckThread

      private final boolean lookForStuckThread
  • Constructor Details

    • FailOnTimeout

      @Deprecated public FailOnTimeout(Statement statement, long timeoutMillis)
      Deprecated.
      use builder() instead.
      Creates an instance wrapping the given statement with the given timeout in milliseconds.
      Parameters:
      statement - the statement to wrap
      timeoutMillis - the timeout in milliseconds
    • FailOnTimeout

      private FailOnTimeout(FailOnTimeout.Builder builder, Statement statement)
  • Method Details

    • builder

      public static FailOnTimeout.Builder builder()
      Returns a new builder for building an instance.
      Since:
      4.12
    • evaluate

      public void evaluate() throws Throwable
      Description copied from class: Statement
      Run the action, throwing a Throwable if anything goes wrong.
      Specified by:
      evaluate in class Statement
      Throws:
      Throwable
    • threadGroupForNewThread

      private ThreadGroup threadGroupForNewThread()
    • getResult

      private Throwable getResult(FutureTask<Throwable> task, Thread thread)
      Wait for the test task, returning the exception thrown by the test if the test failed, an exception indicating a timeout if the test timed out, or null if the test passed.
    • createTimeoutException

      private Exception createTimeoutException(Thread thread)
    • getStackTrace

      private StackTraceElement[] getStackTrace(Thread thread)
      Retrieves the stack trace for a given thread.
      Parameters:
      thread - The thread whose stack is to be retrieved.
      Returns:
      The stack trace; returns a zero-length array if the thread has terminated or the stack cannot be retrieved for some other reason.
    • getStuckThread

      private Thread getStuckThread(Thread mainThread)
      Determines whether the test appears to be stuck in some thread other than the "main thread" (the one created to run the test). This feature is experimental. Behavior may change after the 4.12 release in response to feedback.
      Parameters:
      mainThread - The main thread created by evaluate()
      Returns:
      The thread which appears to be causing the problem, if different from mainThread, or null if the main thread appears to be the problem or if the thread cannot be determined. The return value is never equal to mainThread.
    • getThreadsInGroup

      private List<Thread> getThreadsInGroup(ThreadGroup group)
      Returns all active threads belonging to a thread group.
      Parameters:
      group - The thread group.
      Returns:
      The active threads in the thread group. The result should be a complete list of the active threads at some point in time. Returns an empty list if this cannot be determined, e.g. because new threads are being created at an extremely fast rate.
    • cpuTime

      private long cpuTime(Thread thr)
      Returns the CPU time used by a thread, if possible.
      Parameters:
      thr - The thread to query.
      Returns:
      The CPU time used by thr, or 0 if it cannot be determined.