Skip to content

Commit 8b089a2

Browse files
authored
feat(parallel): assign each worker a worker-id (#4813)
This uses a new feature from the workerpool module, which allows overriding process parameters for each process, which is why the workerpool module has been updated to the latest version (6.2.0).
1 parent 9fbf3ae commit 8b089a2

File tree

8 files changed

+66
-9
lines changed

8 files changed

+66
-9
lines changed

docs/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,12 @@ Free-tier cloud CI services may not provide a suitable multi-core container or V
13791379

13801380
It's unlikely (but not impossible) to see a performance gain from a [job count](#-jobs-count-j-count) _greater than_ the number of available CPU cores. That said, _play around with the job count_--there's no one-size-fits all, and the unique characteristics of your tests will determine the optimal number of jobs; it may even be that fewer is faster!
13811381

1382+
### Parallel Mode Worker IDs
1383+
1384+
> _New in v9.2.0_
1385+
1386+
Each process launched by parallel mode is assigned a unique id, from 0 for the first process to be launched, to N-1 for the Nth process. This worker id may be accessed in tests via the environment variable `MOCHA_WORKER_ID`. It can be used for example to assign a different database, service port, etc for each test process.
1387+
13821388
## Root Hook Plugins
13831389

13841390
> _New in v8.0.0_

lib/nodejs/buffered-worker-pool.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,23 @@ class BufferedWorkerPool {
7575
process.execArgv.join(' ')
7676
);
7777

78-
this.options = {...WORKER_POOL_DEFAULT_OPTS, opts, maxWorkers};
78+
let counter = 0;
79+
const onCreateWorker = ({forkOpts}) => {
80+
return {
81+
forkOpts: {
82+
...forkOpts,
83+
// adds an incremental id to all workers, which can be useful to allocate resources for each process
84+
env: {...process.env, MOCHA_WORKER_ID: counter++}
85+
}
86+
};
87+
};
88+
89+
this.options = {
90+
...WORKER_POOL_DEFAULT_OPTS,
91+
...opts,
92+
maxWorkers,
93+
onCreateWorker
94+
};
7995
this._pool = workerpool.pool(WORKER_PATH, this.options);
8096
}
8197

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"strip-json-comments": "3.1.1",
7373
"supports-color": "8.1.1",
7474
"which": "2.0.2",
75-
"workerpool": "6.1.5",
75+
"workerpool": "6.2.0",
7676
"yargs": "16.2.0",
7777
"yargs-parser": "20.2.4",
7878
"yargs-unparser": "2.0.0"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import assert from 'assert';
2+
3+
describe('test1', () => {
4+
it('should always run on worker with id 0', () => {
5+
assert.ok(process.env.MOCHA_WORKER_ID === '0');
6+
});
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import assert from 'assert';
2+
3+
describe('test2', () => {
4+
it('should always run on worker with id 1', () => {
5+
assert.ok(process.env.MOCHA_WORKER_ID === '1');
6+
});
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import assert from 'assert';
2+
3+
describe('test3', () => {
4+
it('should run on worker with either id 0 or 1', () => {
5+
assert.ok(
6+
process.env.MOCHA_WORKER_ID === '0' || process.env.MOCHA_WORKER_ID === '1'
7+
);
8+
});
9+
});

test/integration/parallel.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ describe('parallel run', () => {
1818
assert.strictEqual(result.stats.failures, 1);
1919
assert.strictEqual(result.stats.passes, 2);
2020
});
21+
22+
it('should correctly set worker ids for each process', async () => {
23+
const result = await runMochaJSONAsync('parallel/testworkerid3.mjs', [
24+
'--parallel',
25+
'--jobs',
26+
'2',
27+
require.resolve('./fixtures/parallel/testworkerid1.mjs'),
28+
require.resolve('./fixtures/parallel/testworkerid2.mjs')
29+
]);
30+
assert.strictEqual(result.stats.failures, 0);
31+
assert.strictEqual(result.stats.passes, 3);
32+
});
2133
});

0 commit comments

Comments
 (0)