apache / apache/maven-surefire
[SUREFIRE-2029] Parallel execution but surefire.forkNumber is the same
- Dominant language
- Java
- Stars
- 461
- Forks
- 588
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 19
Description
**[Nathan McDonald](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=JIRAUSER286028)** opened **[SUREFIRE-2029](https://issues.apache.org/jira/browse/SUREFIRE-2029?redirect=false)** and commented
We have a multi module maven project, and due to our legacy architecture different modules are using the same underlying database.
We are using the one db per fork strategy mentioned, and each test clears the database before running its test.
This seems to work fine. But when running the full build on azure with:
```
mvn ... -T 1C -pl {modulesToBuild} -amd
```
See output kicking off build like:
```
[INFO] Using the MultiThreadedBuilder implementation with a thread count of 8
```
We occasionally see one long running test fail, and have traced cause that another test is running in parallel and clearing the database before long running test completes . This would only seem to be possible if parallel forks running but same surefire.forkNumber being set for multiple forks.
Outputting logs of when tests start and end, with log outputting "Thread#$threadId{$forkNumber}, e.g. Thread#7{3} for thread with id 7, where surefire.forkNumber is 3
Looking at logs we can see test starts on thread/fork 1, clears db as expected, and 30 seconds later logs that it completes (with error that is shown later).
But we can see just moments later, another test starts up and also clears the database, but is using same thread/fork:
```
2022-03-01T16:30:59.5953417Z 2022-03-01 16:30:59.585 [main] INFO e.t.b.h.w.c.AbstractIT:556 - Thread#1{1} - Clearing the database before test
2022-03-01T16:30:59.7150510Z 2022-03-01 16:30:59.711 [main] INFO e.t.b.a.w.c.AnalysisJobDownloadControllerTest:78 - Thread#1{1} - running setup for AnalysisJobDownloadControllerTest#downloadTransactionsAfterAppendingDuplicateRecords
```
…
```
2022-03-01T16:31:01.1305751Z 2022-03-01 16:31:01.128 [main] INFO e.t.b.h.w.c.AbstractIT:556 - Thread#1{1} - Clearing the database before test
2022-03-01T16:31:01.6123984Z [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 24.715 s - in e.t.b.h.w.c.FinancialStatementReportItemRestControllerIT
2022-03-01T16:31:01.6124965Z [INFO] Running e.t.b.h.w.c.KeyInformationRestControllerIT
```
…
```
2022-03-01T16:31:29.1656587Z 2022-03-01 16:31:29.164 [main] INFO e.t.b.a.w.c.AnalysisJobDownloadControllerTest:94 - Thread#1{1} - complete test AnalysisJobDownloadControllerTest#downloadTransactionsAfterAppendingDuplicateRecords
```
This isn't consistent though. Can see other cases where this works, but the logs found where it is working can see long running test running on separate fork:
Thread#1{3}
Obviously there are separate threads/forks running here, so seems like somewhere between maven multi module and -T 1C, there is not always assigning unique forkNumber to each fork.
I figure best practice is probably having separate modules not use same db, so possibly this issue is existing but not being hit by people as would only cause issue if same fork number used for separate modules on different databases.
Still looking into issue our side will update if find workaround or more detailed information.
Our surefire/failsafe config is in the root inherited by all other sub modules:
```java
org.apache.maven.plugins
maven-surefire-plugin
2
@{argLine} -DforkNumber=${surefire.forkNumber}
false
org.apache.maven.plugins
maven-failsafe-plugin
1
@{argLine} -DforkNumber=${surefire.forkNumber}
false
${project.build.directory}/surefire-reports
integration-test
verify
```
mvn version info:
```
Apache Maven 3.8.2 (ea98e05a04480131370aa0c110b8c54cf726c06f)
Maven home: /usr/local/apache-maven
Java version: 11.0.13, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.53.1.el7.x86_64", arch: "amd64", family: "unix"
```
---
**Affects:** 2.22.2
Contributor guide
Research direction
Start with the inherited maven-surefire-plugin and maven-failsafe-plugin configuration, especially forkCount and the argLine using surefire.forkNumber. Reproduce the multi-module command with -T 1C and inspect the logged thread and fork numbers while tests overlap. Done means parallel forks receive distinct usable numbers, or the collision is explained and a reliable workaround is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100