0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-worker-exit-code.js
Rich Trott dcccf9ab9b test: move shared module to test/common
`test/fixtures/process-exit-code-cases.js` is a shared module and should
be in `test/common` (so it gets linted, etc.) and documented in
`test/common/README.md`.

PR-URL: https://github.com/nodejs/node/pull/54042
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2024-07-27 08:52:28 -07:00

47 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
// This test checks that Worker has correct exit codes on parent side
// in multiple situations.
const assert = require('assert');
const worker = require('worker_threads');
const { Worker, parentPort } = worker;
const { getTestCases } = require('../common/process-exit-code-cases');
const testCases = getTestCases(true);
// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
process.env.HAS_STARTED_WORKER = 1;
parent();
} else {
if (!parentPort) {
console.error('Parent port must not be null');
process.exit(100);
return;
}
parentPort.once('message', (msg) => testCases[msg].func());
}
function parent() {
const test = (arg, name = 'worker', exit, error = null) => {
const w = new Worker(__filename);
w.on('exit', common.mustCall((code) => {
assert.strictEqual(
code, exit,
`wrong exit for ${arg}-${name}\nexpected:${exit} but got:${code}`);
console.log(`ok - ${arg} exited with ${exit}`);
}));
if (error) {
w.on('error', common.mustCall((err) => {
console.log(err);
assert.match(String(err), error);
}));
}
w.postMessage(arg);
};
testCases.forEach((tc, i) => test(i, tc.func.name, tc.result, tc.error));
}