cleanup.js
This file
Path
test/terminate/cleanup.js
122 lines · 4.5 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import assert from 'node:assert/strict';import process from 'node:process';import {setTimeout} from 'node:timers/promises';import test from 'node:test';import isRunning from 'is-running';import {assertThrows, assertRejects} from '../helpers/assert.js';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {nestedSubprocess} from '../helpers/nested.js';import {foobarString} from '../helpers/input.js';
setFixtureDirectory();
const isWindows = process.platform === 'win32';
// When subprocess exits before current processconst spawnAndExit = async (worker, cleanup, detached) => { const {nestedResult: {stdout}} = await nestedSubprocess('noop-fd.js', ['1', foobarString], {worker, cleanup, detached}); assert.equal(stdout, foobarString);};
test('spawnAndExit', () => spawnAndExit(false, false, false));test('spawnAndExit cleanup', () => spawnAndExit(false, true, false));test('spawnAndExit detached', () => spawnAndExit(false, false, true));test('spawnAndExit cleanup detached', () => spawnAndExit(false, true, true));test('spawnAndExit, worker', () => spawnAndExit(true, false, false));test('spawnAndExit cleanup, worker', () => spawnAndExit(true, true, false));test('spawnAndExit detached, worker', () => spawnAndExit(true, false, true));test('spawnAndExit cleanup detached, worker', () => spawnAndExit(true, true, true));
// When current process exits before subprocessconst spawnAndKill = async ([signal, cleanup, detached, isKilled]) => { const subprocess = execa('ipc-send-pid.js', [cleanup, detached], {stdio: 'ignore', ipc: true});
const pid = await subprocess.getOneMessage(); assert.ok(Number.isInteger(pid)); assert.ok(isRunning(pid));
process.kill(subprocess.pid, signal);
await assertRejects(subprocess); assert.ok(!isRunning(subprocess.pid));
if (isKilled) { await Promise.race([ setTimeout(1e4, undefined, {ref: false}), pollForSubprocessExit(pid), ]); assert.equal(isRunning(pid), false); } else { assert.equal(isRunning(pid), true); process.kill(pid, 'SIGKILL'); }};
const pollForSubprocessExit = async pid => { while (isRunning(pid)) { // eslint-disable-next-line no-await-in-loop await setTimeout(100); }};
// Without `options.cleanup`:// - on Windows subprocesses are killed if `options.detached: false`, but not// if `options.detached: true`// - on Linux subprocesses are never killed regardless of `options.detached`// With `options.cleanup`, subprocesses are always killed// - `options.cleanup` with SIGKILL is a noop, since it cannot be handledtest('spawnAndKill SIGTERM', () => spawnAndKill(['SIGTERM', false, false, isWindows]));test('spawnAndKill SIGKILL', () => spawnAndKill(['SIGKILL', false, false, isWindows]));test('spawnAndKill cleanup SIGTERM', () => spawnAndKill(['SIGTERM', true, false, true]));test('spawnAndKill cleanup SIGKILL', () => spawnAndKill(['SIGKILL', true, false, isWindows]));test('spawnAndKill detached SIGTERM', () => spawnAndKill(['SIGTERM', false, true, false]));test('spawnAndKill detached SIGKILL', () => spawnAndKill(['SIGKILL', false, true, false]));test('spawnAndKill cleanup detached SIGTERM', () => spawnAndKill(['SIGTERM', true, true, false]));test('spawnAndKill cleanup detached SIGKILL', () => spawnAndKill(['SIGKILL', true, true, false]));
if (!isWindows) { test('spawnAndKill cleanup uses killSignal', async () => { const subprocess = execa('ipc-send-pid.js', ['true', 'false', 'no-killable'], {stdio: 'ignore', ipc: true});
const pid = await subprocess.getOneMessage(); assert.ok(Number.isInteger(pid)); assert.ok(isRunning(pid));
process.kill(subprocess.pid, 'SIGTERM');
await assertRejects(subprocess); await Promise.race([ setTimeout(1e4, undefined, {ref: false}), pollForSubprocessExit(pid), ]); assert.ok(!isRunning(pid)); });}
// See #128test('removes exit handler on exit', async () => { // @todo this relies on `signal-exit` internals const exitListeners = globalThis[Symbol.for('signal-exit emitter')].listeners.exit;
const subprocess = execa('noop.js'); const listener = exitListeners.at(-1);
await subprocess; assert.ok(!exitListeners.includes(listener));});
test('detach subprocess', async () => { const {stdout} = await execa('detach.js'); const pid = Number(stdout); assert.ok(Number.isInteger(pid)); assert.ok(isRunning(pid));
process.kill(pid, 'SIGKILL');});
test('Cannot use "detached" option, sync', () => { assertThrows(() => { execaSync('empty.js', {detached: true}); }, {message: /The "detached: true" option cannot be used/});});