Skip to content

Commit

Permalink
remove enableWorkerThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 22, 2023
1 parent d49089c commit 56825d8
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 {
override 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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 Down Expand Up @@ -107,9 +106,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.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('Jest Worker Process Integration', () => {

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

it('distributes sequential calls across child processes', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
Expand Down Expand Up @@ -152,7 +150,6 @@ describe('Jest Worker Process Integration', () => {

it('distributes concurrent calls across child processes', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
Expand Down Expand Up @@ -181,7 +178,6 @@ describe('Jest Worker Process Integration', () => {
it('sticks parallel calls to children', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
computeWorkerKey: () => '1234567890abcdef',
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
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 @@ -104,7 +104,7 @@ export class Worker {
}

const workerPoolOptions: WorkerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
enableWorkerThreads: this._options.enableWorkerThreads ?? true,
forkOptions: this._options.forkOptions ?? {},
idleMemoryLimit: this._options.idleMemoryLimit,
maxRetries: this._options.maxRetries ?? 3,
Expand Down

0 comments on commit 56825d8

Please sign in to comment.