Skip to content

Commit

Permalink
changes to address kcooney's concerns about thread safety on issue ju…
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Beneschan committed Nov 15, 2013
1 parent a14fc5f commit c1895d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Expand Up @@ -17,7 +17,7 @@ public class FailOnTimeout extends Statement {
private final TimeUnit fTimeUnit;
private final long fTimeout;
private final boolean fLookForStuckThread;
private ThreadGroup fThreadGroup = null;
private volatile ThreadGroup fThreadGroup = null;

public FailOnTimeout(Statement originalStatement, long millis) {
this(originalStatement, millis, TimeUnit.MILLISECONDS);
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/org/junit/rules/Timeout.java
Expand Up @@ -36,7 +36,7 @@
public class Timeout implements TestRule {
private final long fTimeout;
private final TimeUnit fTimeUnit;
private boolean fLookForStuckThread;
private final boolean fLookForStuckThread;

/**
* Create a {@code Timeout} instance with the timeout specified
Expand Down Expand Up @@ -70,6 +70,20 @@ public Timeout(long timeout, TimeUnit unit) {
fLookForStuckThread = false;
}

/**
* Create a {@code Timeout} instance with the same fields as {@code t}
* except for {@code fLookForStuckThread}.
*
* @param t the {@code Timeout} instance to copy
* @param lookForStuckThread whether to look for a stuck thread
* @since 4.12
*/
protected Timeout(Timeout t, boolean lookForStuckThread) {
fTimeout = t.fTimeout;
fTimeUnit = t.fTimeUnit;
fLookForStuckThread = lookForStuckThread;
}

/**
* @param millis the timeout in milliseconds
* @since 4.12
Expand All @@ -95,12 +109,11 @@ public static Timeout seconds(long seconds) {
* @return This object
* @since 4.12
*/
public Timeout lookForStuckThread(boolean enable) {
fLookForStuckThread = enable;
return this;
public Timeout lookingForStuckThread(boolean enable) {
return new Timeout(this, enable);
}

public Statement apply(Statement base, Description description) {
return new FailOnTimeout(base, fTimeout, fTimeUnit, fLookForStuckThread);
}
}
}
Expand Up @@ -199,7 +199,7 @@ public void failure(boolean mainThreadStalls) throws Exception {

public static class InfiniteLoopWithStuckThreadTest {
@Rule
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookForStuckThread(true);
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookingForStuckThread(true);

@Test
public void failure() throws Exception {
Expand All @@ -209,7 +209,7 @@ public void failure() throws Exception {

public static class InfiniteLoopStuckInMainThreadTest {
@Rule
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookForStuckThread(true);
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookingForStuckThread(true);

@Test
public void failure() throws Exception {
Expand Down

0 comments on commit c1895d1

Please sign in to comment.