Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions e2e/__tests__/infinite-loop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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 {tmpdir} from 'os';
import * as path from 'path';
import {
cleanup,
generateTestFilesToForceUsingWorkers,
writeFiles,
} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(tmpdir(), 'inspector');

afterEach(() => cleanup(DIR));

it('fails a test which causes an infinite loop', () => {
const testFiles = generateTestFilesToForceUsingWorkers();

writeFiles(DIR, {
...testFiles,
'__tests__/inspector.test.js': `
test('infinite loop error', () => {
while(true) {}
});
`,
'package.json': '{}',
});

const {exitCode, stderr} = runJest(DIR, ['--maxWorkers=2']);

expect(exitCode).toBe(1);
expect(stderr).toMatch(/worker.+unresponsive/);
});
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jest.mock('child_process', () => ({
}));

jest.mock('jest-worker', () => ({
Worker: jest.fn(worker => {
create: jest.fn(worker => {
mockWorker = jest.fn((...args) => require(worker).worker(...args));
mockEnd = jest.fn();

Expand Down
50 changes: 28 additions & 22 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default class HasteMap extends EventEmitter {
private _console: Console;
private _options: InternalOptions;
private _watchers: Array<Watcher>;
private _worker: WorkerInterface | null;
private _worker: Promise<WorkerInterface> | null;

constructor(options: Options) {
super();
Expand Down Expand Up @@ -543,14 +543,16 @@ export default class HasteMap extends EventEmitter {
if (this._options.retainAllFiles && filePath.includes(NODE_MODULES)) {
if (computeSha1) {
return this._getWorker(workerOptions)
.getSha1({
computeDependencies: this._options.computeDependencies,
computeSha1,
dependencyExtractor: this._options.dependencyExtractor,
filePath,
hasteImplModulePath: this._options.hasteImplModulePath,
rootDir,
})
.then(workerInstance =>
workerInstance.getSha1({
computeDependencies: this._options.computeDependencies,
computeSha1,
dependencyExtractor: this._options.dependencyExtractor,
filePath,
hasteImplModulePath: this._options.hasteImplModulePath,
rootDir,
}),
)
.then(workerReply, workerError);
}

Expand Down Expand Up @@ -619,14 +621,16 @@ export default class HasteMap extends EventEmitter {
}

return this._getWorker(workerOptions)
.worker({
computeDependencies: this._options.computeDependencies,
computeSha1,
dependencyExtractor: this._options.dependencyExtractor,
filePath,
hasteImplModulePath: this._options.hasteImplModulePath,
rootDir,
})
.then(workerInstance =>
workerInstance.worker({
computeDependencies: this._options.computeDependencies,
computeSha1,
dependencyExtractor: this._options.dependencyExtractor,
filePath,
hasteImplModulePath: this._options.hasteImplModulePath,
rootDir,
}),
)
.then(workerReply, workerError);
}

Expand Down Expand Up @@ -711,21 +715,23 @@ export default class HasteMap extends EventEmitter {
/**
* Creates workers or parses files and extracts metadata in-process.
*/
private _getWorker(options?: {forceInBand: boolean}): WorkerInterface {
private async _getWorker(options?: {
forceInBand: boolean;
}): Promise<WorkerInterface> {
if (!this._worker) {
if ((options && options.forceInBand) || this._options.maxWorkers <= 1) {
this._worker = {getSha1, worker};
this._worker = Promise.resolve({getSha1, worker});
} else {
// @ts-expect-error: assignment of a worker with custom properties.
this._worker = new Worker(require.resolve('./worker'), {
this._worker = await Worker.create(require.resolve('./worker'), {
exposedMethods: ['getSha1', 'worker'],
maxRetries: 3,
numWorkers: this._options.maxWorkers,
}) as WorkerInterface;
});
}
}

return this._worker;
return this._worker!;
}

private _crawl(hasteMap: InternalHasteMap) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/CoverageReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class CoverageReporter extends BaseReporter {
if (this._globalConfig.maxWorkers <= 1) {
worker = require('./CoverageWorker');
} else {
worker = new Worker(require.resolve('./CoverageWorker'), {
worker = await Worker.create(require.resolve('./CoverageWorker'), {
exposedMethods: ['worker'],
maxRetries: 2,
numWorkers: this._globalConfig.maxWorkers,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runner/src/__tests__/testRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TestRunner from '../index';
let mockWorkerFarm;

jest.mock('jest-worker', () => ({
Worker: jest.fn(
create: jest.fn(
worker =>
(mockWorkerFarm = {
end: jest.fn().mockResolvedValue({forceExited: false}),
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class TestRunner {
}
}

const worker = new Worker(TEST_WORKER_PATH, {
const worker = (await Worker.create(TEST_WORKER_PATH, {
exposedMethods: ['worker'],
forkOptions: {stdio: 'pipe'},
maxRetries: 3,
Expand All @@ -174,7 +174,7 @@ export default class TestRunner {
serializableResolvers: Array.from(resolvers.values()),
},
],
}) as WorkerInterface;
})) as WorkerInterface;

if (worker.getStdout()) worker.getStdout().pipe(process.stdout);
if (worker.getStderr()) worker.getStderr().pipe(process.stderr);
Expand Down
10 changes: 7 additions & 3 deletions packages/jest-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This example covers the minimal usage:
import JestWorker from 'jest-worker';

async function main() {
const worker = new JestWorker(require.resolve('./Worker'));
const worker = await JestWorker.create(require.resolve('./Worker'));
const result = await worker.hello('Alice'); // "Hello, Alice"
}

Expand Down Expand Up @@ -63,6 +63,10 @@ List of method names that can be called on the child processes from the parent p

Amount of workers to spawn. Defaults to the number of CPUs minus 1.

#### `workerHeartbeatTimeout: number` (optional)

Heartbeat timeout used to ping the parent process when child workers are alive. Defaults to 10000 ms.

#### `maxRetries: number` (optional)

Maximum amount of times that a dead child can be re-spawned, per call. Defaults to `3`, pass `Infinity` to allow endless retries.
Expand Down Expand Up @@ -156,7 +160,7 @@ This example covers the standard usage:
import JestWorker from 'jest-worker';

async function main() {
const myWorker = new JestWorker(require.resolve('./Worker'), {
const myWorker = await JestWorker.create(require.resolve('./Worker'), {
exposedMethods: ['foo', 'bar', 'getWorkerId'],
numWorkers: 4,
});
Expand Down Expand Up @@ -200,7 +204,7 @@ This example covers the usage with a `computeWorkerKey` method:
import {Worker as JestWorker} from 'jest-worker';

async function main() {
const myWorker = new JestWorker(require.resolve('./Worker'), {
const myWorker = await JestWorker.create(require.resolve('./Worker'), {
computeWorkerKey: (method, filename) => filename,
});

Expand Down
13 changes: 8 additions & 5 deletions packages/jest-worker/src/__performance_tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ function testJestWorker() {
}
}

const farm = new JestWorker(require.resolve('./workers/jest_worker'), {
exposedMethods: [method],
forkOptions: {execArgv: []},
numWorkers: threads,
});
const farm = await JestWorker.create(
require.resolve('./workers/jest_worker'),
{
exposedMethods: [method],
forkOptions: {execArgv: []},
workers: threads,
},
);

farm.getStdout().pipe(process.stdout);
farm.getStderr().pipe(process.stderr);
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-worker/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

'use strict';

import {Session} from 'inspector';

let Farm;
let WorkerPool;
let Queue;
Expand Down Expand Up @@ -170,3 +172,9 @@ it('calls getStderr and getStdout from worker', async () => {
expect(farm.getStderr()('err')).toEqual('err');
expect(farm.getStdout()('out')).toEqual('out');
});

it('should create a worker with an inspector attached to it', async () => {
const farm = await Farm.create('/fake-worker.js');

expect(farm._inspectorSession instanceof Session).toBe(true);
});
11 changes: 10 additions & 1 deletion packages/jest-worker/src/base/BaseWorkerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ export default class BaseWorkerPool {
const stdout = mergeStream();
const stderr = mergeStream();

const {forkOptions, maxRetries, resourceLimits, setupArgs} = options;
const {
forkOptions,
inspector,
maxRetries,
setupArgs,
resourceLimits,
workerHeartbeatTimeout,
} = options;

for (let i = 0; i < options.numWorkers; i++) {
const workerOptions: WorkerOptions = {
forkOptions,
inspector,
maxRetries,
resourceLimits,
setupArgs,
workerHeartbeatTimeout,
workerId: i,
workerPath,
};
Expand Down
44 changes: 43 additions & 1 deletion packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/* eslint-disable local/ban-types-eventually */

import * as inspector from 'inspector';
import {cpus} from 'os';
import Farm from './Farm';
import WorkerPool from './WorkerPool';
Expand Down Expand Up @@ -75,18 +76,25 @@ export class Worker {
private _farm: Farm;
private _options: FarmOptions;
private _workerPool: WorkerPoolInterface;
private _inspectorSession: inspector.Session | undefined;

constructor(workerPath: string, options?: FarmOptions) {
constructor(
workerPath: string,
inspectorSession?: inspector.Session,
options?: FarmOptions,
) {
this._options = {...options};
this._ending = false;

const workerPoolOptions: WorkerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
forkOptions: this._options.forkOptions ?? {},
inspector: inspectorSession,
maxRetries: this._options.maxRetries ?? 3,
numWorkers: this._options.numWorkers ?? Math.max(cpus().length - 1, 1),
resourceLimits: this._options.resourceLimits ?? {},
setupArgs: this._options.setupArgs ?? [],
workerHeartbeatTimeout: this._options.workerHeartbeatTimeout ?? 10000,
};

if (this._options.WorkerPool) {
Expand All @@ -112,6 +120,38 @@ export class Worker {
this._bindExposedWorkerMethods(workerPath, this._options);
}

public static create = async (
workerPath: string,
options?: FarmOptions,
): Promise<Worker> => {
const setUpInspector = async () => {
// Open V8 Inspector
inspector.open();

const inspectorUrl = inspector.url();
if (inspectorUrl) {
const session = new inspector.Session();
session.connect();
await new Promise<void>((resolve, reject) => {
session.post('Debugger.enable', (err: Error) => {
if (err === null) {
resolve();
} else {
reject(err);
}
});
});
return session;
}
return undefined;
};

const inspectorSession = await setUpInspector();
const jestWorker = new Worker(workerPath, inspectorSession, options);

return jestWorker;
};

private _bindExposedWorkerMethods(
workerPath: string,
options: FarmOptions,
Expand Down Expand Up @@ -154,6 +194,8 @@ export class Worker {
throw new Error('Farm is ended, no more calls can be done to it');
}
this._ending = true;
this._inspectorSession?.disconnect();
inspector.close();

return this._workerPool.end();
}
Expand Down
Loading