From b2ef05cec08cb5f82a0caf45f920f057b4e116ca Mon Sep 17 00:00:00 2001 From: Bradley Hart Date: Mon, 9 Jun 2014 15:41:04 +0100 Subject: [PATCH] Added test for GroovyBackend to ensure the ThreadLocal is in place --- .../cucumber/runtime/groovy/ParallelTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java diff --git a/groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java b/groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java new file mode 100644 index 0000000000..8e8b6225f5 --- /dev/null +++ b/groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java @@ -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"); + } + } + +}