kill.js
This file
Path
test/exit/kill.js
306 lines · 11.4 KiB · JavaScript
Last changed by
Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
lines 1–100why, one step back1import process from 'node:process';2import {once, defaultMaxListeners} from 'node:events';3import {constants} from 'node:os';4import {setTimeout, setImmediate} from 'node:timers/promises';5import test from 'ava';6import {pEvent} from 'p-event';7import isRunning from 'is-running';8import {execa} from '../../index.js';9import {setFixtureDir} from '../helpers/fixtures-dir.js';10import {assertMaxListeners} from '../helpers/listeners.js';1112setFixtureDir();1314const isWindows = process.platform === 'win32';1516const spawnNoKillable = async (forceKillAfterDelay, options) => {17 const subprocess = execa('no-killable.js', {18 ipc: true,19 forceKillAfterDelay,20 ...options,21 });22 await pEvent(subprocess, 'message');23 return {subprocess};24};2526const spawnNoKillableSimple = options => execa('forever.js', {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2728const spawnNoKillableOutput = options => execa('noop-forever.js', ['.'], {killSignal: 'SIGWINCH', forceKillAfterDelay: 1, ...options});2930test('kill("SIGKILL") should terminate cleanly', async t => {31 const {subprocess} = await spawnNoKillable();3233 subprocess.kill('SIGKILL');3435 const {isTerminated, signal} = await t.throwsAsync(subprocess);36 t.true(isTerminated);37 t.is(signal, 'SIGKILL');38});3940const testInvalidForceKill = async (t, forceKillAfterDelay) => {41 t.throws(() => {42 execa('empty.js', {forceKillAfterDelay});43 }, {instanceOf: TypeError, message: /non-negative integer/});44};4546test('`forceKillAfterDelay` should not be NaN', testInvalidForceKill, Number.NaN);47test('`forceKillAfterDelay` should not be negative', testInvalidForceKill, -1);4849// `SIGTERM` cannot be caught on Windows, and it always aborts the process (like `SIGKILL` on Unix).50// Therefore, this feature and those tests must be different on Windows.51if (isWindows) {52 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async t => {53 const {subprocess} = await spawnNoKillable(1);54 subprocess.kill();5556 const {isTerminated, signal} = await t.throwsAsync(subprocess);57 t.true(isTerminated);58 t.is(signal, 'SIGTERM');59 });60} else {61 const testNoForceKill = async (t, forceKillAfterDelay, killArgument, options) => {62 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);6364 subprocess.kill(killArgument);6566 await setTimeout(6e3);67 t.true(isRunning(subprocess.pid));68 subprocess.kill('SIGKILL');6970 const {isTerminated, signal} = await t.throwsAsync(subprocess);71 t.true(isTerminated);72 t.is(signal, 'SIGKILL');73 };7475 test('`forceKillAfterDelay: false` should not kill after a timeout', testNoForceKill, false);76 test('`forceKillAfterDelay` should not kill after a timeout with other signals', testNoForceKill, true, 'SIGINT');77 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', testNoForceKill, true, 'SIGTERM', {killSignal: 'SIGINT'});78 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', testNoForceKill, true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT});7980 const testForceKill = async (t, forceKillAfterDelay, killArgument, options) => {81 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);8283 subprocess.kill(killArgument);8485 const {isTerminated, signal} = await t.throwsAsync(subprocess);86 t.true(isTerminated);87 t.is(signal, 'SIGKILL');88 };8990 test('`forceKillAfterDelay: number` should kill after a timeout', testForceKill, 50);91 test('`forceKillAfterDelay: true` should kill after a timeout', testForceKill, true);92 test('`forceKillAfterDelay: undefined` should kill after a timeout', testForceKill, undefined);93 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', testForceKill, 50, 'SIGTERM');94 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', testForceKill, 50, 'SIGINT', {killSignal: 'SIGINT'});95 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', testForceKill, 50, constants.signals.SIGINT, {killSignal: constants.signals.SIGINT});96 test('`forceKillAfterDelay` should kill after a timeout with an error', testForceKill, 50, new Error('test'));97 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), {killSignal: 'SIGINT'});9899 test('`forceKillAfterDelay` works with the "signal" option', async t => {100 const abortController = new AbortController();Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf
line 101why, one step back101 const subprocess = spawnNoKillableSimple({cancelSignal: abortController.signal});Split files (#837)ehmickybefore adoption672c2d7
lines 102–282why, one step back102 await once(subprocess, 'spawn');103 abortController.abort();104 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);105 t.true(isTerminated);106 t.is(signal, 'SIGKILL');107 t.true(isCanceled);108 });109110 test('`forceKillAfterDelay` works with the "timeout" option', async t => {111 const subprocess = spawnNoKillableSimple({timeout: 1});112 const {isTerminated, signal, timedOut} = await t.throwsAsync(subprocess);113 t.true(isTerminated);114 t.is(signal, 'SIGKILL');115 t.true(timedOut);116 });117118 test('`forceKillAfterDelay` works with the "maxBuffer" option', async t => {119 const subprocess = spawnNoKillableOutput({maxBuffer: 1});120 const {isTerminated, signal} = await t.throwsAsync(subprocess);121 t.true(isTerminated);122 t.is(signal, 'SIGKILL');123 });124125 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {126 const checkMaxListeners = assertMaxListeners(t);127128 const subprocess = spawnNoKillableSimple();129 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {130 subprocess.kill();131 }132133 const {isTerminated, signal} = await t.throwsAsync(subprocess);134 t.true(isTerminated);135 t.is(signal, 'SIGKILL');136137 checkMaxListeners();138 });139140 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async t => {141 const subprocess = spawnNoKillableSimple();142 subprocess.kill();143 subprocess.kill();144145 const {isTerminated, signal} = await t.throwsAsync(subprocess);146 t.true(isTerminated);147 t.is(signal, 'SIGKILL');148 });149}150151test('Can call `.kill()` multiple times', async t => {152 const subprocess = execa('forever.js');153 subprocess.kill();154 subprocess.kill();155156 const {isTerminated, signal} = await t.throwsAsync(subprocess);157 t.true(isTerminated);158 t.is(signal, 'SIGTERM');159});160161test('execa() returns a promise with kill()', async t => {162 const subprocess = execa('noop.js', ['foo']);163 t.is(typeof subprocess.kill, 'function');164 await subprocess;165});166167const testInvalidKillArgument = async (t, killArgument, secondKillArgument) => {168 const subprocess = execa('empty.js');169 const message = secondKillArgument instanceof Error || secondKillArgument === undefined170 ? /error instance or a signal name/171 : /second argument is optional/;172 t.throws(() => {173 subprocess.kill(killArgument, secondKillArgument);174 }, {message});175 await subprocess;176};177178test('Cannot call .kill(null)', testInvalidKillArgument, null);179test('Cannot call .kill(0n)', testInvalidKillArgument, 0n);180test('Cannot call .kill(true)', testInvalidKillArgument, true);181test('Cannot call .kill(errorObject)', testInvalidKillArgument, {name: '', message: '', stack: ''});182test('Cannot call .kill([error])', testInvalidKillArgument, [new Error('test')]);183test('Cannot call .kill(undefined, true)', testInvalidKillArgument, undefined, true);184test('Cannot call .kill("SIGTERM", true)', testInvalidKillArgument, 'SIGTERM', true);185test('Cannot call .kill(true, error)', testInvalidKillArgument, true, new Error('test'));186187test('.kill(error) propagates error', async t => {188 const subprocess = execa('forever.js');189 const originalMessage = 'test';190 const error = new Error(originalMessage);191 t.true(subprocess.kill(error));192 t.is(await t.throwsAsync(subprocess), error);193 t.is(error.exitCode, undefined);194 t.is(error.signal, 'SIGTERM');195 t.true(error.isTerminated);196 t.is(error.originalMessage, originalMessage);197 t.true(error.message.includes(originalMessage));198 t.true(error.message.includes('was killed with SIGTERM'));199 t.true(error.stack.includes(import.meta.url));200});201202test('.kill(error) uses killSignal', async t => {203 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});204 const error = new Error('test');205 subprocess.kill(error);206 t.is(await t.throwsAsync(subprocess), error);207 t.is(error.signal, 'SIGINT');208});209210test('.kill(signal, error) uses signal', async t => {211 const subprocess = execa('forever.js');212 const error = new Error('test');213 subprocess.kill('SIGINT', error);214 t.is(await t.throwsAsync(subprocess), error);215 t.is(error.signal, 'SIGINT');216});217218test('.kill(error) is a noop if process already exited', async t => {219 const subprocess = execa('empty.js');220 await subprocess;221 t.false(isRunning(subprocess.pid));222 t.false(subprocess.kill(new Error('test')));223});224225test('.kill(error) terminates but does not change the error if the process already errored but did not exit yet', async t => {226 const subprocess = execa('forever.js');227 const error = new Error('first');228 subprocess.stdout.destroy(error);229 await setImmediate();230 const secondError = new Error('second');231 t.true(subprocess.kill(secondError));232 t.is(await t.throwsAsync(subprocess), error);233 t.is(error.exitCode, undefined);234 t.is(error.signal, 'SIGTERM');235 t.true(error.isTerminated);236 t.false(error.message.includes(secondError.message));237});238239test('.kill(error) twice in a row', async t => {240 const subprocess = execa('forever.js');241 const error = new Error('first');242 subprocess.kill(error);243 const secondError = new Error('second');244 subprocess.kill(secondError);245 t.is(await t.throwsAsync(subprocess), error);246 t.false(error.message.includes(secondError.message));247});248249test('.kill(error) does not emit the "error" event', async t => {250 const subprocess = execa('forever.js');251 const error = new Error('test');252 subprocess.kill(error);253 t.is(await Promise.race([t.throwsAsync(subprocess), once(subprocess, 'error')]), error);254});255256test('child process errors are handled before spawn', async t => {257 const subprocess = execa('forever.js');258 const error = new Error('test');259 subprocess.emit('error', error);260 subprocess.kill();261 const thrownError = await t.throwsAsync(subprocess);262 t.is(thrownError, error);263 t.is(thrownError.exitCode, undefined);264 t.is(thrownError.signal, undefined);265 t.false(thrownError.isTerminated);266});267268test('child process errors are handled after spawn', async t => {269 const subprocess = execa('forever.js');270 await once(subprocess, 'spawn');271 const error = new Error('test');272 subprocess.emit('error', error);273 subprocess.kill();274 const thrownError = await t.throwsAsync(subprocess);275 t.is(thrownError, error);276 t.is(thrownError.exitCode, undefined);277 t.is(thrownError.signal, 'SIGTERM');278 t.true(thrownError.isTerminated);279});280281test('child process double errors are handled after spawn', async t => {282 const abortController = new AbortController();Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf
line 283why, one step back283 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});Split files (#837)ehmickybefore adoption672c2d7
lines 284–306why, one step back284 await once(subprocess, 'spawn');285 const error = new Error('test');286 subprocess.emit('error', error);287 await setImmediate();288 abortController.abort();289 const thrownError = await t.throwsAsync(subprocess);290 t.is(thrownError, error);291 t.is(thrownError.exitCode, undefined);292 t.is(thrownError.signal, 'SIGTERM');293 t.true(thrownError.isTerminated);294});295296test('child process errors use killSignal', async t => {297 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});298 await once(subprocess, 'spawn');299 const error = new Error('test');300 subprocess.emit('error', error);301 subprocess.kill();302 const thrownError = await t.throwsAsync(subprocess);303 t.is(thrownError, error);304 t.true(thrownError.isTerminated);305 t.is(thrownError.signal, 'SIGINT');306});