node/test/parallel/test-child-process-exec-timeout-expire.js
Joyee Cheung fc9d44c8ac
test: deflake child process exec timeout tests
On Windows it might take too long for the parent to start the
communication with a child process, so by the time the parent
starts its own timer, the child process might have already
completed running, and the parent in those tests won't have a
chance to terminate these child processes after the timeout.
To address this issue, raise the time for which the child is
supposed to run to make sure that the parent starts
its own timer before the child terminates in the tests.
Also, split the test into smaller ones to reduce the overhead.

PR-URL: https://github.com/nodejs/node/pull/44390
Refs: https://github.com/nodejs/reliability/issues/356
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2022-09-01 02:31:34 +08:00

51 lines
1.4 KiB
JavaScript

'use strict';
// Test exec() with a timeout that expires.
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const {
cleanupStaleProcess,
logAfterTime,
kExpiringChildRunTime,
kExpiringParentTimer
} = require('../common/child_process');
if (process.argv[2] === 'child') {
logAfterTime(kExpiringChildRunTime);
return;
}
const cmd = `"${process.execPath}" "${__filename}" child`;
cp.exec(cmd, {
timeout: kExpiringParentTimer,
}, common.mustCall((err, stdout, stderr) => {
console.log('[stdout]', stdout.trim());
console.log('[stderr]', stderr.trim());
let sigterm = 'SIGTERM';
assert.strictEqual(err.killed, true);
// TODO OpenBSD returns a null signal and 143 for code
if (common.isOpenBSD) {
assert.strictEqual(err.code, 143);
sigterm = null;
} else {
assert.strictEqual(err.code, null);
}
// At least starting with Darwin Kernel Version 16.4.0, sending a SIGTERM to a
// process that is still starting up kills it with SIGKILL instead of SIGTERM.
// See: https://github.com/libuv/libuv/issues/1226
if (common.isOSX)
assert.ok(err.signal === 'SIGTERM' || err.signal === 'SIGKILL');
else
assert.strictEqual(err.signal, sigterm);
assert.strictEqual(err.cmd, cmd);
assert.strictEqual(stdout.trim(), '');
assert.strictEqual(stderr.trim(), '');
}));
cleanupStaleProcess(__filename);