Class FailOnTimeout
- java.lang.Object
-
- org.junit.runners.model.Statement
-
- org.junit.internal.runners.statements.FailOnTimeout
-
public class FailOnTimeout extends Statement
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFailOnTimeout.BuilderBuilder forFailOnTimeout.private classFailOnTimeout.CallableStatement
-
Field Summary
Fields Modifier and Type Field Description private booleanlookForStuckThreadprivate StatementoriginalStatementprivate longtimeoutprivate java.util.concurrent.TimeUnittimeUnit
-
Constructor Summary
Constructors Modifier Constructor Description privateFailOnTimeout(FailOnTimeout.Builder builder, Statement statement)FailOnTimeout(Statement statement, long timeoutMillis)Deprecated.usebuilder()instead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static FailOnTimeout.Builderbuilder()Returns a new builder for building an instance.private longcpuTime(java.lang.Thread thr)Returns the CPU time used by a thread, if possible.private java.lang.ExceptioncreateTimeoutException(java.lang.Thread thread)voidevaluate()Run the action, throwing aThrowableif anything goes wrong.private java.lang.ThrowablegetResult(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, ornullif the test passed.private java.lang.StackTraceElement[]getStackTrace(java.lang.Thread thread)Retrieves the stack trace for a given thread.private java.lang.ThreadgetStuckThread(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.util.List<java.lang.Thread>getThreadsInGroup(java.lang.ThreadGroup group)Returns all active threads belonging to a thread group.private java.lang.ThreadGroupthreadGroupForNewThread()
-
-
-
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
-
-
Constructor Detail
-
FailOnTimeout
@Deprecated public FailOnTimeout(Statement statement, long timeoutMillis)
Deprecated.usebuilder()instead.Creates an instance wrapping the given statement with the given timeout in milliseconds.- Parameters:
statement- the statement to wraptimeoutMillis- the timeout in milliseconds
-
FailOnTimeout
private FailOnTimeout(FailOnTimeout.Builder builder, Statement statement)
-
-
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.ThrowableDescription copied from class:StatementRun the action, throwing aThrowableif anything goes wrong.
-
threadGroupForNewThread
private java.lang.ThreadGroup threadGroupForNewThread()
-
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, ornullif 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 byevaluate()- Returns:
- The thread which appears to be causing the problem, if different from
mainThread, ornullif the main thread appears to be the problem or if the thread cannot be determined. The return value is never equal tomainThread.
-
getThreadsInGroup
private java.util.List<java.lang.Thread> getThreadsInGroup(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 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(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.
-
-