|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const cp = require('child_process'); |
| 5 | + |
| 6 | +if (process.argv[2] === 'child') { |
| 7 | + setInterval(() => {}, 1000); |
| 8 | +} else { |
| 9 | + const exitCode = common.isWindows ? 1 : 0; |
| 10 | + const SIGKILL = process.binding('constants').SIGKILL; |
| 11 | + |
| 12 | + function spawn(killSignal) { |
| 13 | + const child = cp.spawnSync(process.execPath, |
| 14 | + [__filename, 'child'], |
| 15 | + {killSignal, timeout: 100}); |
| 16 | + |
| 17 | + assert.strictEqual(child.status, exitCode); |
| 18 | + assert.strictEqual(child.error.code, 'ETIMEDOUT'); |
| 19 | + return child; |
| 20 | + } |
| 21 | + |
| 22 | + // Verify that an error is thrown for unknown signals. |
| 23 | + assert.throws(() => { |
| 24 | + spawn('SIG_NOT_A_REAL_SIGNAL'); |
| 25 | + }, /Error: Unknown signal: SIG_NOT_A_REAL_SIGNAL/); |
| 26 | + |
| 27 | + // Verify that the default kill signal is SIGTERM. |
| 28 | + { |
| 29 | + const child = spawn(); |
| 30 | + |
| 31 | + assert.strictEqual(child.signal, 'SIGTERM'); |
| 32 | + assert.strictEqual(child.options.killSignal, undefined); |
| 33 | + } |
| 34 | + |
| 35 | + // Verify that a string signal name is handled properly. |
| 36 | + { |
| 37 | + const child = spawn('SIGKILL'); |
| 38 | + |
| 39 | + assert.strictEqual(child.signal, 'SIGKILL'); |
| 40 | + assert.strictEqual(child.options.killSignal, SIGKILL); |
| 41 | + } |
| 42 | + |
| 43 | + // Verify that a numeric signal is handled properly. |
| 44 | + { |
| 45 | + const child = spawn(SIGKILL); |
| 46 | + |
| 47 | + assert.strictEqual(typeof SIGKILL, 'number'); |
| 48 | + assert.strictEqual(child.signal, 'SIGKILL'); |
| 49 | + assert.strictEqual(child.options.killSignal, SIGKILL); |
| 50 | + } |
| 51 | +} |
0 commit comments