Skip to content

Commit

Permalink
remove enableWorkerThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 9, 2022
1 parent 10f91f6 commit db488f1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/jest-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function hello(param) {

Node 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 feature can significantly improve the communication time between parent and child processes in `jest-worker`.

To use `worker_threads` instead of default `child_process` you have to pass `enableWorkerThreads: true` when instantiating the worker.
To disable the default `worker_threads` and instead use `child_process` you have to pass `enableWorkerThreads: false` when instantiating the worker.

## API

Expand Down
6 changes: 4 additions & 2 deletions packages/jest-worker/src/WorkerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
WorkerOptions,
WorkerPoolInterface,
} from './types';
import ChildProcessWorker from './workers/ChildProcessWorker';
import NodeThreadsWorker from './workers/NodeThreadsWorker';

class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {
send(
Expand All @@ -30,9 +32,9 @@ class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {
createWorker(workerOptions: WorkerOptions): WorkerInterface {
let Worker;
if (this._options.enableWorkerThreads) {
Worker = require('./workers/NodeThreadsWorker').default;
Worker = NodeThreadsWorker;
} else {
Worker = require('./workers/ChildProcessWorker').default;
Worker = ChildProcessWorker;
}

return new Worker(workerOptions);
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 @@ -75,7 +75,6 @@ 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,9 +101,10 @@ describe('WorkerPool', () => {
);
});

it('should avoid NodeThreadWorker if not passed enableWorkerThreads', () => {
it('should use ChildProcessWorker if passed enableWorkerThreads: false', () => {
jest.mock('worker_threads', () => 'Defined');
const workerPool = new WorkerPool('/path', {
enableWorkerThreads: false,
forkOptions: {},
maxRetries: 1,
numWorkers: 1,
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-worker/src/__tests__/thread-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ 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,7 +78,6 @@ 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 Down Expand Up @@ -150,7 +148,6 @@ 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 @@ -179,7 +176,6 @@ 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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Worker {
}

const workerPoolOptions: WorkerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
enableWorkerThreads: this._options.enableWorkerThreads ?? true,
forkOptions: this._options.forkOptions ?? {},
maxRetries: this._options.maxRetries ?? 3,
numWorkers: this._options.numWorkers ?? Math.max(cpus().length - 1, 1),
Expand Down

0 comments on commit db488f1

Please sign in to comment.