test: refactor process/worker exitCode tests
PR-URL: https://github.com/nodejs/node/pull/21739 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
35326f27fd
commit
19e10ec2e2
113
test/fixtures/process-exit-code-cases.js
vendored
Normal file
113
test/fixtures/process-exit-code-cases.js
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const cases = [];
|
||||
module.exports = cases;
|
||||
|
||||
function exitsOnExitCodeSet() {
|
||||
process.exitCode = 42;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 42);
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
}
|
||||
cases.push({ func: exitsOnExitCodeSet, result: 42 });
|
||||
|
||||
function changesCodeViaExit() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 42);
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
process.exit(42);
|
||||
}
|
||||
cases.push({ func: changesCodeViaExit, result: 42 });
|
||||
|
||||
function changesCodeZeroExit() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 0);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.exit(0);
|
||||
}
|
||||
cases.push({ func: changesCodeZeroExit, result: 0 });
|
||||
|
||||
function exitWithOneOnUncaught() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
// cannot use assert because it will be uncaughtException -> 1 exit code
|
||||
// that will render this test useless
|
||||
if (code !== 1 || process.exitCode !== 1) {
|
||||
console.log('wrong code! expected 1 for uncaughtException');
|
||||
process.exit(99);
|
||||
}
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
cases.push({
|
||||
func: exitWithOneOnUncaught,
|
||||
result: 1,
|
||||
error: /^Error: ok$/,
|
||||
});
|
||||
|
||||
function changeCodeInsideExit() {
|
||||
process.exitCode = 95;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 95);
|
||||
assert.strictEqual(code, 95);
|
||||
process.exitCode = 99;
|
||||
});
|
||||
}
|
||||
cases.push({ func: changeCodeInsideExit, result: 99 });
|
||||
|
||||
function zeroExitWithUncaughtHandler() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 0);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.on('uncaughtException', () => {});
|
||||
throw new Error('ok');
|
||||
}
|
||||
cases.push({ func: zeroExitWithUncaughtHandler, result: 0 });
|
||||
|
||||
function changeCodeInUncaughtHandler() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 97);
|
||||
assert.strictEqual(code, 97);
|
||||
});
|
||||
process.on('uncaughtException', () => {
|
||||
process.exitCode = 97;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
cases.push({ func: changeCodeInUncaughtHandler, result: 97 });
|
||||
|
||||
function changeCodeInExitWithUncaught() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 98;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
cases.push({
|
||||
func: changeCodeInExitWithUncaught,
|
||||
result: 98,
|
||||
error: /^Error: ok$/,
|
||||
});
|
||||
|
||||
function exitWithZeroInExitWithUncaught() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 0;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
cases.push({
|
||||
func: exitWithZeroInExitWithUncaught,
|
||||
result: 0,
|
||||
error: /^Error: ok$/,
|
||||
});
|
@ -23,113 +23,18 @@
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
switch (process.argv[2]) {
|
||||
case 'child1':
|
||||
return child1();
|
||||
case 'child2':
|
||||
return child2();
|
||||
case 'child3':
|
||||
return child3();
|
||||
case 'child4':
|
||||
return child4();
|
||||
case 'child5':
|
||||
return child5();
|
||||
case 'child6':
|
||||
return child6();
|
||||
case 'child7':
|
||||
return child7();
|
||||
case 'child8':
|
||||
return child8();
|
||||
case 'child9':
|
||||
return child9();
|
||||
case undefined:
|
||||
return parent();
|
||||
default:
|
||||
throw new Error('invalid');
|
||||
}
|
||||
const testCases = require('../fixtures/process-exit-code-cases');
|
||||
|
||||
function child1() {
|
||||
process.exitCode = 42;
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 42);
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
}
|
||||
|
||||
function child2() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 42);
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
process.exit(42);
|
||||
}
|
||||
|
||||
function child3() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 0);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function child4() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', function(code) {
|
||||
if (code !== 1 || process.exitCode !== 1) {
|
||||
console.log('wrong code! expected 1 for uncaughtException');
|
||||
process.exit(99);
|
||||
}
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child5() {
|
||||
process.exitCode = 95;
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 95);
|
||||
assert.strictEqual(code, 95);
|
||||
process.exitCode = 99;
|
||||
});
|
||||
}
|
||||
|
||||
function child6() {
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 0);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.on('uncaughtException', () => {});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child7() {
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 97);
|
||||
assert.strictEqual(code, 97);
|
||||
});
|
||||
process.on('uncaughtException', () => {
|
||||
process.exitCode = 97;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child8() {
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 98;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child9() {
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 0;
|
||||
});
|
||||
throw new Error('ok');
|
||||
if (!process.argv[2]) {
|
||||
parent();
|
||||
} else {
|
||||
const i = parseInt(process.argv[2]);
|
||||
if (Number.isNaN(i)) {
|
||||
console.log('Invalid test case index');
|
||||
process.exit(100);
|
||||
return;
|
||||
}
|
||||
testCases[i].func();
|
||||
}
|
||||
|
||||
function parent() {
|
||||
@ -138,22 +43,14 @@ function parent() {
|
||||
const f = __filename;
|
||||
const option = { stdio: [ 0, 1, 'ignore' ] };
|
||||
|
||||
const test = (arg, exit) => {
|
||||
const test = (arg, name = 'child', exit) => {
|
||||
spawn(node, [f, arg], option).on('exit', (code) => {
|
||||
assert.strictEqual(
|
||||
code, exit,
|
||||
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
|
||||
`wrong exit for ${arg}-${name}\nexpected:${exit} but got:${code}`);
|
||||
console.log(`ok - ${arg} exited with ${exit}`);
|
||||
});
|
||||
};
|
||||
|
||||
test('child1', 42);
|
||||
test('child2', 42);
|
||||
test('child3', 0);
|
||||
test('child4', 1);
|
||||
test('child5', 99);
|
||||
test('child6', 0);
|
||||
test('child7', 97);
|
||||
test('child8', 98);
|
||||
test('child9', 0);
|
||||
testCases.forEach((tc, i) => test(i, tc.func.name, tc.result));
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ const assert = require('assert');
|
||||
const worker = require('worker_threads');
|
||||
const { Worker, parentPort } = worker;
|
||||
|
||||
const testCases = require('../fixtures/process-exit-code-cases');
|
||||
|
||||
// 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;
|
||||
@ -19,121 +21,16 @@ if (!process.env.HAS_STARTED_WORKER) {
|
||||
process.exit(100);
|
||||
return;
|
||||
}
|
||||
parentPort.once('message', (msg) => {
|
||||
switch (msg) {
|
||||
case 'child1':
|
||||
return child1();
|
||||
case 'child2':
|
||||
return child2();
|
||||
case 'child3':
|
||||
return child3();
|
||||
case 'child4':
|
||||
return child4();
|
||||
case 'child5':
|
||||
return child5();
|
||||
case 'child6':
|
||||
return child6();
|
||||
case 'child7':
|
||||
return child7();
|
||||
case 'child8':
|
||||
return child8();
|
||||
case 'child9':
|
||||
return child9();
|
||||
default:
|
||||
throw new Error('invalid');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function child1() {
|
||||
process.exitCode = 42;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
}
|
||||
|
||||
function child2() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 42);
|
||||
});
|
||||
process.exit(42);
|
||||
}
|
||||
|
||||
function child3() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function child4() {
|
||||
process.exitCode = 99;
|
||||
process.on('exit', (code) => {
|
||||
// cannot use assert because it will be uncaughtException -> 1 exit code
|
||||
// that will render this test useless
|
||||
if (code !== 1) {
|
||||
console.error('wrong code! expected 1 for uncaughtException');
|
||||
process.exit(99);
|
||||
}
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child5() {
|
||||
process.exitCode = 95;
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 95);
|
||||
process.exitCode = 99;
|
||||
});
|
||||
}
|
||||
|
||||
function child6() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
process.on('uncaughtException', common.mustCall(() => {
|
||||
// handle
|
||||
}));
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child7() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(code, 97);
|
||||
});
|
||||
process.on('uncaughtException', common.mustCall(() => {
|
||||
process.exitCode = 97;
|
||||
}));
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child8() {
|
||||
process.on('exit', (code) => {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 98;
|
||||
});
|
||||
throw new Error('ok');
|
||||
}
|
||||
|
||||
function child9() {
|
||||
process.on('exit', function(code) {
|
||||
assert.strictEqual(process.exitCode, 1);
|
||||
assert.strictEqual(code, 1);
|
||||
process.exitCode = 0;
|
||||
});
|
||||
throw new Error('ok');
|
||||
parentPort.once('message', (msg) => testCases[msg].func());
|
||||
}
|
||||
|
||||
function parent() {
|
||||
const test = (arg, exit, error = null) => {
|
||||
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}\nexpected:${exit} but got:${code}`);
|
||||
`wrong exit for ${arg}-${name}\nexpected:${exit} but got:${code}`);
|
||||
console.log(`ok - ${arg} exited with ${exit}`);
|
||||
}));
|
||||
if (error) {
|
||||
@ -146,13 +43,5 @@ function parent() {
|
||||
w.postMessage(arg);
|
||||
};
|
||||
|
||||
test('child1', 42);
|
||||
test('child2', 42);
|
||||
test('child3', 0);
|
||||
test('child4', 1, /^Error: ok$/);
|
||||
test('child5', 99);
|
||||
test('child6', 0);
|
||||
test('child7', 97);
|
||||
test('child8', 98, /^Error: ok$/);
|
||||
test('child9', 0, /^Error: ok$/);
|
||||
testCases.forEach((tc, i) => test(i, tc.func.name, tc.result, tc.error));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user