Skip to content

Commit be4cdb2

Browse files
committed
Merge pull request #962 from chrisvest/thread-start-timeout
Do not include thread start time in test timeout measurements.
2 parents e8b6558 + f17e067 commit be4cdb2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.lang.management.ThreadMXBean;
55
import java.util.Arrays;
66
import java.util.concurrent.Callable;
7+
import java.util.concurrent.CountDownLatch;
78
import java.util.concurrent.ExecutionException;
89
import java.util.concurrent.FutureTask;
910
import java.util.concurrent.TimeUnit;
@@ -37,11 +38,13 @@ public FailOnTimeout(Statement originalStatement, long timeout, TimeUnit unit, b
3738

3839
@Override
3940
public void evaluate() throws Throwable {
40-
FutureTask<Throwable> task = new FutureTask<Throwable>(new CallableStatement());
41+
CallableStatement callable = new CallableStatement();
42+
FutureTask<Throwable> task = new FutureTask<Throwable>(callable);
4143
threadGroup = new ThreadGroup("FailOnTimeoutGroup");
4244
Thread thread = new Thread(threadGroup, task, "Time-limited test");
4345
thread.setDaemon(true);
4446
thread.start();
47+
callable.awaitStarted();
4548
Throwable throwable = getResult(task, thread);
4649
if (throwable != null) {
4750
throw throwable;
@@ -204,8 +207,11 @@ private long cpuTime (Thread thr) {
204207
}
205208

206209
private class CallableStatement implements Callable<Throwable> {
210+
private final CountDownLatch startLatch = new CountDownLatch(1);
211+
207212
public Throwable call() throws Exception {
208213
try {
214+
startLatch.countDown();
209215
originalStatement.evaluate();
210216
} catch (Exception e) {
211217
throw e;
@@ -214,5 +220,9 @@ public Throwable call() throws Exception {
214220
}
215221
return null;
216222
}
223+
224+
public void awaitStarted() throws InterruptedException {
225+
startLatch.await();
226+
}
217227
}
218228
}

0 commit comments

Comments
 (0)