aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/Timeout.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/junit/rules/Timeout.java')
-rw-r--r--src/main/java/org/junit/rules/Timeout.java37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/main/java/org/junit/rules/Timeout.java b/src/main/java/org/junit/rules/Timeout.java
index 8d382df..334a923 100644
--- a/src/main/java/org/junit/rules/Timeout.java
+++ b/src/main/java/org/junit/rules/Timeout.java
@@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit;
* public static class HasGlobalLongTimeout {
*
* @Rule
- * public Timeout globalTimeout= new Timeout(20);
+ * public Timeout globalTimeout = Timeout.millis(20);
*
* @Test
* public void run1() throws InterruptedException {
@@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit;
public class Timeout implements TestRule {
private final long timeout;
private final TimeUnit timeUnit;
+ private final boolean lookForStuckThread;
/**
* Returns a new builder for building an instance.
@@ -79,10 +80,11 @@ public class Timeout implements TestRule {
public Timeout(long timeout, TimeUnit timeUnit) {
this.timeout = timeout;
this.timeUnit = timeUnit;
+ lookForStuckThread = false;
}
/**
- * Create a {@code Timeout} instance initialized with values form
+ * Create a {@code Timeout} instance initialized with values from
* a builder.
*
* @since 4.12
@@ -90,6 +92,7 @@ public class Timeout implements TestRule {
protected Timeout(Builder builder) {
timeout = builder.getTimeout();
timeUnit = builder.getTimeUnit();
+ lookForStuckThread = builder.getLookingForStuckThread();
}
/**
@@ -122,6 +125,16 @@ public class Timeout implements TestRule {
}
/**
+ * Gets whether this {@code Timeout} will look for a stuck thread
+ * when the test times out.
+ *
+ * @since 4.12
+ */
+ protected final boolean getLookingForStuckThread() {
+ return lookForStuckThread;
+ }
+
+ /**
* Creates a {@link Statement} that will run the given
* {@code statement}, and timeout the operation based
* on the values configured in this rule. Subclasses
@@ -133,6 +146,7 @@ public class Timeout implements TestRule {
Statement statement) throws Exception {
return FailOnTimeout.builder()
.withTimeout(timeout, timeUnit)
+ .withLookingForStuckThread(lookForStuckThread)
.build(statement);
}
@@ -191,6 +205,25 @@ public class Timeout implements TestRule {
}
/**
+ * Specifies whether to look for a stuck thread. If a timeout occurs and this
+ * feature is enabled, the rule 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.
+ *
+ * @param enable {@code true} to enable the feature
+ * @return {@code this} for method chaining.
+ */
+ public Builder withLookingForStuckThread(boolean enable) {
+ this.lookForStuckThread = enable;
+ return this;
+ }
+
+ protected boolean getLookingForStuckThread() {
+ return lookForStuckThread;
+ }
+
+
+ /**
* Builds a {@link Timeout} instance using the values in this builder.,
*/
public Timeout build() {