node/test/parallel/test-child-process-spawnsync-kill-signal.js
Mithun Sasidharan d8c896cac5
test: use common.expectsError in tests
PR-URL: https://github.com/nodejs/node/pull/17484
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
2017-12-08 16:21:47 -05: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 } = process.binding('constants').os.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.
common.expectsError(() => {
spawn('SIG_NOT_A_REAL_SIGNAL');
}, { code: 'ERR_UNKNOWN_SIGNAL', type: TypeError });
// Verify that the default kill signal is SIGTERM.
{
const child = spawn(undefined, (opts) => {
assert.strictEqual(opts.options.killSignal, undefined);
});
assert.strictEqual(child.signal, 'SIGTERM');
}
// Verify that a string signal name is handled properly.
{
const child = spawn('SIGKILL', (opts) => {
assert.strictEqual(opts.options.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.options.killSignal, SIGKILL);
});
assert.strictEqual(child.signal, 'SIGKILL');
}
}