Skip to content

Commit b2ef05c

Browse files
author
Bradley Hart
committed
Added test for GroovyBackend to ensure the ThreadLocal is in place
1 parent 6fbe602 commit b2ef05c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cucumber.runtime.groovy;
2+
3+
import cucumber.runtime.io.ResourceLoader;
4+
import groovy.lang.Closure;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mock;
8+
import org.mockito.runners.MockitoJUnitRunner;
9+
10+
11+
@RunWith(MockitoJUnitRunner.class)
12+
public class ParallelTest {
13+
@Mock
14+
ResourceLoader resourceLoader;
15+
@Mock
16+
Closure closure;
17+
18+
@Test(expected = RuntimeException.class)
19+
public void exception_throw_when_world_already_set_on_same_thread() {
20+
GroovyBackend groovyBackend = new GroovyBackend(resourceLoader);
21+
groovyBackend.registerWorld(closure);
22+
groovyBackend.registerWorld(closure);
23+
}
24+
25+
@Test
26+
public void can_have_a_new_backend_on_a_different_thread() {
27+
new GroovyBackend(resourceLoader);
28+
Thread interactWithBackendThread = new Thread(new Runnable(){
29+
@Override
30+
public void run() {
31+
try {
32+
GroovyBackend.getInstance().registerWorld(closure);
33+
} catch (NullPointerException e){
34+
// This is what we want as there should be no GroovyBackend on this thread
35+
}
36+
}
37+
});
38+
runAndWait(interactWithBackendThread);
39+
GroovyBackend.getInstance().registerWorld(closure);
40+
}
41+
42+
private void runAndWait(Thread interactWithBackendThread) {
43+
interactWithBackendThread.start();
44+
try {
45+
interactWithBackendThread.join();
46+
} catch (InterruptedException e) {
47+
throw new RuntimeException("Doh");
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)