Skip to content

Commit 2ac08a8

Browse files
committed
Merge cucumber#606. Update History.md
2 parents f241755 + 0a9edb6 commit 2ac08a8

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [1-1-6-SNAPSHOT (Git master)](https://github.com/cucumber/cucumber-jvm/compare/v1.1.5...master)
22

3+
* [Core] Fix stop watch thread safety ([#606](https://github.com/cucumber/cucumber-jvm/pull/606) Dave Bassan)
34
* [Android] Fix Cucumber reports for cucumber-android ([#605](https://github.com/cucumber/cucumber-jvm/pull/605) Frieder Bluemle)
45
* [Spring] Fix for tests annotated with @ContextHierarchy ([#590](https://github.com/cucumber/cucumber-jvm/pull/590) Martin Lau)
56
* [Core] Add error check to scenario outline, add java snippet escaping for `+` and `.` ([#596](https://github.com/cucumber/cucumber-jvm/pull/596) Guy Burton)

core/src/main/java/cucumber/runtime/StopWatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ public interface StopWatch {
99
long stop();
1010

1111
StopWatch SYSTEM = new StopWatch() {
12-
public Long start;
12+
public ThreadLocal<Long> start = new ThreadLocal<Long>();
1313

1414
@Override
1515
public void start() {
16-
start = System.nanoTime();
16+
start.set(System.nanoTime());
1717
}
1818

1919
@Override
2020
public long stop() {
21-
Long duration = System.nanoTime() - start;
22-
start = null;
21+
Long duration = System.nanoTime() - start.get();
22+
start.set(null);
2323
return duration;
2424
}
2525
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cucumber.runtime;
2+
3+
import static org.junit.Assert.assertNull;
4+
5+
import org.junit.Test;
6+
7+
public class StopWatchTest {
8+
private final StopWatch stopWatch = StopWatch.SYSTEM;
9+
private Throwable exception;
10+
11+
@Test
12+
public void should_be_thread_safe() {
13+
try {
14+
Thread timerThreadOne = new TimerThread(500L);
15+
Thread timerThreadTwo = new TimerThread(750L);
16+
17+
timerThreadOne.start();
18+
timerThreadTwo.start();
19+
20+
timerThreadOne.join();
21+
timerThreadTwo.join();
22+
23+
assertNull("null_pointer_exception", exception);
24+
} catch (InterruptedException e) {
25+
Thread.currentThread().interrupt();
26+
}
27+
}
28+
29+
class TimerThread extends Thread {
30+
private final long timeoutMillis;
31+
32+
public TimerThread(long timeoutMillis) {
33+
this.timeoutMillis = timeoutMillis;
34+
}
35+
36+
@Override
37+
public void run() {
38+
try {
39+
stopWatch.start();
40+
Thread.sleep(timeoutMillis);
41+
stopWatch.stop();
42+
} catch (NullPointerException e) {
43+
exception = e;
44+
} catch (InterruptedException e) {
45+
Thread.currentThread().interrupt();
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)