|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { mustCall } = require('../common'); |
| 4 | +const { strictEqual, ok, throws } = require('assert'); |
| 5 | +const fixtures = require('../common/fixtures'); |
| 6 | +const { spawn } = require('child_process'); |
| 7 | +const { getEventListeners } = require('events'); |
| 8 | + |
| 9 | +const aliveForeverFile = 'child-process-stay-alive-forever.js'; |
| 10 | +{ |
| 11 | + // Verify default signal + closes after at least 4 ms. |
| 12 | + let closed = false; |
| 13 | + setTimeout(() => ok(!closed), 4); |
| 14 | + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 15 | + timeout: 5, |
| 16 | + }); |
| 17 | + cp.on('close', mustCall(() => closed = true)); |
| 18 | + cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGTERM'))); |
| 19 | +} |
| 20 | + |
| 21 | +{ |
| 22 | + // Verify correct signal + closes after at least 4 ms. |
| 23 | + let closed = false; |
| 24 | + setTimeout(() => ok(!closed), 4); |
| 25 | + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 26 | + timeout: 5, |
| 27 | + killSignal: 'SIGKILL', |
| 28 | + }); |
| 29 | + cp.on('close', mustCall(() => closed = true)); |
| 30 | + cp.on('exit', mustCall((code, ks) => strictEqual(ks, 'SIGKILL'))); |
| 31 | +} |
| 32 | + |
| 33 | +{ |
| 34 | + // Verify timeout verification |
| 35 | + throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 36 | + timeout: 'badValue', |
| 37 | + }), /ERR_OUT_OF_RANGE/); |
| 38 | + |
| 39 | + throws(() => spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 40 | + timeout: {}, |
| 41 | + }), /ERR_OUT_OF_RANGE/); |
| 42 | +} |
| 43 | + |
| 44 | +{ |
| 45 | + // Verify abort signal gets unregistered |
| 46 | + const signal = new EventTarget(); |
| 47 | + signal.aborted = false; |
| 48 | + |
| 49 | + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 50 | + timeout: 6, |
| 51 | + signal, |
| 52 | + }); |
| 53 | + strictEqual(getEventListeners(signal, 'abort').length, 1); |
| 54 | + cp.on('close', mustCall(() => { |
| 55 | + strictEqual(getEventListeners(signal, 'abort').length, 0); |
| 56 | + })); |
| 57 | +} |
| 58 | + |
| 59 | +{ |
| 60 | + // Verify no error happens when abort is called timeout |
| 61 | + const controller = new AbortController(); |
| 62 | + const { signal } = controller; |
| 63 | + |
| 64 | + const cp = spawn(process.execPath, [fixtures.path(aliveForeverFile)], { |
| 65 | + timeout: 100, |
| 66 | + signal, |
| 67 | + }); |
| 68 | + cp.on('error', mustCall()); |
| 69 | + setTimeout(() => controller.abort(), 1); |
| 70 | +} |
0 commit comments