|
| 1 | +package cucumber.runtime.java.spring.threading; |
| 2 | + |
| 3 | +import static java.lang.Thread.currentThread; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotSame; |
| 6 | +import static org.junit.Assert.assertSame; |
| 7 | + |
| 8 | +import cucumber.api.java.en.Given; |
| 9 | +import cucumber.api.java.en.Then; |
| 10 | +import cucumber.api.java.en.When; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.web.WebAppConfiguration; |
| 13 | + |
| 14 | +import java.util.Map; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +@WebAppConfiguration |
| 20 | +@ContextConfiguration("classpath:cucumber.xml") |
| 21 | +public class ThreadingStepDefs { |
| 22 | + |
| 23 | + private static final ConcurrentHashMap<Thread, ThreadingStepDefs> map = new ConcurrentHashMap<Thread, ThreadingStepDefs>(); |
| 24 | + |
| 25 | + private static final CountDownLatch latch = new CountDownLatch(2); |
| 26 | + |
| 27 | + @Given("^I am a step definition$") |
| 28 | + public void iAmAStepDefinition() throws Throwable { |
| 29 | + map.put(currentThread(), this); |
| 30 | + } |
| 31 | + |
| 32 | + @When("^when executed in parallel$") |
| 33 | + public void whenExecutedInParallel() throws Throwable { |
| 34 | + latch.await(10, TimeUnit.SECONDS); |
| 35 | + } |
| 36 | + |
| 37 | + @Then("^I should not be shared between threads$") |
| 38 | + public void iShouldNotBeSharedBetweenThreads() throws Throwable { |
| 39 | + for (Map.Entry<Thread, ThreadingStepDefs> entries : map.entrySet()) { |
| 40 | + if (entries.getKey().equals(currentThread())) { |
| 41 | + assertSame(entries.getValue(), this); |
| 42 | + } else { |
| 43 | + assertNotSame(entries.getValue(), this); |
| 44 | + } |
| 45 | + } |
| 46 | + assertEquals(2, map.size()); |
| 47 | + } |
| 48 | +} |
0 commit comments