Skip to content

Commit d6ad15b

Browse files
authored
fix: worker being killed after being spawned and other worker bugs (#13107)
1 parent 24ed3b5 commit d6ad15b

14 files changed

+644
-256
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036))
66
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade to `jsdom@20` ([#13037](https://github.com/facebook/jest/pull/13037), [#13058](https://github.com/facebook/jest/pull/13058))
7-
- `[jest-worker]` Adds `workerIdleMemoryLimit` option which is used as a check for worker memory leaks >= Node 16.11.0 and recycles child workers as required. ([#13056](https://github.com/facebook/jest/pull/13056), [#13105](https://github.com/facebook/jest/pull/13105), [#13106](https://github.com/facebook/jest/pull/13106))
7+
- `[jest-worker]` Adds `workerIdleMemoryLimit` option which is used as a check for worker memory leaks >= Node 16.11.0 and recycles child workers as required. ([#13056](https://github.com/facebook/jest/pull/13056), [#13105](https://github.com/facebook/jest/pull/13105), [#13106](https://github.com/facebook/jest/pull/13106), [#13107](https://github.com/facebook/jest/pull/13107))
88
- `[pretty-format]` [**BREAKING**] Remove `ConvertAnsi` plugin in favour of `jest-serializer-ansi-escapes` ([#13040](https://github.com/facebook/jest/pull/13040))
99

1010
### Fixes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`all 3 test files should complete 1`] = `
4+
"Test Suites: 3 passed, 3 total
5+
Tests: 3 passed, 3 total
6+
Snapshots: 0 total
7+
Time: <<REPLACED>>
8+
Ran all test suites."
9+
`;
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {extractSummary} from '../Utils';
9+
import runJest from '../runJest';
10+
11+
it('all 3 test files should complete', () => {
12+
const result = runJest('worker-restarting');
13+
expect(result.exitCode).toBe(0);
14+
const {summary} = extractSummary(result.stderr);
15+
expect(summary).toMatchSnapshot();
16+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('basic test', () => {
9+
expect(true).toBeTruthy();
10+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('basic test', () => {
9+
expect(true).toBeTruthy();
10+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('basic test', () => {
9+
expect(true).toBeTruthy();
10+
});

e2e/worker-restarting/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jest": {
3+
"maxWorkers": 2,
4+
"workerIdleMemoryLimit": "1MB"
5+
}
6+
}

packages/jest-worker/src/types.ts

+32
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ export interface WorkerPoolInterface {
6666
}
6767

6868
export interface WorkerInterface {
69+
get state(): WorkerStates;
70+
6971
send(
7072
request: ChildMessage,
7173
onProcessStart: OnStart,
7274
onProcessEnd: OnEnd,
7375
onCustomMessage: OnCustomMessage,
7476
): void;
77+
7578
waitForExit(): Promise<void>;
7679
forceExit(): void;
7780

@@ -83,6 +86,18 @@ export interface WorkerInterface {
8386
*/
8487
getWorkerSystemId(): number;
8588
getMemoryUsage(): Promise<number | null>;
89+
/**
90+
* Checks to see if the child worker is actually running.
91+
*/
92+
isWorkerRunning(): boolean;
93+
/**
94+
* When the worker child is started and ready to start handling requests.
95+
*
96+
* @remarks
97+
* This mostly exists to help with testing so that you don't check the status
98+
* of things like isWorkerRunning before it actually is.
99+
*/
100+
waitForWorkerReady(): Promise<void>;
86101
}
87102

88103
export type PoolExitResult = {
@@ -170,8 +185,21 @@ export type WorkerOptions = {
170185
* the raw output of the worker.
171186
*/
172187
silent?: boolean;
188+
/**
189+
* Used to immediately bind event handlers.
190+
*/
191+
on?: {
192+
[WorkerEvents.STATE_CHANGE]:
193+
| OnStateChangeHandler
194+
| ReadonlyArray<OnStateChangeHandler>;
195+
};
173196
};
174197

198+
export type OnStateChangeHandler = (
199+
state: WorkerStates,
200+
oldState: WorkerStates,
201+
) => void;
202+
175203
// Messages passed from the parent to the children.
176204

177205
export type MessagePort = typeof EventEmitter & {
@@ -265,3 +293,7 @@ export enum WorkerStates {
265293
SHUTTING_DOWN = 'shutting-down',
266294
SHUT_DOWN = 'shut-down',
267295
}
296+
297+
export enum WorkerEvents {
298+
STATE_CHANGE = 'state-change',
299+
}

0 commit comments

Comments
 (0)