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 class
FailOnTimeout.Builder
Builder forFailOnTimeout
.private class
FailOnTimeout.CallableStatement
-
Field Summary
Fields Modifier and Type Field Description private boolean
lookForStuckThread
private Statement
originalStatement
private java.lang.ThreadGroup
threadGroup
private long
timeout
private java.util.concurrent.TimeUnit
timeUnit
-
Constructor Summary
Constructors Modifier Constructor Description private
FailOnTimeout(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.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 firstcount
Threads inthreads
.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 aThrowable
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, ornull
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.
-
-
-
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.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.Throwable
Description copied from class:Statement
Run the action, throwing aThrowable
if anything goes wrong.
-
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, ornull
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 byevaluate()
- Returns:
- The thread which appears to be causing the problem, if different from
mainThread
, ornull
if the main thread appears to be the problem or if the thread cannot be determined. The return value is never equal tomainThread
.
-
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 firstcount
Threads inthreads
. (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.
-
-