Skip to content

Commit 21898b1

Browse files
committed
Merge pull request #765 from adam-beneschan/742
changes to address kcooney's concerns about thread safety on issue #742
2 parents 86fbf45 + c1895d1 commit 21898b1

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class FailOnTimeout extends Statement {
1717
private final TimeUnit fTimeUnit;
1818
private final long fTimeout;
1919
private final boolean fLookForStuckThread;
20-
private ThreadGroup fThreadGroup = null;
20+
private volatile ThreadGroup fThreadGroup = null;
2121

2222
public FailOnTimeout(Statement originalStatement, long millis) {
2323
this(originalStatement, millis, TimeUnit.MILLISECONDS);

src/main/java/org/junit/rules/Timeout.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class Timeout implements TestRule {
3737
private final long fTimeout;
3838
private final TimeUnit fTimeUnit;
39-
private boolean fLookForStuckThread;
39+
private final boolean fLookForStuckThread;
4040

4141
/**
4242
* Create a {@code Timeout} instance with the timeout specified
@@ -70,6 +70,20 @@ public Timeout(long timeout, TimeUnit unit) {
7070
fLookForStuckThread = false;
7171
}
7272

73+
/**
74+
* Create a {@code Timeout} instance with the same fields as {@code t}
75+
* except for {@code fLookForStuckThread}.
76+
*
77+
* @param t the {@code Timeout} instance to copy
78+
* @param lookForStuckThread whether to look for a stuck thread
79+
* @since 4.12
80+
*/
81+
protected Timeout(Timeout t, boolean lookForStuckThread) {
82+
fTimeout = t.fTimeout;
83+
fTimeUnit = t.fTimeUnit;
84+
fLookForStuckThread = lookForStuckThread;
85+
}
86+
7387
/**
7488
* @param millis the timeout in milliseconds
7589
* @since 4.12
@@ -95,12 +109,11 @@ public static Timeout seconds(long seconds) {
95109
* @return This object
96110
* @since 4.12
97111
*/
98-
public Timeout lookForStuckThread(boolean enable) {
99-
fLookForStuckThread = enable;
100-
return this;
112+
public Timeout lookingForStuckThread(boolean enable) {
113+
return new Timeout(this, enable);
101114
}
102115

103116
public Statement apply(Statement base, Description description) {
104117
return new FailOnTimeout(base, fTimeout, fTimeUnit, fLookForStuckThread);
105118
}
106-
}
119+
}

src/test/java/org/junit/tests/running/methods/TimeoutTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void failure(boolean mainThreadStalls) throws Exception {
199199

200200
public static class InfiniteLoopWithStuckThreadTest {
201201
@Rule
202-
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookForStuckThread(true);
202+
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookingForStuckThread(true);
203203

204204
@Test
205205
public void failure() throws Exception {
@@ -209,7 +209,7 @@ public void failure() throws Exception {
209209

210210
public static class InfiniteLoopStuckInMainThreadTest {
211211
@Rule
212-
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookForStuckThread(true);
212+
public TestRule globalTimeout = new Timeout(100, TimeUnit.MILLISECONDS).lookingForStuckThread(true);
213213

214214
@Test
215215
public void failure() throws Exception {

0 commit comments

Comments
 (0)