Skip to content

Commit

Permalink
Added test for GroovyBackend to ensure the ThreadLocal is in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Bradley Hart committed Jun 9, 2014
1 parent 6fbe602 commit b2ef05c
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cucumber.runtime.groovy;

import cucumber.runtime.io.ResourceLoader;
import groovy.lang.Closure;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class ParallelTest {
@Mock
ResourceLoader resourceLoader;
@Mock
Closure closure;

@Test(expected = RuntimeException.class)
public void exception_throw_when_world_already_set_on_same_thread() {
GroovyBackend groovyBackend = new GroovyBackend(resourceLoader);
groovyBackend.registerWorld(closure);
groovyBackend.registerWorld(closure);
}

@Test
public void can_have_a_new_backend_on_a_different_thread() {
new GroovyBackend(resourceLoader);
Thread interactWithBackendThread = new Thread(new Runnable(){
@Override
public void run() {
try {
GroovyBackend.getInstance().registerWorld(closure);
} catch (NullPointerException e){
// This is what we want as there should be no GroovyBackend on this thread
}
}
});
runAndWait(interactWithBackendThread);
GroovyBackend.getInstance().registerWorld(closure);
}

private void runAndWait(Thread interactWithBackendThread) {
interactWithBackendThread.start();
try {
interactWithBackendThread.join();
} catch (InterruptedException e) {
throw new RuntimeException("Doh");
}
}

}

0 comments on commit b2ef05c

Please sign in to comment.