|
| 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