Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–5why, one step back
2import 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 back
6import test from 'node:test';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 7why, one step back
7import isRunning from 'is-running';

TweaksSindre Sorhusbefore adoption698a1b8

line 8why, one step back
8import {assertThrows, assertRejects} from '../helpers/assert.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 9–11why, one step back
9import {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 back
12import {foobarString} from '../helpers/input.js';
13import {getEarlyErrorSubprocess} from '../helpers/early-error.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 14–24why, one step back
14
15setFixtureDirectory();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 back
25 await subprocess.getOneMessage();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 26–28why, one step back
26 return {subprocess};
27};28

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 29–30why, one step back
29const noKillableSimpleOptions = {killSignal: 'SIGWINCH', forceKillAfterDelay: 1};
30const spawnNoKillableSimple = options => execa('forever.js', {...noKillableSimpleOptions, ...options});

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 31why, one step back
31

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32test('kill("SIGKILL") should terminate cleanly', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 33–36why, one step back
33 const {subprocess} = await spawnNoKillable();
3435 subprocess.kill('SIGKILL');36

TweaksSindre Sorhusbefore adoption698a1b8

lines 37–41why, one step back
37 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 back
42});
43

TweaksSindre Sorhusbefore adoption698a1b8

lines 44–45why, one step back
44const testInvalidForceKill = async forceKillAfterDelay => {
45 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–49why, one step back
46 execa('empty.js', {forceKillAfterDelay});
47 }, {instanceOf: TypeError, message: /non-negative integer/});48};49

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

line 50why, one step back
50test('`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 back
51test('`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 back
56test('`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 back
58// Larger delays make timers fire right away
59test('`forceKillAfterDelay` should not be larger than the maximum timer delay', () => testInvalidForceKill(2 ** 31));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 60–63why, one step back
60
61// `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 back
64 test('Can call `.kill()` with `forceKillAfterDelay` on Windows', async () => {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 65why, one step back
65 const {subprocess} = await spawnNoKillable();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 66–67why, one step back
66 subprocess.kill();
67

TweaksSindre Sorhusbefore adoption698a1b8

lines 68–72why, one step back
68 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 back
73 });
74} else {

TweaksSindre Sorhusbefore adoption698a1b8

line 75why, one step back
75 const testNoForceKill = async (forceKillAfterDelay, killArgument, options) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 76–80why, one step back
76 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);
7778 subprocess.kill(killArgument);7980 await setTimeout(6e3);

TweaksSindre Sorhusbefore adoption698a1b8

line 81why, one step back
81 assert.ok(isRunning(subprocess.pid));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 82–83why, one step back
82 subprocess.kill('SIGKILL');
83

TweaksSindre Sorhusbefore adoption698a1b8

lines 84–87why, one step back
84 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 back
88 };
89

TweaksSindre Sorhusbefore adoption698a1b8

line 90why, one step back
90 test('`forceKillAfterDelay: false` should not kill after a timeout', () => testNoForceKill(false));

Fix invalid `timeout` and `forceKillAfterDelay` valuesSindre Sorhusbefore adoptiond9a5689

line 91why, one step back
91 test('`forceKillAfterDelay` can be the maximum timer delay', () => testNoForceKill((2 ** 31) - 1));

TweaksSindre Sorhusbefore adoption698a1b8

lines 92–94why, one step back
92 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 back
95

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 96why, one step back
96 // eslint-disable-next-line max-params

TweaksSindre Sorhusbefore adoption698a1b8

line 97why, one step back
97 const testForceKill = async (forceKillAfterDelay, killSignal, expectedDelay, expectedKillSignal, options) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 98–99why, one step back
98 const {subprocess} = await spawnNoKillable(forceKillAfterDelay, options);
99

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 100why, one step back
100 subprocess.kill(killSignal);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 101why, one step back
101

TweaksSindre Sorhusbefore adoption698a1b8

lines 102–105why, one step back
102 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 back
106 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 back
108 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 back
109 };
110

TweaksSindre Sorhusbefore adoption698a1b8

lines 111–120why, one step back
111 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 back
121

TweaksSindre Sorhusbefore adoption698a1b8

line 122why, one step back
122 test('`forceKillAfterDelay` works with the "cancelSignal" option', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 123–124why, one step back
123 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 back
125 await once(subprocess.nodeChildProcess, 'spawn');

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 126why, one step back
126 abortController.abort('');

TweaksSindre Sorhusbefore adoption698a1b8

lines 127–133why, one step back
127 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 back
134 });
135

TweaksSindre Sorhusbefore adoption698a1b8

lines 136–142why, one step back
136 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 back
143 });
144

TweaksSindre Sorhusbefore adoption698a1b8

line 145why, one step back
145 test('`forceKillAfterDelay` works with the "maxBuffer" option', async () => {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 146–147why, one step back
146 const subprocess = execa('noop-forever.js', ['.'], {...noKillableSimpleOptions, maxBuffer: 1});
147 const [chunk] = await once(subprocess.stdout, 'data');

TweaksSindre Sorhusbefore adoption698a1b8

line 148why, one step back
148 assert.equal(chunk.toString(), '.\n');

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 149why, one step back
149 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 150–154why, one step back
150 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 back
155 });
156

TweaksSindre Sorhusbefore adoption698a1b8

line 157why, one step back
157 test('`forceKillAfterDelay` works with the "error" event', async () => {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 158why, one step back
158 const subprocess = spawnNoKillableSimple();

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 159why, one step back
159 await once(subprocess.nodeChildProcess, 'spawn');

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 160–161why, one step back
160 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 back
162 subprocess.nodeChildProcess.emit('error', error);

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 163why, one step back
163 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 164–170why, one step back
164 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 back
171 });
172

TweaksSindre Sorhusbefore adoption698a1b8

lines 173–174why, one step back
173 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 back
175
176 const subprocess = spawnNoKillableSimple();177 for (let index = 0; index < defaultMaxListeners + 1; index += 1) {178 subprocess.kill();179 }180

TweaksSindre Sorhusbefore adoption698a1b8

lines 181–184why, one step back
181 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 back
185
186 checkMaxListeners();187 });188

TweaksSindre Sorhusbefore adoption698a1b8

line 189why, one step back
189 test('Can call `.kill()` with `forceKillAfterDelay` multiple times', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 190–193why, one step back
190 const subprocess = spawnNoKillableSimple();
191 subprocess.kill();192 subprocess.kill();193

TweaksSindre Sorhusbefore adoption698a1b8

lines 194–197why, one step back
194 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 back
198 });
199}

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 200why, one step back
200

TweaksSindre Sorhusbefore adoption698a1b8

line 201why, one step back
201test('result.isForcefullyTerminated is false on success', async () => {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 202why, one step back
202 const {isForcefullyTerminated} = await execa('empty.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 203why, one step back
203 assert.equal(isForcefullyTerminated, false);

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 204–205why, one step back
204});
205

TweaksSindre Sorhusbefore adoption698a1b8

lines 206–208why, one step back
206test('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 back
209});
210

TweaksSindre Sorhusbefore adoption698a1b8

line 211why, one step back
211test('error.isForcefullyTerminated is false when already terminated', async () => {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 212–224why, one step back
212 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 back
225 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 back
229});