Skip to content

Commit

Permalink
make worker_threads opt-in, not opt-out (#7693)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Jan 24, 2019
1 parent 6de22dd commit 9cfcd81
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442))
- `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476))
- `[expect]` Improve report when assertion fails, part 6 ([#7621](https://github.com/facebook/jest/pull/7621))
- `[jest-worker]` Add `disableWorkerThreads` option to explicitly opt out of `worker_threads` even if available ([#7681](https://github.com/facebook/jest/pull/7681))
- `[jest-worker]` Add `enableWorkerThreads` option to explicitly opt-in to `worker_threads` if available ([#7681](https://github.com/facebook/jest/pull/7681))

### Fixes

Expand Down
1 change: 0 additions & 1 deletion packages/jest-cli/src/reporters/coverage_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export default class CoverageReporter extends BaseReporter {
} else {
// $FlowFixMe: assignment of a worker with custom properties.
worker = new Worker(require.resolve('./coverage_worker'), {
disableWorkerThreads: true,
exposedMethods: ['worker'],
maxRetries: 2,
numWorkers: this._globalConfig.maxWorkers,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ class HasteMap extends EventEmitter {
} else {
// $FlowFixMe: assignment of a worker with custom properties.
this._worker = (new Worker(require.resolve('./worker'), {
disableWorkerThreads: true,
exposedMethods: ['getSha1', 'worker'],
maxRetries: 3,
numWorkers: this._options.maxWorkers,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-runner/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class TestRunner {
) {
// $FlowFixMe: class object is augmented with worker when instantiating.
const worker: WorkerInterface = new Worker(TEST_WORKER_PATH, {
disableWorkerThreads: true,
exposedMethods: ['worker'],
forkOptions: {stdio: 'pipe'},
maxRetries: 3,
Expand Down
8 changes: 3 additions & 5 deletions packages/jest-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export function hello(param) {

Node 10 shipped with [worker-threads](https://nodejs.org/api/worker_threads.html), a "threading API" that uses SharedArrayBuffers to communicate between the main process and its child threads. This experimental Node feature can significantly improve the communication time between parent and child processes in `jest-worker`.

We will use worker threads where available. To enable in Node 10+, run the Node process with the `--experimental-worker` flag.

You can explicitly opt-out of this by passing `disableWorkerThreads: true`.
Since `worker_threads` are considered experimental in Node, you have to opt-in to this behavior by passing `enableWorkerThreads: true` when instantiating the worker. While the feature was unflagged in Node 11.7.0, you'll need to run the Node process with the `--experimental-worker` flag for Node 10.

## API

Expand Down Expand Up @@ -91,9 +89,9 @@ Provide a custom worker pool to be used for spawning child processes. By default

The arguments that will be passed to the `setup` method during initialization.

#### `disableWorkerThreads: boolean` (optional)
#### `enableWorkerThreads: boolean` (optional)

`jest-worker` will automatically detect if `worker_threads` are available and use them. However, running under threads comes with [some caveats](https://nodejs.org/api/worker_threads.html#worker_threads_class_worker), and is still experimental, so you can `opt-out` of this and use `disableWorkerThreads: true`.
`jest-worker` will automatically detect if `worker_threads` are available, but will not use them unless passed `enableWorkerThreads: true`.

## Worker

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-worker/src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {

createWorker(workerOptions: WorkerOptions): WorkerInterface {
let Worker;
if (!this._options.disableWorkerThreads && canUseWorkerThreads()) {
if (this._options.enableWorkerThreads && canUseWorkerThreads()) {
Worker = require('./workers/NodeThreadsWorker').default;
} else {
Worker = require('./workers/ChildProcessWorker').default;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-worker/src/__tests__/WorkerPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('WorkerPool', () => {
it('should create a NodeThreadWorker and send to it', () => {
jest.mock('worker_threads', () => 'Defined');
const workerPool = new WorkerPool('/path', {
enableWorkerThreads: true,
forkOptions: {},
maxRetries: 1,
numWorkers: 1,
Expand All @@ -102,10 +103,9 @@ describe('WorkerPool', () => {
);
});

it('should avoid NodeThreadWorker if passed disableWorkerThreads', () => {
it('should avoid NodeThreadWorker if not passed enableWorkerThreads', () => {
jest.mock('worker_threads', () => 'Defined');
const workerPool = new WorkerPool('/path', {
disableWorkerThreads: true,
forkOptions: {},
maxRetries: 1,
numWorkers: 1,
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-worker/src/__tests__/thread-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('Jest Worker Process Integration', () => {

it('calls a single method from the worker', async () => {
const farm = new Farm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
Expand All @@ -79,6 +80,7 @@ describe('Jest Worker Process Integration', () => {

it('distributes sequential calls across child processes', async () => {
const farm = new Farm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
Expand All @@ -100,6 +102,7 @@ describe('Jest Worker Process Integration', () => {

it('distributes concurrent calls across child processes', async () => {
const farm = new Farm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
Expand Down Expand Up @@ -128,6 +131,7 @@ describe('Jest Worker Process Integration', () => {
it('sticks parallel calls to children', async () => {
const farm = new Farm('/tmp/baz.js', {
computeWorkerKey: () => '1234567890abcdef',
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class JestWorker {
this._options = {...options};

const workerPoolOptions: WorkerPoolOptions = {
disableWorkerThreads: this._options.disableWorkerThreads || false,
enableWorkerThreads: this._options.enableWorkerThreads || false,
forkOptions: this._options.forkOptions || {},
maxRetries: this._options.maxRetries || 3,
numWorkers: this._options.numWorkers || Math.max(os.cpus().length - 1, 1),
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-worker/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ export type FarmOptions = {
workerPath: string,
options?: WorkerPoolOptions,
) => WorkerPoolInterface,
disableWorkerThreads?: boolean,
enableWorkerThreads?: boolean,
};

export type WorkerPoolOptions = {|
setupArgs: Array<mixed>,
forkOptions: ForkOptions,
maxRetries: number,
numWorkers: number,
disableWorkerThreads: boolean,
enableWorkerThreads: boolean,
|};

export type WorkerOptions = {|
Expand Down

0 comments on commit 9cfcd81

Please sign in to comment.