This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,490 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = exports.SIGKILL_DELAY = void 0;
function _child_process() {
const data = require('child_process');
_child_process = function () {
return data;
};
return data;
}
function _os() {
const data = require('os');
_os = function () {
return data;
};
return data;
}
function _mergeStream() {
const data = _interopRequireDefault(require('merge-stream'));
_mergeStream = function () {
return data;
};
return data;
}
function _supportsColor() {
const data = require('supports-color');
_supportsColor = function () {
return data;
};
return data;
}
var _types = require('../types');
var _WorkerAbstract = _interopRequireDefault(require('./WorkerAbstract'));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const SIGNAL_BASE_EXIT_CODE = 128;
const SIGKILL_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 9;
const SIGTERM_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 15;
// How long to wait after SIGTERM before sending SIGKILL
const SIGKILL_DELAY = 500;
/**
* This class wraps the child process and provides a nice interface to
* communicate with. It takes care of:
*
* - Re-spawning the process if it dies.
* - Queues calls while the worker is busy.
* - Re-sends the requests if the worker blew up.
*
* The reason for queueing them here (since childProcess.send also has an
* internal queue) is because the worker could be doing asynchronous work, and
* this would lead to the child process to read its receiving buffer and start a
* second call. By queueing calls here, we don't send the next call to the
* children until we receive the result of the previous one.
*
* As soon as a request starts to be processed by a worker, its "processed"
* field is changed to "true", so that other workers which might encounter the
* same call skip it.
*/
exports.SIGKILL_DELAY = SIGKILL_DELAY;
class ChildProcessWorker extends _WorkerAbstract.default {
_child;
_options;
_request;
_retries;
_onProcessEnd;
_onCustomMessage;
_stdout;
_stderr;
_stderrBuffer = [];
_memoryUsagePromise;
_resolveMemoryUsage;
_childIdleMemoryUsage;
_childIdleMemoryUsageLimit;
_memoryUsageCheck = false;
_childWorkerPath;
constructor(options) {
super(options);
this._options = options;
this._request = null;
this._stdout = null;
this._stderr = null;
this._childIdleMemoryUsage = null;
this._childIdleMemoryUsageLimit = options.idleMemoryLimit || null;
this._childWorkerPath =
options.childWorkerPath || require.resolve('./processChild');
this.state = _types.WorkerStates.STARTING;
this.initialize();
}
initialize() {
if (
this.state === _types.WorkerStates.OUT_OF_MEMORY ||
this.state === _types.WorkerStates.SHUTTING_DOWN ||
this.state === _types.WorkerStates.SHUT_DOWN
) {
return;
}
if (this._child && this._child.connected) {
this._child.kill('SIGKILL');
}
this.state = _types.WorkerStates.STARTING;
const forceColor = _supportsColor().stdout
? {
FORCE_COLOR: '1'
}
: {};
const silent = this._options.silent ?? true;
if (!silent) {
// NOTE: Detecting an out of memory crash is independent of idle memory usage monitoring. We want to
// monitor for a crash occurring so that it can be handled as required and so we can tell the difference
// between an OOM crash and another kind of crash. We need to do this because if a worker crashes due to
// an OOM event sometimes it isn't seen by the worker pool and it just sits there waiting for the worker
// to respond and it never will.
console.warn('Unable to detect out of memory event if silent === false');
}
this._stderrBuffer = [];
const options = {
cwd: process.cwd(),
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1),
// 0-indexed workerId, 1-indexed JEST_WORKER_ID
...forceColor
},
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
// default to advanced serialization in order to match worker threads
serialization: 'advanced',
silent,
...this._options.forkOptions
};
this._child = (0, _child_process().fork)(
this._childWorkerPath,
[],
options
);
if (this._child.stdout) {
if (!this._stdout) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stdout = (0, _mergeStream().default)(this._getFakeStream());
}
this._stdout.add(this._child.stdout);
}
if (this._child.stderr) {
if (!this._stderr) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stderr = (0, _mergeStream().default)(this._getFakeStream());
}
this._stderr.add(this._child.stderr);
this._child.stderr.on('data', this.stderrDataHandler.bind(this));
}
this._child.on('message', this._onMessage.bind(this));
this._child.on('exit', this._onExit.bind(this));
this._child.on('disconnect', this._onDisconnect.bind(this));
this._child.send([
_types.CHILD_MESSAGE_INITIALIZE,
false,
this._options.workerPath,
this._options.setupArgs
]);
this._retries++;
// If we exceeded the amount of retries, we will emulate an error reply
// coming from the child. This avoids code duplication related with cleaning
// the queue, and scheduling the next call.
if (this._retries > this._options.maxRetries) {
const error = new Error(
`Jest worker encountered ${this._retries} child process exceptions, exceeding retry limit`
);
this._onMessage([
_types.PARENT_MESSAGE_CLIENT_ERROR,
error.name,
error.message,
error.stack,
{
type: 'WorkerError'
}
]);
// Clear the request so we don't keep executing it.
this._request = null;
}
this.state = _types.WorkerStates.OK;
if (this._resolveWorkerReady) {
this._resolveWorkerReady();
}
}
stderrDataHandler(chunk) {
if (chunk) {
this._stderrBuffer.push(Buffer.from(chunk));
}
this._detectOutOfMemoryCrash();
if (this.state === _types.WorkerStates.OUT_OF_MEMORY) {
this._workerReadyPromise = undefined;
this._resolveWorkerReady = undefined;
this.killChild();
this._shutdown();
}
}
_detectOutOfMemoryCrash() {
try {
const bufferStr = Buffer.concat(this._stderrBuffer).toString('utf8');
if (
bufferStr.includes('heap out of memory') ||
bufferStr.includes('allocation failure;') ||
bufferStr.includes('Last few GCs')
) {
if (
this.state === _types.WorkerStates.OK ||
this.state === _types.WorkerStates.STARTING
) {
this.state = _types.WorkerStates.OUT_OF_MEMORY;
}
}
} catch (err) {
console.error('Error looking for out of memory crash', err);
}
}
_onDisconnect() {
this._workerReadyPromise = undefined;
this._resolveWorkerReady = undefined;
this._detectOutOfMemoryCrash();
if (this.state === _types.WorkerStates.OUT_OF_MEMORY) {
this.killChild();
this._shutdown();
}
}
_onMessage(response) {
// Ignore messages not intended for us
if (!Array.isArray(response)) return;
// TODO: Add appropriate type check
let error;
switch (response[0]) {
case _types.PARENT_MESSAGE_OK:
this._onProcessEnd(null, response[1]);
break;
case _types.PARENT_MESSAGE_CLIENT_ERROR:
error = response[4];
if (error != null && typeof error === 'object') {
const extra = error;
// @ts-expect-error: no index
const NativeCtor = globalThis[response[1]];
const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
error = new Ctor(response[2]);
error.type = response[1];
error.stack = response[3];
for (const key in extra) {
error[key] = extra[key];
}
}
this._onProcessEnd(error, null);
break;
case _types.PARENT_MESSAGE_SETUP_ERROR:
error = new Error(`Error when calling setup: ${response[2]}`);
error.type = response[1];
error.stack = response[3];
this._onProcessEnd(error, null);
break;
case _types.PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
case _types.PARENT_MESSAGE_MEM_USAGE:
this._childIdleMemoryUsage = response[1];
if (this._resolveMemoryUsage) {
this._resolveMemoryUsage(response[1]);
this._resolveMemoryUsage = undefined;
this._memoryUsagePromise = undefined;
}
this._performRestartIfRequired();
break;
default:
// Ignore messages not intended for us
break;
}
}
_performRestartIfRequired() {
if (this._memoryUsageCheck) {
this._memoryUsageCheck = false;
let limit = this._childIdleMemoryUsageLimit;
// TODO: At some point it would make sense to make use of
// stringToBytes found in jest-config, however as this
// package does not have any dependencies on an other jest
// packages that can wait until some other time.
if (limit && limit > 0 && limit <= 1) {
limit = Math.floor((0, _os().totalmem)() * limit);
} else if (limit) {
limit = Math.floor(limit);
}
if (
limit &&
this._childIdleMemoryUsage &&
this._childIdleMemoryUsage > limit
) {
this.state = _types.WorkerStates.RESTARTING;
this.killChild();
}
}
}
_onExit(exitCode, signal) {
this._workerReadyPromise = undefined;
this._resolveWorkerReady = undefined;
this._detectOutOfMemoryCrash();
if (exitCode !== 0 && this.state === _types.WorkerStates.OUT_OF_MEMORY) {
this._onProcessEnd(
new Error('Jest worker ran out of memory and crashed'),
null
);
this._shutdown();
} else if (
(exitCode !== 0 &&
exitCode !== null &&
exitCode !== SIGTERM_EXIT_CODE &&
exitCode !== SIGKILL_EXIT_CODE &&
this.state !== _types.WorkerStates.SHUTTING_DOWN) ||
this.state === _types.WorkerStates.RESTARTING
) {
this.state = _types.WorkerStates.RESTARTING;
this.initialize();
if (this._request) {
this._child.send(this._request);
}
} else {
// At this point, it's not clear why the child process exited. There could
// be several reasons:
//
// 1. The child process exited successfully after finishing its work.
// This is the most likely case.
// 2. The child process crashed in a manner that wasn't caught through
// any of the heuristic-based checks above.
// 3. The child process was killed by another process or daemon unrelated
// to Jest. For example, oom-killer on Linux may have picked the child
// process to kill because overall system memory is constrained.
//
// If there's a pending request to the child process in any of those
// situations, the request still needs to be handled in some manner before
// entering the shutdown phase. Otherwise the caller expecting a response
// from the worker will never receive indication that something unexpected
// happened and hang forever.
//
// In normal operation, the request is handled and cleared before the
// child process exits. If it's still present, it's not clear what
// happened and probably best to throw an error. In practice, this usually
// happens when the child process is killed externally.
//
// There's a reasonable argument that the child process should be retried
// with request re-sent in this scenario. However, if the problem was due
// to situations such as oom-killer attempting to free up system
// resources, retrying would exacerbate the problem.
const isRequestStillPending = !!this._request;
if (isRequestStillPending) {
// If a signal is present, we can be reasonably confident the process
// was killed externally. Log this fact so it's more clear to users that
// something went wrong externally, rather than a bug in Jest itself.
const error = new Error(
signal != null
? `A jest worker process (pid=${this._child.pid}) was terminated by another process: signal=${signal}, exitCode=${exitCode}. Operating system logs may contain more information on why this occurred.`
: `A jest worker process (pid=${this._child.pid}) crashed for an unknown reason: exitCode=${exitCode}`
);
this._onProcessEnd(error, null);
}
this._shutdown();
}
}
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
this._stderrBuffer = [];
onProcessStart(this);
this._onProcessEnd = (...args) => {
const hasRequest = !!this._request;
// Clean the request to avoid sending past requests to workers that fail
// while waiting for a new request (timers, unhandled rejections...)
this._request = null;
if (
this._childIdleMemoryUsageLimit &&
this._child.connected &&
hasRequest
) {
this.checkMemoryUsage();
}
return onProcessEnd(...args);
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
// eslint-disable-next-line @typescript-eslint/no-empty-function
this._child.send(request, () => {});
}
waitForExit() {
return this._exitPromise;
}
killChild() {
// We store a reference so that there's no way we can accidentally
// kill a new worker that has been spawned.
const childToKill = this._child;
childToKill.kill('SIGTERM');
return setTimeout(() => childToKill.kill('SIGKILL'), SIGKILL_DELAY);
}
forceExit() {
this.state = _types.WorkerStates.SHUTTING_DOWN;
const sigkillTimeout = this.killChild();
this._exitPromise.then(() => clearTimeout(sigkillTimeout));
}
getWorkerId() {
return this._options.workerId;
}
/**
* Gets the process id of the worker.
*
* @returns Process id.
*/
getWorkerSystemId() {
return this._child.pid;
}
getStdout() {
return this._stdout;
}
getStderr() {
return this._stderr;
}
/**
* Gets the last reported memory usage.
*
* @returns Memory usage in bytes.
*/
getMemoryUsage() {
if (!this._memoryUsagePromise) {
let rejectCallback;
const promise = new Promise((resolve, reject) => {
this._resolveMemoryUsage = resolve;
rejectCallback = reject;
});
this._memoryUsagePromise = promise;
if (!this._child.connected && rejectCallback) {
rejectCallback(new Error('Child process is not running.'));
this._memoryUsagePromise = undefined;
this._resolveMemoryUsage = undefined;
return promise;
}
this._child.send([_types.CHILD_MESSAGE_MEM_USAGE], err => {
if (err && rejectCallback) {
this._memoryUsagePromise = undefined;
this._resolveMemoryUsage = undefined;
rejectCallback(err);
}
});
return promise;
}
return this._memoryUsagePromise;
}
/**
* Gets updated memory usage and restarts if required
*/
checkMemoryUsage() {
if (this._childIdleMemoryUsageLimit) {
this._memoryUsageCheck = true;
this._child.send([_types.CHILD_MESSAGE_MEM_USAGE], err => {
if (err) {
console.error('Unable to check memory usage', err);
}
});
} else {
console.warn(
'Memory usage of workers can only be checked if a limit is set'
);
}
}
isWorkerRunning() {
return this._child.connected && !this._child.killed;
}
}
exports.default = ChildProcessWorker;

View File

@@ -0,0 +1,359 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
function _os() {
const data = require('os');
_os = function () {
return data;
};
return data;
}
function _worker_threads() {
const data = require('worker_threads');
_worker_threads = function () {
return data;
};
return data;
}
function _mergeStream() {
const data = _interopRequireDefault(require('merge-stream'));
_mergeStream = function () {
return data;
};
return data;
}
var _types = require('../types');
var _WorkerAbstract = _interopRequireDefault(require('./WorkerAbstract'));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class ExperimentalWorker extends _WorkerAbstract.default {
_worker;
_options;
_request;
_retries;
_onProcessEnd;
_onCustomMessage;
_stdout;
_stderr;
_memoryUsagePromise;
_resolveMemoryUsage;
_childWorkerPath;
_childIdleMemoryUsage;
_childIdleMemoryUsageLimit;
_memoryUsageCheck = false;
constructor(options) {
super(options);
this._options = options;
this._request = null;
this._stdout = null;
this._stderr = null;
this._childWorkerPath =
options.childWorkerPath || require.resolve('./threadChild');
this._childIdleMemoryUsage = null;
this._childIdleMemoryUsageLimit = options.idleMemoryLimit || null;
this.initialize();
}
initialize() {
if (
this.state === _types.WorkerStates.OUT_OF_MEMORY ||
this.state === _types.WorkerStates.SHUTTING_DOWN ||
this.state === _types.WorkerStates.SHUT_DOWN
) {
return;
}
if (this._worker) {
this._worker.terminate();
}
this.state = _types.WorkerStates.STARTING;
this._worker = new (_worker_threads().Worker)(this._childWorkerPath, {
eval: false,
resourceLimits: this._options.resourceLimits,
stderr: true,
stdout: true,
workerData: this._options.workerData,
...this._options.forkOptions
});
if (this._worker.stdout) {
if (!this._stdout) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stdout = (0, _mergeStream().default)(this._getFakeStream());
}
this._stdout.add(this._worker.stdout);
}
if (this._worker.stderr) {
if (!this._stderr) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stderr = (0, _mergeStream().default)(this._getFakeStream());
}
this._stderr.add(this._worker.stderr);
}
// This can be useful for debugging.
if (!(this._options.silent ?? true)) {
this._worker.stdout.setEncoding('utf8');
// eslint-disable-next-line no-console
this._worker.stdout.on('data', console.log);
this._worker.stderr.setEncoding('utf8');
this._worker.stderr.on('data', console.error);
}
this._worker.on('message', this._onMessage.bind(this));
this._worker.on('exit', this._onExit.bind(this));
this._worker.on('error', this._onError.bind(this));
this._worker.postMessage([
_types.CHILD_MESSAGE_INITIALIZE,
false,
this._options.workerPath,
this._options.setupArgs,
String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
]);
this._retries++;
// If we exceeded the amount of retries, we will emulate an error reply
// coming from the child. This avoids code duplication related with cleaning
// the queue, and scheduling the next call.
if (this._retries > this._options.maxRetries) {
const error = new Error('Call retries were exceeded');
this._onMessage([
_types.PARENT_MESSAGE_CLIENT_ERROR,
error.name,
error.message,
error.stack,
{
type: 'WorkerError'
}
]);
}
this.state = _types.WorkerStates.OK;
if (this._resolveWorkerReady) {
this._resolveWorkerReady();
}
}
_onError(error) {
if (error.message.includes('heap out of memory')) {
this.state = _types.WorkerStates.OUT_OF_MEMORY;
// Threads don't behave like processes, they don't crash when they run out of
// memory. But for consistency we want them to behave like processes so we call
// terminate to simulate a crash happening that was not planned
this._worker.terminate();
}
}
_onMessage(response) {
// Ignore messages not intended for us
if (!Array.isArray(response)) return;
let error;
switch (response[0]) {
case _types.PARENT_MESSAGE_OK:
this._onProcessEnd(null, response[1]);
break;
case _types.PARENT_MESSAGE_CLIENT_ERROR:
error = response[4];
if (error != null && typeof error === 'object') {
const extra = error;
// @ts-expect-error: no index
const NativeCtor = globalThis[response[1]];
const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
error = new Ctor(response[2]);
error.type = response[1];
error.stack = response[3];
for (const key in extra) {
// @ts-expect-error: no index
error[key] = extra[key];
}
}
this._onProcessEnd(error, null);
break;
case _types.PARENT_MESSAGE_SETUP_ERROR:
error = new Error(`Error when calling setup: ${response[2]}`);
// @ts-expect-error: adding custom properties to errors.
error.type = response[1];
error.stack = response[3];
this._onProcessEnd(error, null);
break;
case _types.PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
case _types.PARENT_MESSAGE_MEM_USAGE:
this._childIdleMemoryUsage = response[1];
if (this._resolveMemoryUsage) {
this._resolveMemoryUsage(response[1]);
this._resolveMemoryUsage = undefined;
this._memoryUsagePromise = undefined;
}
this._performRestartIfRequired();
break;
default:
// Ignore messages not intended for us
break;
}
}
_onExit(exitCode) {
this._workerReadyPromise = undefined;
this._resolveWorkerReady = undefined;
if (exitCode !== 0 && this.state === _types.WorkerStates.OUT_OF_MEMORY) {
this._onProcessEnd(
new Error('Jest worker ran out of memory and crashed'),
null
);
this._shutdown();
} else if (
(exitCode !== 0 &&
this.state !== _types.WorkerStates.SHUTTING_DOWN &&
this.state !== _types.WorkerStates.SHUT_DOWN) ||
this.state === _types.WorkerStates.RESTARTING
) {
this.initialize();
if (this._request) {
this._worker.postMessage(this._request);
}
} else {
// If the worker thread exits while a request is still pending, throw an
// error. This is unexpected and tests may not have run to completion.
const isRequestStillPending = !!this._request;
if (isRequestStillPending) {
this._onProcessEnd(
new Error(
'A Jest worker thread exited unexpectedly before finishing tests for an unknown reason. One of the ways this can happen is if process.exit() was called in testing code.'
),
null
);
}
this._shutdown();
}
}
waitForExit() {
return this._exitPromise;
}
forceExit() {
this.state = _types.WorkerStates.SHUTTING_DOWN;
this._worker.terminate();
}
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
onProcessStart(this);
this._onProcessEnd = (...args) => {
const hasRequest = !!this._request;
// Clean the request to avoid sending past requests to workers that fail
// while waiting for a new request (timers, unhandled rejections...)
this._request = null;
if (this._childIdleMemoryUsageLimit && hasRequest) {
this.checkMemoryUsage();
}
const res = onProcessEnd?.(...args);
// Clean up the reference so related closures can be garbage collected.
onProcessEnd = null;
return res;
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
this._worker.postMessage(request);
}
getWorkerId() {
return this._options.workerId;
}
getStdout() {
return this._stdout;
}
getStderr() {
return this._stderr;
}
_performRestartIfRequired() {
if (this._memoryUsageCheck) {
this._memoryUsageCheck = false;
let limit = this._childIdleMemoryUsageLimit;
// TODO: At some point it would make sense to make use of
// stringToBytes found in jest-config, however as this
// package does not have any dependencies on an other jest
// packages that can wait until some other time.
if (limit && limit > 0 && limit <= 1) {
limit = Math.floor((0, _os().totalmem)() * limit);
} else if (limit) {
limit = Math.floor(limit);
}
if (
limit &&
this._childIdleMemoryUsage &&
this._childIdleMemoryUsage > limit
) {
this.state = _types.WorkerStates.RESTARTING;
this._worker.terminate();
}
}
}
/**
* Gets the last reported memory usage.
*
* @returns Memory usage in bytes.
*/
getMemoryUsage() {
if (!this._memoryUsagePromise) {
let rejectCallback;
const promise = new Promise((resolve, reject) => {
this._resolveMemoryUsage = resolve;
rejectCallback = reject;
});
this._memoryUsagePromise = promise;
if (!this._worker.threadId) {
rejectCallback(new Error('Child process is not running.'));
this._memoryUsagePromise = undefined;
this._resolveMemoryUsage = undefined;
return promise;
}
try {
this._worker.postMessage([_types.CHILD_MESSAGE_MEM_USAGE]);
} catch (err) {
this._memoryUsagePromise = undefined;
this._resolveMemoryUsage = undefined;
rejectCallback(err);
}
return promise;
}
return this._memoryUsagePromise;
}
/**
* Gets updated memory usage and restarts if required
*/
checkMemoryUsage() {
if (this._childIdleMemoryUsageLimit) {
this._memoryUsageCheck = true;
this._worker.postMessage([_types.CHILD_MESSAGE_MEM_USAGE]);
} else {
console.warn(
'Memory usage of workers can only be checked if a limit is set'
);
}
}
/**
* Gets the thread id of the worker.
*
* @returns Thread id.
*/
getWorkerSystemId() {
return this._worker.threadId;
}
isWorkerRunning() {
return this._worker.threadId >= 0;
}
}
exports.default = ExperimentalWorker;

View File

@@ -0,0 +1,135 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
function _stream() {
const data = require('stream');
_stream = function () {
return data;
};
return data;
}
var _types = require('../types');
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class WorkerAbstract extends _stream().EventEmitter {
/**
* DO NOT WRITE TO THIS DIRECTLY.
* Use this.state getter/setters so events are emitted correctly.
*/
#state = _types.WorkerStates.STARTING;
_fakeStream = null;
_exitPromise;
_resolveExitPromise;
_workerReadyPromise;
_resolveWorkerReady;
get state() {
return this.#state;
}
set state(value) {
if (this.#state !== value) {
const oldState = this.#state;
this.#state = value;
this.emit(_types.WorkerEvents.STATE_CHANGE, value, oldState);
}
}
constructor(options) {
super();
if (typeof options.on === 'object') {
for (const [event, handlers] of Object.entries(options.on)) {
// Can't do Array.isArray on a ReadonlyArray<T>.
// https://github.com/microsoft/TypeScript/issues/17002
if (typeof handlers === 'function') {
super.on(event, handlers);
} else {
for (const handler of handlers) {
super.on(event, handler);
}
}
}
}
this._exitPromise = new Promise(resolve => {
this._resolveExitPromise = resolve;
});
this._exitPromise.then(() => {
this.state = _types.WorkerStates.SHUT_DOWN;
});
}
/**
* Wait for the worker child process to be ready to handle requests.
*
* @returns Promise which resolves when ready.
*/
waitForWorkerReady() {
if (!this._workerReadyPromise) {
this._workerReadyPromise = new Promise((resolve, reject) => {
let settled = false;
let to;
switch (this.state) {
case _types.WorkerStates.OUT_OF_MEMORY:
case _types.WorkerStates.SHUTTING_DOWN:
case _types.WorkerStates.SHUT_DOWN:
settled = true;
reject(
new Error(
`Worker state means it will never be ready: ${this.state}`
)
);
break;
case _types.WorkerStates.STARTING:
case _types.WorkerStates.RESTARTING:
this._resolveWorkerReady = () => {
settled = true;
resolve();
if (to) {
clearTimeout(to);
}
};
break;
case _types.WorkerStates.OK:
settled = true;
resolve();
break;
}
if (!settled) {
to = setTimeout(() => {
if (!settled) {
reject(new Error('Timeout starting worker'));
}
}, 500);
}
});
}
return this._workerReadyPromise;
}
/**
* Used to shut down the current working instance once the children have been
* killed off.
*/
_shutdown() {
this.state === _types.WorkerStates.SHUT_DOWN;
// End the permanent stream so the merged stream end too
if (this._fakeStream) {
this._fakeStream.end();
this._fakeStream = null;
}
this._resolveExitPromise();
}
_getFakeStream() {
if (!this._fakeStream) {
this._fakeStream = new (_stream().PassThrough)();
}
return this._fakeStream;
}
}
exports.default = WorkerAbstract;

View File

@@ -0,0 +1,33 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = messageParent;
function _worker_threads() {
const data = require('worker_threads');
_worker_threads = function () {
return data;
};
return data;
}
var _types = require('../types');
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
function messageParent(message, parentProcess = process) {
if (!_worker_threads().isMainThread && _worker_threads().parentPort != null) {
_worker_threads().parentPort.postMessage([
_types.PARENT_MESSAGE_CUSTOM,
message
]);
} else if (typeof parentProcess.send === 'function') {
parentProcess.send([_types.PARENT_MESSAGE_CUSTOM, message]);
} else {
throw new Error('"messageParent" can only be used inside a worker');
}
}

159
node_modules/jest-worker/build/workers/processChild.js generated vendored Normal file
View File

@@ -0,0 +1,159 @@
'use strict';
function _jestUtil() {
const data = require('jest-util');
_jestUtil = function () {
return data;
};
return data;
}
var _types = require('../types');
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
let file = null;
let setupArgs = [];
let initialized = false;
/**
* This file is a small bootstrapper for workers. It sets up the communication
* between the worker and the parent process, interpreting parent messages and
* sending results back.
*
* The file loaded will be lazily initialized the first time any of the workers
* is called. This is done for optimal performance: if the farm is initialized,
* but no call is made to it, child Node processes will be consuming the least
* possible amount of memory.
*
* If an invalid message is detected, the child will exit (by throwing) with a
* non-zero exit code.
*/
const messageListener = request => {
switch (request[0]) {
case _types.CHILD_MESSAGE_INITIALIZE:
const init = request;
file = init[2];
setupArgs = init[3];
break;
case _types.CHILD_MESSAGE_CALL:
const call = request;
execMethod(call[2], call[3]);
break;
case _types.CHILD_MESSAGE_END:
end();
break;
case _types.CHILD_MESSAGE_MEM_USAGE:
reportMemoryUsage();
break;
case _types.CHILD_MESSAGE_CALL_SETUP:
if (initialized) {
reportSuccess(void 0);
} else {
const main = require(file);
initialized = true;
if (main.setup) {
execFunction(
main.setup,
main,
setupArgs,
reportSuccess,
reportInitializeError
);
} else {
reportSuccess(void 0);
}
}
break;
default:
throw new TypeError(
`Unexpected request from parent process: ${request[0]}`
);
}
};
process.on('message', messageListener);
function reportSuccess(result) {
if (!process || !process.send) {
throw new Error('Child can only be used on a forked process');
}
process.send([_types.PARENT_MESSAGE_OK, result]);
}
function reportClientError(error) {
return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
}
function reportInitializeError(error) {
return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
}
function reportMemoryUsage() {
if (!process || !process.send) {
throw new Error('Child can only be used on a forked process');
}
const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed];
process.send(msg);
}
function reportError(error, type) {
if (!process || !process.send) {
throw new Error('Child can only be used on a forked process');
}
if (error == null) {
error = new Error('"null" or "undefined" thrown');
}
process.send([
type,
error.constructor && error.constructor.name,
error.message,
error.stack,
typeof error === 'object'
? {
...error
}
: error
]);
}
function end() {
const main = require(file);
if (!main.teardown) {
exitProcess();
return;
}
execFunction(main.teardown, main, [], exitProcess, exitProcess);
}
function exitProcess() {
// Clean up open handles so the process ideally exits gracefully
process.removeListener('message', messageListener);
}
function execMethod(method, args) {
const main = require(file);
let fn;
if (method === 'default') {
fn = main.__esModule ? main.default : main;
} else {
fn = main[method];
}
function execHelper() {
execFunction(fn, main, args, reportSuccess, reportClientError);
}
if (initialized || !main.setup) {
execHelper();
return;
}
initialized = true;
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}
function execFunction(fn, ctx, args, onResult, onError) {
let result;
try {
result = fn.apply(ctx, args);
} catch (err) {
onError(err);
return;
}
if ((0, _jestUtil().isPromise)(result)) {
result.then(onResult, onError);
} else {
onResult(result);
}
}

177
node_modules/jest-worker/build/workers/threadChild.js generated vendored Normal file
View File

@@ -0,0 +1,177 @@
'use strict';
function _worker_threads() {
const data = require('worker_threads');
_worker_threads = function () {
return data;
};
return data;
}
function _jestUtil() {
const data = require('jest-util');
_jestUtil = function () {
return data;
};
return data;
}
var _types = require('../types');
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
let file = null;
let setupArgs = [];
let initialized = false;
/**
* This file is a small bootstrapper for workers. It sets up the communication
* between the worker and the parent process, interpreting parent messages and
* sending results back.
*
* The file loaded will be lazily initialized the first time any of the workers
* is called. This is done for optimal performance: if the farm is initialized,
* but no call is made to it, child Node processes will be consuming the least
* possible amount of memory.
*
* If an invalid message is detected, the child will exit (by throwing) with a
* non-zero exit code.
*/
const messageListener = request => {
switch (request[0]) {
case _types.CHILD_MESSAGE_INITIALIZE:
const init = request;
file = init[2];
setupArgs = init[3];
process.env.JEST_WORKER_ID = init[4];
break;
case _types.CHILD_MESSAGE_CALL:
const call = request;
execMethod(call[2], call[3]);
break;
case _types.CHILD_MESSAGE_END:
end();
break;
case _types.CHILD_MESSAGE_MEM_USAGE:
reportMemoryUsage();
break;
case _types.CHILD_MESSAGE_CALL_SETUP:
if (initialized) {
reportSuccess(void 0);
} else {
const main = require(file);
initialized = true;
if (main.setup) {
execFunction(
main.setup,
main,
setupArgs,
reportSuccess,
reportInitializeError
);
} else {
reportSuccess(void 0);
}
}
break;
default:
throw new TypeError(
`Unexpected request from parent process: ${request[0]}`
);
}
};
_worker_threads().parentPort.on('message', messageListener);
function reportMemoryUsage() {
if (_worker_threads().isMainThread) {
throw new Error('Child can only be used on a forked process');
}
const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed];
_worker_threads().parentPort.postMessage(msg);
}
function reportSuccess(result) {
if (_worker_threads().isMainThread) {
throw new Error('Child can only be used on a forked process');
}
try {
_worker_threads().parentPort.postMessage([
_types.PARENT_MESSAGE_OK,
result
]);
} catch (err) {
// Handling it here to avoid unhandled `DataCloneError` rejection
// which is hard to distinguish on the parent side
// (such error doesn't have any message or stack trace)
reportClientError(err);
}
}
function reportClientError(error) {
return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
}
function reportInitializeError(error) {
return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
}
function reportError(error, type) {
if (_worker_threads().isMainThread) {
throw new Error('Child can only be used on a forked process');
}
if (error == null) {
error = new Error('"null" or "undefined" thrown');
}
_worker_threads().parentPort.postMessage([
type,
error.constructor && error.constructor.name,
error.message,
error.stack,
typeof error === 'object'
? {
...error
}
: error
]);
}
function end() {
const main = require(file);
if (!main.teardown) {
exitProcess();
return;
}
execFunction(main.teardown, main, [], exitProcess, exitProcess);
}
function exitProcess() {
// Clean up open handles so the worker ideally exits gracefully
_worker_threads().parentPort.removeListener('message', messageListener);
}
function execMethod(method, args) {
const main = require(file);
let fn;
if (method === 'default') {
fn = main.__esModule ? main.default : main;
} else {
fn = main[method];
}
function execHelper() {
execFunction(fn, main, args, reportSuccess, reportClientError);
}
if (initialized || !main.setup) {
execHelper();
return;
}
initialized = true;
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}
function execFunction(fn, ctx, args, onResult, onError) {
let result;
try {
result = fn.apply(ctx, args);
} catch (err) {
onError(err);
return;
}
if ((0, _jestUtil().isPromise)(result)) {
result.then(onResult, onError);
} else {
onResult(result);
}
}