|
| 1 | +import process from 'node:process'; |
| 2 | +import {setTimeout} from 'node:timers/promises'; |
| 3 | +import test from 'ava'; |
| 4 | +import isRunning from 'is-running'; |
| 5 | +import {execa, execaSync} from '../../index.js'; |
| 6 | +import {setFixtureDirectory} from '../helpers/fixtures-directory.js'; |
| 7 | + |
| 8 | +setFixtureDirectory(); |
| 9 | + |
| 10 | +const isWindows = process.platform === 'win32'; |
| 11 | + |
| 12 | +const pollForSubprocessExit = async pid => { |
| 13 | + while (isRunning(pid)) { |
| 14 | + // eslint-disable-next-line no-await-in-loop |
| 15 | + await setTimeout(100); |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +// `ipc-send-pid.js` spawns a descendant process (`forever.js`) and sends back its PID. |
| 20 | +const spawnDescendant = async (killDescendants, options) => { |
| 21 | + const subprocess = execa('ipc-send-pid.js', ['false', 'false'], {stdio: 'ignore', ipc: true, killDescendants, ...options}); |
| 22 | + const descendantPid = await subprocess.getOneMessage(); |
| 23 | + return {subprocess, descendantPid}; |
| 24 | +}; |
| 25 | + |
| 26 | +test('killDescendants terminates descendant processes', async t => { |
| 27 | + const {subprocess, descendantPid} = await spawnDescendant(true); |
| 28 | + t.true(isRunning(descendantPid)); |
| 29 | + |
| 30 | + subprocess.kill(); |
| 31 | + await t.throwsAsync(subprocess); |
| 32 | + t.false(isRunning(subprocess.pid)); |
| 33 | + |
| 34 | + await Promise.race([ |
| 35 | + setTimeout(1e4, undefined, {ref: false}), |
| 36 | + pollForSubprocessExit(descendantPid), |
| 37 | + ]); |
| 38 | + t.false(isRunning(descendantPid)); |
| 39 | +}); |
| 40 | + |
| 41 | +test('killDescendants also terminates descendant processes when the subprocess times out', async t => { |
| 42 | + const {subprocess, descendantPid} = await spawnDescendant(true, {timeout: 1_000}); |
| 43 | + t.true(isRunning(descendantPid)); |
| 44 | + |
| 45 | + const {isTerminated, timedOut} = await t.throwsAsync(subprocess); |
| 46 | + t.true(isTerminated); |
| 47 | + t.true(timedOut); |
| 48 | + |
| 49 | + await Promise.race([ |
| 50 | + setTimeout(1e4, undefined, {ref: false}), |
| 51 | + pollForSubprocessExit(descendantPid), |
| 52 | + ]); |
| 53 | + t.false(isRunning(descendantPid)); |
| 54 | +}); |
| 55 | + |
| 56 | +// On Windows, terminating the direct subprocess already terminates its descendants, so this |
| 57 | +// only asserts the default Unix behavior of leaving descendants running. |
| 58 | +if (!isWindows) { |
| 59 | + test('descendant processes are not terminated without killDescendants', async t => { |
| 60 | + const {subprocess, descendantPid} = await spawnDescendant(false); |
| 61 | + t.true(isRunning(descendantPid)); |
| 62 | + |
| 63 | + subprocess.kill(); |
| 64 | + await t.throwsAsync(subprocess); |
| 65 | + t.false(isRunning(subprocess.pid)); |
| 66 | + |
| 67 | + t.true(isRunning(descendantPid)); |
| 68 | + process.kill(descendantPid, 'SIGKILL'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('timeout does not terminate descendants without killDescendants', async t => { |
| 72 | + const {subprocess, descendantPid} = await spawnDescendant(false, {timeout: 1_000}); |
| 73 | + t.true(isRunning(descendantPid)); |
| 74 | + |
| 75 | + const {timedOut} = await t.throwsAsync(subprocess); |
| 76 | + t.true(timedOut); |
| 77 | + t.true(isRunning(descendantPid)); |
| 78 | + process.kill(descendantPid, 'SIGKILL'); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +test('Cannot use "killDescendants" option, sync', t => { |
| 83 | + t.throws(() => { |
| 84 | + execaSync('empty.js', {killDescendants: true}); |
| 85 | + }, {message: /The "killDescendants: true" option cannot be used/}); |
| 86 | +}); |
0 commit comments