1// Flags: --expose-internals 2'use strict'; 3const common = require('../common'); 4const assert = require('assert'); 5const cp = require('child_process'); 6 7if (process.argv[2] === 'child') { 8 setInterval(() => {}, 1000); 9} else { 10 const internalCp = require('internal/child_process'); 11 const oldSpawnSync = internalCp.spawnSync; 12 const { SIGKILL } = require('os').constants.signals; 13 14 function spawn(killSignal, beforeSpawn) { 15 if (beforeSpawn) { 16 internalCp.spawnSync = common.mustCall(function(opts) { 17 beforeSpawn(opts); 18 return oldSpawnSync(opts); 19 }); 20 } 21 const child = cp.spawnSync(process.execPath, 22 [__filename, 'child'], 23 { killSignal, timeout: 100 }); 24 if (beforeSpawn) 25 internalCp.spawnSync = oldSpawnSync; 26 assert.strictEqual(child.status, null); 27 assert.strictEqual(child.error.code, 'ETIMEDOUT'); 28 return child; 29 } 30 31 // Verify that an error is thrown for unknown signals. 32 assert.throws(() => { 33 spawn('SIG_NOT_A_REAL_SIGNAL'); 34 }, { code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' }); 35 36 // Verify that the default kill signal is SIGTERM. 37 { 38 const child = spawn(undefined, (opts) => { 39 assert.strictEqual(opts.killSignal, undefined); 40 }); 41 42 assert.strictEqual(child.signal, 'SIGTERM'); 43 } 44 45 // Verify that a string signal name is handled properly. 46 { 47 const child = spawn('SIGKILL', (opts) => { 48 assert.strictEqual(opts.killSignal, SIGKILL); 49 }); 50 51 assert.strictEqual(child.signal, 'SIGKILL'); 52 } 53 54 // Verify that a numeric signal is handled properly. 55 { 56 assert.strictEqual(typeof SIGKILL, 'number'); 57 58 const child = spawn(SIGKILL, (opts) => { 59 assert.strictEqual(opts.killSignal, SIGKILL); 60 }); 61 62 assert.strictEqual(child.signal, 'SIGKILL'); 63 } 64} 65