adding a MOCHA_WORKER_ID environment variable, unique for each worker #4463
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Requirements
Description of the Change
This change addresses #4456. It adds a
MOCHA_WORKER_ID
environment variable that is unique for each worker, so that it may be used for by any test resources that need to use global resources (network ports, filesystem paths, etc.) but ensure that these are unique in order to avoid conflicts during parallel tests.Alternate Designs
I considered using
forkArgs
instead of an environment variable, so that an ID may be accessed usingprocess.argv[2]
. I did not want to do this in case adding arguments was necessary for internal uses in the future.Why should this be in core?
It is common for tests to use global resources like network ports, which can conflict during parallel testing. Without a feature like this in core, users would be left on their own to create a home-brewed solution (using filesystem files as signals, sockets that elect a leader in the cluster so that one process hands out all global data, a standalone cluster orchestration system), all of which add a lot of complication to writing tests. This would also be a difference when writing tests that run in series vs. parallel, as this concern would not exist (and potentially needs to be turned off) for test in series, and would add pain points when taking existing serial tests and having them run in parallel. This makes using mocha significantly less fun.
This feature takes that pain point away. For example, allocating ports would once again be trivial, using
const port = 1234 + Number(process.env.MOCHA_WORKER_ID || 0)
.Other parallel testing frameworks, like jest, already use this approach: jestjs/jest#2284
Benefits
This solves common pain points for developers writing tests.
Possible Drawbacks
Other than more complexity, I can't think of a drawback for the feature itself.
Since
workerpool
does not itself provide an option for worker-specific options or to execute code deterministically in a specific worker (only an option to add to the queue to execute on any available worker), I had to use a workaround in the implementation. This workaround could make internal updates more difficult and adds extra requirements to replacingworkerpool
if a need arises to use a different implementation internally. I personally believe that this tradeoff is worth it, though I am probably biased here.Applicable issues
Enter any applicable Issues here.
parallel workers should have an ID that can be used to prevent conflicts among workers #4456
Mocha follows semantic versioning: http://semver.org
Is this a breaking change (major release)? No
Is it an enhancement (minor release)? Yes
Is it a bug fix, or does it not impact production code (patch release)? No