[ 
https://jira.codehaus.org/browse/SUREFIRE-869?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=301439#comment-301439
 ] 

Guillermo GARCIA OCHOA commented on SUREFIRE-869:
-------------------------------------------------

*I found the problem:*

Surefire use the _ForkStater.runSuitesForkPerTestSet()_ method to create the 
forks. Its create a thread for each fork to be created because it waits for 
each fork to gather the results (a _RunResult_ object).
{code}
Callable<RunResult> pf = new Callable<RunResult>()
{
   public RunResult call() throws Exception {
      return fork( testSet, properties, forkClient, 
fileReporterFactory.getGlobalRunStatistics() );
   }
};
{code}

*The problem is the _properties_ passed to the fork.* This is a shared 
reference between all the _Callable_ created! Inside the _fork()_ method this 
property is changed to fit the test suite configuration (see 
_BooterSerializer.serialize()_) and due to the fact that is shared, every 
thread will change it producing the erratic behavior on the execution of the 
test suites.

I have a simple solution: cloning the property passed to the _fork()_ method 
like this:
{code}
Callable<RunResult> pf = new Callable<RunResult>()
{
   public RunResult call() throws Exception {
      return fork( testSet, (Properties) properties.clone(), forkClient, 
fileReporterFactory.getGlobalRunStatistics() );
   }
};
{code}

I tested and is working fine.
                
> Parallel test execution on class level runs not all tests but some twice
> ------------------------------------------------------------------------
>
>                 Key: SUREFIRE-869
>                 URL: https://jira.codehaus.org/browse/SUREFIRE-869
>             Project: Maven Surefire
>          Issue Type: Bug
>          Components: Junit 4.7+ (parallel) support
>    Affects Versions: 2.12
>         Environment: Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
> Maven home: /opt/local/share/java/maven3
> Java version: 1.7.0_04, vendor: Oracle Corporation
> Java home: /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre
> Default locale: en_US, platform encoding: UTF-8
> OS name: "mac os x", version: "10.7.4", arch: "x86_64", family: "mac"
> JUnit 4.10
>            Reporter: Martin Burger
>            Priority: Blocker
>         Attachments: parallel-tests.tgz
>
>
> When I run JUnit tests in parallel on the class level, Surefire behaves 
> somehow erratically. For instance, having test classes TestA and TestB, 
> Surefire sometimes runs TestA twice, TestB twice at other times - but never 
> TestA and TestB in parallel.
> I attached a simple Maven project that helps reproduce this issue. Via {{mvn 
> test -P sequential}} you can execute two test classes sequentially:
> {noformat}
> ...
> -------------------------------------------------------
>  T E S T S
> -------------------------------------------------------
> Running mavendebug.TestAppFirstTest
> -----------------> TestAppFirstTest.test()
> Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 sec
> Running mavendebug.TestAppSecondTest
> -----------------> TestAppSecondTest.test()
> Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
> Results :
> Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
> ...
> {noformat}
> Running {{mvn test}} instead runs tests in parallel:
> {noformat}
> ...
> -------------------------------------------------------
>  T E S T S
> -------------------------------------------------------
> -------------------------------------------------------
>  T E S T S
> -------------------------------------------------------
> Concurrency config is parallel='classes', perCoreThreadCount=false, 
> threadCount=4, useUnlimitedThreads=false
> Concurrency config is parallel='classes', perCoreThreadCount=false, 
> threadCount=4, useUnlimitedThreads=false
> Running mavendebug.TestAppSecondTest
> Running mavendebug.TestAppSecondTest
> -----------------> TestAppSecondTest.test()
> -----------------> TestAppSecondTest.test()
> Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
> Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
> Results :
> Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
> ...
> {noformat}
> As you can see, TestAppSecondTest is executed twice, while test 
> TestAppFirstTest is not executed at all.
> Please note: I chose Blocker as priority as this caused (1) that our 
> extensive test suite was not executed completely during the last weeks which 
> we only discovered today, and (2) that caused tests that run against a 
> database to fail with unexpected constraint violations just because those 
> tests where executed twice in parallel.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://jira.codehaus.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to