Class FailOnTimeout


  • public class FailOnTimeout
    extends Statement
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static FailOnTimeout.Builder builder()
      Returns a new builder for building an instance.
      private java.lang.Thread[] copyThreads​(java.lang.Thread[] threads, int count)
      Returns an array of the first count Threads in threads.
      private long cpuTime​(java.lang.Thread thr)
      Returns the CPU time used by a thread, if possible.
      private java.lang.Exception createTimeoutException​(java.lang.Thread thread)  
      void evaluate()
      Run the action, throwing a Throwable if anything goes wrong.
      private java.lang.Throwable getResult​(java.util.concurrent.FutureTask<java.lang.Throwable> task, java.lang.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.
      private java.lang.StackTraceElement[] getStackTrace​(java.lang.Thread thread)
      Retrieves the stack trace for a given thread.
      private java.lang.Thread getStuckThread​(java.lang.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).
      private java.lang.Thread[] getThreadArray​(java.lang.ThreadGroup group)
      Returns all active threads belonging to a thread group.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • originalStatement

        private final Statement originalStatement
      • timeUnit

        private final java.util.concurrent.TimeUnit timeUnit
      • timeout

        private final long timeout
      • lookForStuckThread

        private final boolean lookForStuckThread
      • threadGroup

        private volatile java.lang.ThreadGroup threadGroup
    • Constructor Detail

      • 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
    • Method Detail

      • builder

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

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

        private java.lang.Throwable getResult​(java.util.concurrent.FutureTask<java.lang.Throwable> task,
                                              java.lang.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 java.lang.Exception createTimeoutException​(java.lang.Thread thread)
      • getStackTrace

        private java.lang.StackTraceElement[] getStackTrace​(java.lang.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 java.lang.Thread getStuckThread​(java.lang.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.
      • getThreadArray

        private java.lang.Thread[] getThreadArray​(java.lang.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 null if this cannot be determined, e.g. because new threads are being created at an extremely fast rate.
      • copyThreads

        private java.lang.Thread[] copyThreads​(java.lang.Thread[] threads,
                                               int count)
        Returns an array of the first count Threads in threads. (Use instead of Arrays.copyOf to maintain compatibility with Java 1.5.)
        Parameters:
        threads - The source array.
        count - The maximum length of the result array.
        Returns:
        The first (at most) elements of threads.
      • cpuTime

        private long cpuTime​(java.lang.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.