[JVM] Race condition in Cucumber during parallel testing

633 views
Skip to first unread message

Ganggang Hu Guidice

unread,
May 1, 2014, 10:06:01 PM5/1/14
to cu...@googlegroups.com

I create a Singleton class and run parallel testing using Maven.
I want the Singleton class to be instantiated only once. However, it is created every time when a test process starts.
I use the keyword "synchronized" in Singleton class but it only prevent race condition within one test, not parallel tests.

Is it possible to create a cross process Singleton in Cucumber JVM? Thanks a lot for your help.

Here is the Singleton class definition:
singleton

Step definition:
stepdef

Feature definition:

feature

And two test files start at the same time, each test is as follows:

test1

Here is the result, as you can see, the Singleton object is created twice, one for each test run:

result

Paolo Ambrosio

unread,
May 2, 2014, 2:09:54 AM5/2/14
to cu...@googlegroups.com
On Fri, May 2, 2014 at 3:06 AM, Ganggang Hu Guidice <ganggang...@gmail.com> wrote:

I create a Singleton class and run parallel testing using Maven.
I want the Singleton class to be instantiated only once. However, it is created every time when a test process starts.
I use the keyword "synchronized" in Singleton class but it only prevent race condition within one test, not parallel tests.

Is it possible to create a cross process Singleton in Cucumber JVM? Thanks a lot for your help.

Here is the Singleton class definition:
singleton

Step definition:
stepdef

Feature definition:

feature

And two test files start at the same time,


How do you start two tests at the same time? If each test run is using a different JVM process, that is expected behaviour and it is not related to Cucumber.

each test is as follows:

test1

Here is the result, as you can see, the Singleton object is created twice, one for each test run:

result

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ganggang Hu Guidice

unread,
May 5, 2014, 3:09:39 PM5/5/14
to cu...@googlegroups.com


On Friday, May 2, 2014 2:09:54 AM UTC-4, Paolo Ambrosio wrote:
On Fri, May 2, 2014 at 3:06 AM, Ganggang Hu Guidice <ganggang...@gmail.com> wrote:

I create a Singleton class and run parallel testing using Maven.
I want the Singleton class to be instantiated only once. However, it is created every time when a test process starts.
I use the keyword "synchronized" in Singleton class but it only prevent race condition within one test, not parallel tests.

Is it possible to create a cross process Singleton in Cucumber JVM? Thanks a lot for your help.

Here is the Singleton class definition:
singleton

Step definition:
stepdef

Feature definition:

feature

And two test files start at the same time,


How do you start two tests at the same time? If each test run is using a different JVM process, that is expected behaviour and it is not related to Cucumber.

Actually I want to create a global variable/object in parent process in Cucumber, it should be visible to all child processes so each child process can access it. i.e.

Parent process in Cucumber - how to create a global object here?
    -JVM process 1
    -JVM process 2
    - ......
    -JVM process n

Is there any way to do it?

David Kowis

unread,
May 5, 2014, 3:37:38 PM5/5/14
to cu...@googlegroups.com
On 05/05/2014 02:09 PM, Ganggang Hu Guidice wrote:
> Actually I want to create a global variable/object in parent process in
> Cucumber, it should be visible to all child processes so each child
> process can access it. i.e.
>
> Parent process in Cucumber - how to create a global object here?
> -JVM process 1
> -JVM process 2
> - ......
> -JVM process n
>
> Is there any way to do it?

Certainly, but it'll be outside of the scope of Cucumber. It's not a
cucumber specific solution.

I suggest you look into finding ways to cluster JVMs so that the data
can be shared. Perhaps Hazelcast would provide you a simple solution to
have a multiple JMV aware data structure.

It's not something that's Cucumber specific, however, I think you'd have
better luck looking via google and other avenues.

--
David Kowis

Ganggang Hu Guidice

unread,
May 6, 2014, 7:33:53 AM5/6/14
to cu...@googlegroups.com

 Thanks a lot for your suggestion. I will look at Hazelcast and other solutions.

--
David Kowis

Ganggang Hu Guidice

unread,
May 6, 2014, 9:48:51 AM5/6/14
to cu...@googlegroups.com


On Monday, May 5, 2014 3:37:38 PM UTC-4, David Kowis wrote:

Also, as Cucumber has @before hook in each scenario, is there any global @before hook can be executed before all parallel scenarios in Cucumber process?

I'm just trying to understand if there is a solution inside Cucumber before look for other ways. Thanks

Leslie Brooks

unread,
Jun 12, 2014, 8:18:04 PM6/12/14
to cu...@googlegroups.com
I think this is a problem that anyone running tests in parallel is likely to have. If the child processes need to coordinate or communicate, then communicating via the parent (Cucumber) seems like a good approach. A global 'Before' hook would be useful for that. Does Cucumber have a global 'Before' hook, or any clean way of creating one?

Jason Smiley

unread,
Sep 15, 2014, 5:55:39 PM9/15/14
to cu...@googlegroups.com
Not sure if this helps, but I run tests in parallel Threads on the OS level (more chron jobs using different commands to select the suites).

Jason Smiley

unread,
Sep 15, 2014, 5:58:12 PM9/15/14
to cu...@googlegroups.com
Ah, yeah, the way you have your code implemented, this seems to be expected.

Basically, each thread is its "own version of the code". So it makes sense that each thread has its own singleton.

You need to give the threads scope of each other. There are annotations in java for this, but, for me, I just made it so each thread can run indepently. 

Jason Smiley

unread,
Sep 15, 2014, 6:05:30 PM9/15/14
to cu...@googlegroups.com
Sorry for so many posts - its not letting me Edit!!!!

One last thing I wanted to say about using OS to run threads in parallel. You will have to merge the reports since each thread will only write to 1 report.

I use Jenkins to combine artifacts for me and Jenkins Plugins to merge report files (JSON, XML, HTML, etc.)


On Thursday, May 1, 2014 10:06:01 PM UTC-4, Ganggang Hu Guidice wrote:
Reply all
Reply to author
Forward
0 new messages