kill-force.js
This file
Path
test/terminate/kill-force.js
229 lines · 11.1 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–5why, one step back2import process from 'node:process';3import {once, defaultMaxListeners} from 'node:events';4import {constants} from 'node:os';5import {setTimeout} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 6why, one step back6import test from 'node:test';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 7why, one step back7import isRunning from 'is-running';TweaksSindre Sorhusbefore adoption698a1b8
line 8why, one step back8import {assertThrows, assertRejects} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 9–11why, one step back9import {execa} from '../../index.js';10import {setFixtureDirectory} from '../helpers/fixtures-directory.js';11import {assertMaxListeners} from '../helpers/listeners.js';Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 12–13why, one step back12import {foobarString} from '../helpers/input.js';13import {getEarlyErrorSubprocess} from '../helpers/early-error.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 14–24why, one step back1415setFixtureDirectory();1617const isWindows = process.platform === 'win32';1819const spawnNoKillable = async (forceKillAfterDelay, options) => {20 const subprocess = execa('no-killable.js', {21 ipc: true,22 forceKillAfterDelay,23 ...options,24 });Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 25why, one step back25 await subprocess.getOneMessage();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 26–28why, one step back26 return {subprocess};27};28Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 29–30why, one step back29const noKillableSimpleOptions = {killSignal: 'SIGWINCH', forceKillAfterDelay: 1};30const spawnNoKillableSimple = options => execa('forever.js', {...noKillableSimpleOptions, ...options});100% test coverage (#1018)ehmickybefore adoption9f65b86
line 31why, one step back31TweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32test('kill("SIGKILL") should terminate cleanly', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 33–36why, one step back33 const {subprocess} = await spawnNoKillable();3435 subprocess.kill('SIGKILL');36TweaksSindre Sorhusbefore adoption698a1b8
lines 37–41why, one step back37 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await assertRejects(subprocess);38 assert.equal(isTerminated, true);39 assert.equal(signal, 'SIGKILL');40 assert.equal(isForcefullyTerminated, false);41 assert.equal(shortMessage, 'Command was killed with SIGKILL (Forced termination): no-killable.js');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 42–43why, one step back42});43TweaksSindre Sorhusbefore adoption698a1b8
lines 44–45why, one step back44const testInvalidForceKill = async forceKillAfterDelay => {45 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–49why, one step back46 execa('empty.js', {forceKillAfterDelay});47 }, {instanceOf: TypeError, message: /non-negative integer/});48};49Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689
line 50why, one step back50test('`forceKillAfterDelay` should not be fractional', () => testInvalidForceKill(5000.5));Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 51–55why, one step back51test('`forceKillAfterDelay` should not be null', () => {52 assertThrows(() => {53 execa('empty.js', {forceKillAfterDelay: null});54 }, {instanceOf: TypeError, message: /got `null` \(null\)/});55});TweaksSindre Sorhusbefore adoption698a1b8
lines 56–57why, one step back56test('`forceKillAfterDelay` should not be NaN', () => testInvalidForceKill(NaN));57test('`forceKillAfterDelay` should not be negative', () => testInvalidForceKill(-1));Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689
lines 58–59why, one step back58// Larger delays make timers fire right away59test('`forceKillAfterDelay` should not be larger than the maximum timer delay', () => testInvalidForceKill(2 ** 31));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 60–63why, one step back6061// `SIGTERM` cannot be caught on Windows, and it always aborts the subprocess (like `SIGKILL` on Unix).62// Therefore, this feature and those tests must be different on Windows.63if (isWindows) {TweaksSindre Sorhusbefore adoption698a1b8
line 64why, one step back64 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async () => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 65why, one step back65 const {subprocess} = await spawnNoKillable();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 66–67why, one step back66 subprocess.kill();67TweaksSindre Sorhusbefore adoption698a1b8
lines 68–72why, one step back68 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await assertRejects(subprocess);69 assert.equal(isTerminated, true);70 assert.equal(signal, 'SIGTERM');71 assert.equal(isForcefullyTerminated, false);72 assert.equal(shortMessage, 'Command was killed with SIGTERM (Termination): no-killable.js');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 73–74why, one step back73 });74} else {TweaksSindre Sorhusbefore adoption698a1b8
line 75why, one step back75 const testNoForceKill = async (forceKillAfterDelay, killArgument, options) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 76–80why, one step back76 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);7778 subprocess.kill(killArgument);7980 await setTimeout(6e3);TweaksSindre Sorhusbefore adoption698a1b8
line 81why, one step back81 assert.ok(isRunning(subprocess.pid));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 82–83why, one step back82 subprocess.kill('SIGKILL');83TweaksSindre Sorhusbefore adoption698a1b8
lines 84–87why, one step back84 const {isTerminated, signal, isForcefullyTerminated} = await assertRejects(subprocess);85 assert.equal(isTerminated, true);86 assert.equal(signal, 'SIGKILL');87 assert.equal(isForcefullyTerminated, false);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 88–89why, one step back88 };89TweaksSindre Sorhusbefore adoption698a1b8
line 90why, one step back90 test('`forceKillAfterDelay: false` should not kill after a timeout', () => testNoForceKill(false));Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689
line 91why, one step back91 test('`forceKillAfterDelay` can be the maximum timer delay', () => testNoForceKill((2 ** 31) - 1));TweaksSindre Sorhusbefore adoption698a1b8
lines 92–94why, one step back92 test('`forceKillAfterDelay` should not kill after a timeout with other signals', () => testNoForceKill(true, 'SIGINT'));93 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal string', () => testNoForceKill(true, 'SIGTERM', {killSignal: 'SIGINT'}));94 test('`forceKillAfterDelay` should not kill after a timeout with wrong killSignal number', () => testNoForceKill(true, constants.signals.SIGTERM, {killSignal: constants.signals.SIGINT}));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 95why, one step back95Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 96why, one step back96 // eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 97why, one step back97 const testForceKill = async (forceKillAfterDelay, killSignal, expectedDelay, expectedKillSignal, options) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 98–99why, one step back98 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);99Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 100why, one step back100 subprocess.kill(killSignal);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 101why, one step back101TweaksSindre Sorhusbefore adoption698a1b8
lines 102–105why, one step back102 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await assertRejects(subprocess);103 assert.equal(isTerminated, true);104 assert.equal(signal, 'SIGKILL');105 assert.equal(isForcefullyTerminated, true);Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 106–107why, one step back106 const messageSuffix = killSignal instanceof Error ? `\n${killSignal.message}` : '';107 const signalDescription = expectedKillSignal === 'SIGINT' ? 'User interruption with CTRL-C' : 'Termination';TweaksSindre Sorhusbefore adoption698a1b8
line 108why, one step back108 assert.equal(shortMessage, `Command was killed with ${expectedKillSignal} (${signalDescription}) and was forcefully terminated after ${expectedDelay} milliseconds: no-killable.js${messageSuffix}`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 109–110why, one step back109 };110TweaksSindre Sorhusbefore adoption698a1b8
lines 111–120why, one step back111 test('`forceKillAfterDelay: number` should kill after a timeout', () => testForceKill(50, undefined, 50, 'SIGTERM'));112 test('`forceKillAfterDelay: true` should kill after a timeout', () => testForceKill(true, undefined, 5e3, 'SIGTERM'));113 test('`forceKillAfterDelay: undefined` should kill after a timeout', () => testForceKill(undefined, undefined, 5e3, 'SIGTERM'));114 test('`forceKillAfterDelay` should kill after a timeout with SIGTERM', () => testForceKill(50, 'SIGTERM', 50, 'SIGTERM'));115 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string', () => testForceKill(50, 'SIGINT', 50, 'SIGINT', {killSignal: 'SIGINT'}));116 test('`forceKillAfterDelay` should kill after a timeout with the killSignal string, mixed', () => testForceKill(50, 'SIGINT', 50, 'SIGINT', {killSignal: constants.signals.SIGINT}));117 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number', () => testForceKill(50, constants.signals.SIGINT, 50, 'SIGINT', {killSignal: constants.signals.SIGINT}));118 test('`forceKillAfterDelay` should kill after a timeout with the killSignal number, mixed', () => testForceKill(50, constants.signals.SIGINT, 50, 'SIGINT', {killSignal: 'SIGINT'}));119 test('`forceKillAfterDelay` should kill after a timeout with an error', () => testForceKill(50, new Error('test'), 50, 'SIGTERM'));120 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', () => testForceKill(50, new Error('test'), 50, 'SIGINT', {killSignal: 'SIGINT'}));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 121why, one step back121TweaksSindre Sorhusbefore adoption698a1b8
line 122why, one step back122 test('`forceKillAfterDelay` works with the "cancelSignal" option', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 123–124why, one step back123 const abortController = new AbortController();124 const subprocess = spawnNoKillableSimple({cancelSignal: abortController.signal});Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 125why, one step back125 await once(subprocess.nodeChildProcess, 'spawn');Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 126why, one step back126 abortController.abort('');TweaksSindre Sorhusbefore adoption698a1b8
lines 127–133why, one step back127 const {isTerminated, signal, isCanceled, isGracefullyCanceled, isForcefullyTerminated, shortMessage} = await assertRejects(subprocess);128 assert.equal(isTerminated, true);129 assert.equal(signal, 'SIGKILL');130 assert.equal(isCanceled, true);131 assert.equal(isGracefullyCanceled, false);132 assert.equal(isForcefullyTerminated, true);133 assert.equal(shortMessage, 'Command was canceled and was forcefully terminated after 1 milliseconds: forever.js');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 134–135why, one step back134 });135TweaksSindre Sorhusbefore adoption698a1b8
lines 136–142why, one step back136 test('`forceKillAfterDelay` works with the "timeout" option', async () => {137 const {isTerminated, signal, timedOut, isForcefullyTerminated, shortMessage} = await assertRejects(spawnNoKillableSimple({timeout: 1}));138 assert.equal(isTerminated, true);139 assert.equal(signal, 'SIGKILL');140 assert.equal(timedOut, true);141 assert.equal(isForcefullyTerminated, true);142 assert.equal(shortMessage, 'Command timed out after 1 milliseconds and was forcefully terminated after 1 milliseconds: forever.js');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 143–144why, one step back143 });144TweaksSindre Sorhusbefore adoption698a1b8
line 145why, one step back145 test('`forceKillAfterDelay` works with the "maxBuffer" option', async () => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 146–147why, one step back146 const subprocess = execa('noop-forever.js', ['.'], {...noKillableSimpleOptions, maxBuffer: 1});147 const [chunk] = await once(subprocess.stdout, 'data');TweaksSindre Sorhusbefore adoption698a1b8
line 148why, one step back148 assert.equal(chunk.toString(), '.\n');Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 149why, one step back149 subprocess.kill();TweaksSindre Sorhusbefore adoption698a1b8
lines 150–154why, one step back150 const {isTerminated, signal, isForcefullyTerminated, shortMessage} = await assertRejects(subprocess);151 assert.equal(isTerminated, true);152 assert.equal(signal, 'SIGKILL');153 assert.equal(isForcefullyTerminated, true);154 assert.equal(shortMessage, 'Command\'s stdout was larger than 1 characters and was forcefully terminated after 1 milliseconds: noop-forever.js .\nmaxBuffer exceeded');Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 155–156why, one step back155 });156TweaksSindre Sorhusbefore adoption698a1b8
line 157why, one step back157 test('`forceKillAfterDelay` works with the "error" event', async () => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 158why, one step back158 const subprocess = spawnNoKillableSimple();Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 159why, one step back159 await once(subprocess.nodeChildProcess, 'spawn');Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 160–161why, one step back160 const error = new Error(foobarString);161 error.code = 'ECODE';Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 162why, one step back162 subprocess.nodeChildProcess.emit('error', error);Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 163why, one step back163 subprocess.kill();TweaksSindre Sorhusbefore adoption698a1b8
lines 164–170why, one step back164 const {isTerminated, signal, isForcefullyTerminated, shortMessage, originalMessage, cause} = await assertRejects(subprocess);165 assert.equal(isTerminated, true);166 assert.equal(signal, 'SIGKILL');167 assert.equal(isForcefullyTerminated, true);168 assert.equal(cause, error);169 assert.equal(originalMessage, error.message);170 assert.equal(shortMessage, `Command failed with ${error.code} and was forcefully terminated after 1 milliseconds: forever.js\n${error.message}`);Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 171–172why, one step back171 });172TweaksSindre Sorhusbefore adoption698a1b8
lines 173–174why, one step back173 test('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async () => {174 const checkMaxListeners = assertMaxListeners();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 175–180why, one step back175176 const subprocess = spawnNoKillableSimple();177 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {178 subprocess.kill();179 }180TweaksSindre Sorhusbefore adoption698a1b8
lines 181–184why, one step back181 const {isTerminated, signal, isForcefullyTerminated} = await assertRejects(subprocess);182 assert.equal(isTerminated, true);183 assert.equal(signal, 'SIGKILL');184 assert.equal(isForcefullyTerminated, true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 185–188why, one step back185186 checkMaxListeners();187 });188TweaksSindre Sorhusbefore adoption698a1b8
line 189why, one step back189 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async () => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 190–193why, one step back190 const subprocess = spawnNoKillableSimple();191 subprocess.kill();192 subprocess.kill();193TweaksSindre Sorhusbefore adoption698a1b8
lines 194–197why, one step back194 const {isTerminated, signal, isForcefullyTerminated} = await assertRejects(subprocess);195 assert.equal(isTerminated, true);196 assert.equal(signal, 'SIGKILL');197 assert.equal(isForcefullyTerminated, true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 198–199why, one step back198 });199}Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 200why, one step back200TweaksSindre Sorhusbefore adoption698a1b8
line 201why, one step back201test('result.isForcefullyTerminated is false on success', async () => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 202why, one step back202 const {isForcefullyTerminated} = await execa('empty.js');TweaksSindre Sorhusbefore adoption698a1b8
line 203why, one step back203 assert.equal(isForcefullyTerminated, false);Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 204–205why, one step back204});205TweaksSindre Sorhusbefore adoption698a1b8
lines 206–208why, one step back206test('error.isForcefullyTerminated is false on spawn errors', async () => {207 const {isForcefullyTerminated} = await assertRejects(getEarlyErrorSubprocess());208 assert.equal(isForcefullyTerminated, false);Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 209–210why, one step back209});210TweaksSindre Sorhusbefore adoption698a1b8
line 211why, one step back211test('error.isForcefullyTerminated is false when already terminated', async () => {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 212–224why, one step back212 const abortController = new AbortController();213 const final = async function * () {214 try {215 await setTimeout(1e6, undefined, {signal: abortController.signal});216 } catch {}217218 yield * [];219 };220221 const subprocess = execa('forever.js', {stdout: {final}});222 subprocess.kill();223 await setTimeout(6e3);224 abortController.abort();TweaksSindre Sorhusbefore adoption698a1b8
lines 225–228why, one step back225 const {isForcefullyTerminated, isTerminated, signal} = await assertRejects(subprocess);226 assert.equal(isForcefullyTerminated, false);227 assert.equal(isTerminated, true);228 assert.equal(signal, 'SIGTERM');Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 229why, one step back229});