public class Timeout extends Object implements TestRule
public static class HasGlobalLongTimeout { @Rule public Timeout globalTimeout= new Timeout(20); @Test public void run1() throws InterruptedException { Thread.sleep(100); } @Test public void infiniteLoop() { while (true) {} } }Each test is run in a new thread. If the specified timeout elapses before the test completes, its execution is interrupted via
Thread.interrupt()
.
This happens in interruptable I/O and locks, and methods in Object
and Thread
throwing InterruptedException
.Constructor and Description |
---|
Timeout(int millis)
Deprecated.
|
Timeout(long timeout,
TimeUnit unit)
Create a
Timeout instance with the timeout specified
at the unit of granularity of the provided TimeUnit . |
@Deprecated public Timeout(int millis)
Timeout
instance with the timeout specified
in milliseconds.
This constructor is deprecated.
Instead use Timeout(long, java.util.concurrent.TimeUnit)
,
millis(long)
, or seconds(long)
.
millis
- the maximum time in milliseconds to allow the
test to run before it should timeoutpublic Timeout(long timeout, TimeUnit unit)
Timeout
instance with the timeout specified
at the unit of granularity of the provided TimeUnit
.timeout
- the maximum time to allow the test to run
before it should timeoutunit
- the time unit for the timeout
public static Timeout millis(long millis)
millis
- the timeout in millisecondspublic static Timeout seconds(long seconds)
seconds
- the timeout in secondspublic Statement apply(Statement base, Description description)
TestRule
Statement
to implement this
test-running rule.apply
in interface TestRule
base
- The Statement
to be modifieddescription
- A Description
of the test implemented in base
base
,
a wrapper around base
, or a completely new Statement.