0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-child-process-spawnsync-kill-signal.js
Juan José Arboleda 7534477786
test: replace flag expose_internals to expose-internals
PR-URL: https://github.com/nodejs/node/pull/32542
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-04-02 18:28:23 +02:00

65 lines
1.8 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
if (process.argv[2] === 'child') {
setInterval(() => {}, 1000);
} else {
const internalCp = require('internal/child_process');
const oldSpawnSync = internalCp.spawnSync;
const { SIGKILL } = require('os').constants.signals;
function spawn(killSignal, beforeSpawn) {
if (beforeSpawn) {
internalCp.spawnSync = common.mustCall(function(opts) {
beforeSpawn(opts);
return oldSpawnSync(opts);
});
}
const child = cp.spawnSync(process.execPath,
[__filename, 'child'],
{ killSignal, timeout: 100 });
if (beforeSpawn)
internalCp.spawnSync = oldSpawnSync;
assert.strictEqual(child.status, null);
assert.strictEqual(child.error.code, 'ETIMEDOUT');
return child;
}
// Verify that an error is thrown for unknown signals.
assert.throws(() => {
spawn('SIG_NOT_A_REAL_SIGNAL');
}, { code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' });
// Verify that the default kill signal is SIGTERM.
{
const child = spawn(undefined, (opts) => {
assert.strictEqual(opts.killSignal, undefined);
});
assert.strictEqual(child.signal, 'SIGTERM');
}
// Verify that a string signal name is handled properly.
{
const child = spawn('SIGKILL', (opts) => {
assert.strictEqual(opts.killSignal, SIGKILL);
});
assert.strictEqual(child.signal, 'SIGKILL');
}
// Verify that a numeric signal is handled properly.
{
assert.strictEqual(typeof SIGKILL, 'number');
const child = spawn(SIGKILL, (opts) => {
assert.strictEqual(opts.killSignal, SIGKILL);
});
assert.strictEqual(child.signal, 'SIGKILL');
}
}