org.junit.rules
Class Timeout

java.lang.Object
  extended by org.junit.rules.Timeout
All Implemented Interfaces:
TestRule

public class Timeout
extends Object
implements TestRule

The Timeout Rule applies the same timeout to all test methods in a class:

 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.

A specified timeout of 0 will be interpreted as not set, however tests will still launch from separate threads. This can be useful for disabling timeouts in environments where they are dynamically set based on some property.

Since:
4.7

Constructor Summary
  Timeout(int millis)
          Deprecated. 
  Timeout(long timeout, TimeUnit timeUnit)
          Create a Timeout instance with the timeout specified at the timeUnit of granularity of the provided TimeUnit.
protected Timeout(Timeout t, boolean lookForStuckThread)
          Create a Timeout instance with the same fields as t except for lookForStuckThread.
 
Method Summary
 Statement apply(Statement base, Description description)
          Modifies the method-running Statement to implement this test-running rule.
 Timeout lookingForStuckThread(boolean enable)
          Specifies whether to look for a stuck thread.
static Timeout millis(long millis)
           
static Timeout seconds(long seconds)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Timeout

@Deprecated
public Timeout(int millis)
Deprecated. 

Create a 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).

Parameters:
millis - the maximum time in milliseconds to allow the test to run before it should timeout

Timeout

public Timeout(long timeout,
               TimeUnit timeUnit)
Create a Timeout instance with the timeout specified at the timeUnit of granularity of the provided TimeUnit.

Parameters:
timeout - the maximum time to allow the test to run before it should timeout
timeUnit - the time unit for the timeout
Since:
4.12

Timeout

protected Timeout(Timeout t,
                  boolean lookForStuckThread)
Create a Timeout instance with the same fields as t except for lookForStuckThread.

Parameters:
t - the Timeout instance to copy
lookForStuckThread - whether to look for a stuck thread
Since:
4.12
Method Detail

millis

public static Timeout millis(long millis)
Parameters:
millis - the timeout in milliseconds
Since:
4.12

seconds

public static Timeout seconds(long seconds)
Parameters:
seconds - the timeout in seconds
Since:
4.12

lookingForStuckThread

public Timeout lookingForStuckThread(boolean enable)
Specifies whether to look for a stuck thread. If a timeout occurs and this feature is enabled, the test will look for a thread that appears to be stuck and dump its backtrace. This feature is experimental. Behavior may change after the 4.12 release in response to feedback.

Parameters:
enable - true to enable the feature
Returns:
This object
Since:
4.12

apply

public Statement apply(Statement base,
                       Description description)
Description copied from interface: TestRule
Modifies the method-running Statement to implement this test-running rule.

Specified by:
apply in interface TestRule
Parameters:
base - The Statement to be modified
description - A Description of the test implemented in base
Returns:
a new statement, which may be the same as base, a wrapper around base, or a completely new Statement.


Copyright © 2002–2014 JUnit. All rights reserved.