Skip to content

Commit efec054

Browse files
ranyitzmjesun
authored andcommitted
Add unique id for each worker and pass it to the child process (#5494)
1 parent c8d1c79 commit efec054

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## master
22

3+
### Features
4+
5+
* `[jest-worker]` Assign a unique id for each worker and pass it to the child
6+
process. It will be available via `process.env.JEST_WORKER_ID`
7+
([#5494](https://github.com/facebook/jest/pull/5494))
8+
39
## jest 22.2.1
410

511
### Fixes

packages/jest-worker/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ overriding options through `forkOptions`.
130130
Finishes the workers by killing all workers. No further calls can be done to the
131131
`Worker` instance.
132132

133+
**Note:** Each worker has a unique id (index that starts with `1`) which is
134+
available on `process.env.JEST_WORKER_ID`
135+
133136
# More examples
134137

135138
## Standard usage
@@ -143,12 +146,13 @@ import Worker from 'jest-worker';
143146

144147
async function main() {
145148
const myWorker = new Worker(require.resolve('./worker'), {
146-
exposedMethods: ['foo', 'bar'],
149+
exposedMethods: ['foo', 'bar', 'getWorkerId'],
147150
numWorkers: 4,
148151
});
149152

150153
console.log(await myWorker.foo('Alice')); // "Hello from foo: Alice"
151154
console.log(await myWorker.bar('Bob')); // "Hello from bar: Bob"
155+
console.log(await myWorker.getWorkerId()); // "3" -> this message has sent from the 3rd worker
152156

153157
myWorker.end();
154158
}
@@ -166,6 +170,10 @@ export function foo(param) {
166170
export function bar(param) {
167171
return 'Hello from bar: ' + param;
168172
}
173+
174+
export function getWorkerId() {
175+
return process.env.JEST_WORKER_ID;
176+
}
169177
```
170178

171179
## Bound worker usage:

packages/jest-worker/src/__tests__/index.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,26 @@ it('tries instantiating workers with the right options', () => {
122122
expect(Worker.mock.calls[0][0]).toEqual({
123123
forkOptions: {execArgv: []},
124124
maxRetries: 6,
125+
workerId: 1,
125126
workerPath: '/tmp/baz.js',
126127
});
127128
});
128129

130+
it('create multiple workers with unique worker ids', () => {
131+
// eslint-disable-next-line no-new
132+
new Farm('/tmp/baz.js', {
133+
exposedMethods: ['foo', 'bar'],
134+
forkOptions: {execArgv: []},
135+
maxRetries: 6,
136+
numWorkers: 3,
137+
});
138+
139+
expect(Worker).toHaveBeenCalledTimes(3);
140+
expect(Worker.mock.calls[0][0].workerId).toEqual(1);
141+
expect(Worker.mock.calls[1][0].workerId).toEqual(2);
142+
expect(Worker.mock.calls[2][0].workerId).toEqual(3);
143+
});
144+
129145
it('makes a non-existing relative worker throw', () => {
130146
expect(
131147
() =>

packages/jest-worker/src/__tests__/worker.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () =>
5757
execPath: 'hello',
5858
},
5959
maxRetries: 3,
60+
workerId: process.env.JEST_WORKER_ID,
6061
workerPath: '/tmp/foo/bar/baz.js',
6162
});
6263

@@ -70,6 +71,17 @@ it('passes fork options down to child_process.fork, adding the defaults', () =>
7071
});
7172
});
7273

74+
it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', () => {
75+
new Worker({
76+
forkOptions: {},
77+
maxRetries: 3,
78+
workerId: 2,
79+
workerPath: '/tmp/foo',
80+
});
81+
82+
expect(childProcess.fork.mock.calls[0][1].env.JEST_WORKER_ID).toEqual(2);
83+
});
84+
7385
it('initializes the child process with the given workerPath', () => {
7486
new Worker({
7587
forkOptions: {},

packages/jest-worker/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,16 @@ export default class {
6666
workerPath = require.resolve(workerPath);
6767
}
6868

69-
// Build the options once for all workers to avoid allocating extra objects.
70-
const workerOptions = {
69+
const sharedWorkerOptions = {
7170
forkOptions: options.forkOptions || {},
7271
maxRetries: options.maxRetries || 3,
7372
workerPath,
7473
};
7574

7675
for (let i = 0; i < numWorkers; i++) {
76+
const workerOptions = Object.assign({}, sharedWorkerOptions, {
77+
workerId: i + 1,
78+
});
7779
const worker = new Worker(workerOptions);
7880
const workerStdout = worker.getStdout();
7981
const workerStderr = worker.getStderr();

packages/jest-worker/src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type FarmOptions = {
4646
export type WorkerOptions = {|
4747
forkOptions: ForkOptions,
4848
maxRetries: number,
49+
workerId: number,
4950
workerPath: string,
5051
|};
5152

packages/jest-worker/src/worker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export default class {
7979
Object.assign(
8080
{
8181
cwd: process.cwd(),
82-
env: process.env,
82+
env: Object.assign({}, process.env, {
83+
JEST_WORKER_ID: this._options.workerId,
84+
}),
8385
// suppress --debug / --inspect flags while preserving others (like --harmony)
8486
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
8587
silent: true,

0 commit comments

Comments
 (0)