Skip to content

Commit

Permalink
fix: worker being killed after being spawned and other worker bugs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
phawxby committed Aug 11, 2022
1 parent 24ed3b5 commit d6ad15b
Show file tree
Hide file tree
Showing 14 changed files with 644 additions and 256 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,7 @@

- `[jest-config]` [**BREAKING**] Make `snapshotFormat` default to `escapeString: false` and `printBasicPrototype: false` ([#13036](https://github.com/facebook/jest/pull/13036))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade to `jsdom@20` ([#13037](https://github.com/facebook/jest/pull/13037), [#13058](https://github.com/facebook/jest/pull/13058))
- `[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))
- `[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))
- `[pretty-format]` [**BREAKING**] Remove `ConvertAnsi` plugin in favour of `jest-serializer-ansi-escapes` ([#13040](https://github.com/facebook/jest/pull/13040))

### Fixes
Expand Down
9 changes: 9 additions & 0 deletions e2e/__tests__/__snapshots__/workerRestarting.test.ts.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`all 3 test files should complete 1`] = `
"Test Suites: 3 passed, 3 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
16 changes: 16 additions & 0 deletions e2e/__tests__/workerRestarting.test.ts
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {extractSummary} from '../Utils';
import runJest from '../runJest';

it('all 3 test files should complete', () => {
const result = runJest('worker-restarting');
expect(result.exitCode).toBe(0);
const {summary} = extractSummary(result.stderr);
expect(summary).toMatchSnapshot();
});
10 changes: 10 additions & 0 deletions e2e/worker-restarting/__tests__/test1.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('basic test', () => {
expect(true).toBeTruthy();
});
10 changes: 10 additions & 0 deletions e2e/worker-restarting/__tests__/test2.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('basic test', () => {
expect(true).toBeTruthy();
});
10 changes: 10 additions & 0 deletions e2e/worker-restarting/__tests__/test3.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('basic test', () => {
expect(true).toBeTruthy();
});
6 changes: 6 additions & 0 deletions e2e/worker-restarting/package.json
@@ -0,0 +1,6 @@
{
"jest": {
"maxWorkers": 2,
"workerIdleMemoryLimit": "1MB"
}
}
32 changes: 32 additions & 0 deletions packages/jest-worker/src/types.ts
Expand Up @@ -66,12 +66,15 @@ export interface WorkerPoolInterface {
}

export interface WorkerInterface {
get state(): WorkerStates;

send(
request: ChildMessage,
onProcessStart: OnStart,
onProcessEnd: OnEnd,
onCustomMessage: OnCustomMessage,
): void;

waitForExit(): Promise<void>;
forceExit(): void;

Expand All @@ -83,6 +86,18 @@ export interface WorkerInterface {
*/
getWorkerSystemId(): number;
getMemoryUsage(): Promise<number | null>;
/**
* Checks to see if the child worker is actually running.
*/
isWorkerRunning(): boolean;
/**
* When the worker child is started and ready to start handling requests.
*
* @remarks
* This mostly exists to help with testing so that you don't check the status
* of things like isWorkerRunning before it actually is.
*/
waitForWorkerReady(): Promise<void>;
}

export type PoolExitResult = {
Expand Down Expand Up @@ -170,8 +185,21 @@ export type WorkerOptions = {
* the raw output of the worker.
*/
silent?: boolean;
/**
* Used to immediately bind event handlers.
*/
on?: {
[WorkerEvents.STATE_CHANGE]:
| OnStateChangeHandler
| ReadonlyArray<OnStateChangeHandler>;
};
};

export type OnStateChangeHandler = (
state: WorkerStates,
oldState: WorkerStates,
) => void;

// Messages passed from the parent to the children.

export type MessagePort = typeof EventEmitter & {
Expand Down Expand Up @@ -265,3 +293,7 @@ export enum WorkerStates {
SHUTTING_DOWN = 'shutting-down',
SHUT_DOWN = 'shut-down',
}

export enum WorkerEvents {
STATE_CHANGE = 'state-change',
}

0 comments on commit d6ad15b

Please sign in to comment.