Skip to content

[Spring] Support multithreaded execution of scenarios #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [2.0.0-SNAPSHOT](https://github.com/cucumber/cucumber-jvm/compare/v1.2.5...master) (In Git)

* [Spring] Support multithreaded execution of scenarios ([#1106](https://github.com/cucumber/cucumber-jvm/issues/1106), [#1107](https://github.com/cucumber/cucumber-jvm/issues/1107), [#1148](https://github.com/cucumber/cucumber-jvm/issues/1148) Ismail Bhana, M.P. Korstanje)
* [Java8, Kotlin Java8] Support java 8 method references ([#1140](https://github.com/cucumber/cucumber-jvm/pull/1140) M.P. Korstanje)
* [Core] Show explicit error message when field name missed in table header ([#1014](https://github.com/cucumber/cucumber-jvm/pull/1014) Mykola Gurov)
* [Examples] Properly quit selenium in webbit examples ([#1146](https://github.com/cucumber/cucumber-jvm/pull/1146) Alberto Scotto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
import java.util.Map;

class GlueCodeContext {
public static final GlueCodeContext INSTANCE = new GlueCodeContext();

private static final ThreadLocal<GlueCodeContext> localContext = new ThreadLocal<GlueCodeContext>() {
protected GlueCodeContext initialValue() {
return new GlueCodeContext();
}
};

private final Map<String, Object> objects = new HashMap<String, Object>();
private final Map<String, Runnable> callbacks = new HashMap<String, Runnable>();
private int counter;

private GlueCodeContext() {
}

public static GlueCodeContext getInstance() {
return localContext.get();
}

public void start() {
cleanUp();
counter++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
class GlueCodeScope implements Scope {
public static final String NAME = "cucumber-glue";

private final GlueCodeContext context = GlueCodeContext.INSTANCE;

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
GlueCodeContext context = GlueCodeContext.getInstance();
Object obj = context.get(name);
if (obj == null) {
obj = objectFactory.getObject();
Expand All @@ -21,11 +20,13 @@ public Object get(String name, ObjectFactory<?> objectFactory) {

@Override
public Object remove(String name) {
GlueCodeContext context = GlueCodeContext.getInstance();
return context.remove(name);
}

@Override
public void registerDestructionCallback(String name, Runnable callback) {
GlueCodeContext context = GlueCodeContext.getInstance();
context.registerDestructionCallback(name, callback);
}

Expand All @@ -36,6 +37,7 @@ public Object resolveContextualObject(String key) {

@Override
public String getConversationId() {
GlueCodeContext context = GlueCodeContext.getInstance();
return context.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void start() {
registerStepClassBeanDefinition(beanFactory, stepClass);
}
}
GlueCodeContext.INSTANCE.start();
GlueCodeContext.getInstance().start();
}

@SuppressWarnings("resource")
Expand Down Expand Up @@ -161,7 +161,7 @@ private void registerStepClassBeanDefinition(ConfigurableListableBeanFactory bea
@Override
public void stop() {
notifyContextManagerAboutTestClassFinished();
GlueCodeContext.INSTANCE.stop();
GlueCodeContext.getInstance().stop();
}

private void notifyContextManagerAboutTestClassFinished() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cucumber.runtime.java.spring.threading;

import static java.util.concurrent.Executors.newFixedThreadPool;
import static org.junit.Assert.assertEquals;

import cucumber.api.cli.Main;
import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;


public class RunParallelCukesTest {

private final Callable<Byte> runCuke = new Callable<Byte>() {
@Override
public Byte call() throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String[] args = {
"--glue", "cucumber.runtime.java.spring.threading",
"classpath:cucumber/runtime/java/spring/threadingCukes.feature",
"--strict"
};
return Main.run(args, classLoader);
}
};

@Test
public void test() throws InterruptedException, ExecutionException {
ExecutorService executorService = newFixedThreadPool(2);
Future<Byte> result1 = executorService.submit(runCuke);
Future<Byte> result2 = executorService.submit(runCuke);
assertEquals(result1.get().byteValue(), 0x0);
assertEquals(result2.get().byteValue(), 0x0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cucumber.runtime.java.spring.threading;

import static java.lang.Thread.currentThread;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@WebAppConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class ThreadingStepDefs {

private static final ConcurrentHashMap<Thread, ThreadingStepDefs> map = new ConcurrentHashMap<Thread, ThreadingStepDefs>();

private static final CountDownLatch latch = new CountDownLatch(2);

@Given("^I am a step definition$")
public void iAmAStepDefinition() throws Throwable {
map.put(currentThread(), this);
}

@When("^when executed in parallel$")
public void whenExecutedInParallel() throws Throwable {
latch.await(10, TimeUnit.SECONDS);
}

@Then("^I should not be shared between threads$")
public void iShouldNotBeSharedBetweenThreads() throws Throwable {
for (Map.Entry<Thread, ThreadingStepDefs> entries : map.entrySet()) {
if (entries.getKey().equals(currentThread())) {
assertSame(entries.getValue(), this);
} else {
assertNotSame(entries.getValue(), this);
}
}
assertEquals(2, map.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Spring Threading Cukes
In order to have a completely clean system for each scenario
As a purity activist
I want that beans have both scenario and thread scope.

Scenario: A parallel execution
Given I am a step definition
When when executed in parallel
Then I should not be shared between threads