Skip to content
Myr

graceful.js

This file

Path

test/ipc/graceful.js

248 lines · 12.0 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 2–3why, one step back
2import {getEventListeners} from 'node:events';
3import {setTimeout} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 4–5why, one step back
4import test from 'node:test';
5import {assertRejects} from '../helpers/assert.js';

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 6–11why, one step back
6import {execa, execaSync} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {foobarString} from '../helpers/input.js';910setFixtureDirectory();11

TweaksSindre Sorhusbefore adoption698a1b8

lines 12–18why, one step back
12test('Graceful cancelSignal can be already aborted', async () => {
13 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(execa('graceful-send.js', {cancelSignal: AbortSignal.abort(foobarString), gracefulCancel: true, forceKillAfterDelay: false}));14 assert.equal(isCanceled, true);15 assert.equal(isGracefullyCanceled, true);16 assert.equal(isTerminated, false);17 assert.equal(exitCode, 0);18 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 19–20why, one step back
19});
20

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21test('Graceful cancelSignal can be aborted', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 22–23why, one step back
22 const controller = new AbortController();
23 const subprocess = execa('graceful-send-twice.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 24why, one step back
24 assert.equal(await subprocess.getOneMessage(), false);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 25why, one step back
25 controller.abort(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 26–31why, one step back
26 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
27 assert.equal(isCanceled, true);28 assert.equal(isGracefullyCanceled, true);29 assert.equal(isTerminated, false);30 assert.equal(exitCode, 0);31 assert.deepEqual(ipcOutput, [false, foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 32–33why, one step back
32});
33

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34test('Graceful cancelSignal can be never aborted', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 35–36why, one step back
35 const controller = new AbortController();
36 const subprocess = execa('graceful-send-fast.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37 assert.equal(await subprocess.getOneMessage(), false);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 38–40why, one step back
38 await subprocess;
39});40

TweaksSindre Sorhusbefore adoption698a1b8

line 41why, one step back
41test('Graceful cancelSignal can be already aborted but not used', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 42why, one step back
42 const subprocess = execa('ipc-send-get.js', {cancelSignal: AbortSignal.abort(foobarString), gracefulCancel: true, forceKillAfterDelay: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 43why, one step back
43 assert.equal(await subprocess.getOneMessage(), foobarString);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 44–45why, one step back
44 await setTimeout(1e3);
45 await subprocess.sendMessage('.');

TweaksSindre Sorhusbefore adoption698a1b8

lines 46–51why, one step back
46 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
47 assert.equal(isCanceled, true);48 assert.equal(isGracefullyCanceled, true);49 assert.equal(isTerminated, false);50 assert.equal(exitCode, 0);51 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 52–53why, one step back
52});
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('Graceful cancelSignal can be aborted but not used', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 55–56why, one step back
55 const controller = new AbortController();
56 const subprocess = execa('ipc-send-get.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 57why, one step back
57 assert.equal(await subprocess.getOneMessage(), foobarString);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 58–60why, one step back
58 controller.abort(foobarString);
59 await setTimeout(1e3);60 await subprocess.sendMessage(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 61–66why, one step back
61 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
62 assert.equal(isCanceled, true);63 assert.equal(isGracefullyCanceled, true);64 assert.equal(isTerminated, false);65 assert.equal(exitCode, 0);66 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 67–68why, one step back
67});
68

TweaksSindre Sorhusbefore adoption698a1b8

line 69why, one step back
69test('Graceful cancelSignal can be never aborted nor used', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 70–71why, one step back
70 const controller = new AbortController();
71 const subprocess = execa('empty.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 72why, one step back
72 assert.equal(getEventListeners(controller.signal, 'abort').length, 1);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 73why, one step back
73 await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74 assert.equal(getEventListeners(controller.signal, 'abort').length, 0);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 75–76why, one step back
75});
76

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77test('Graceful cancelSignal can be aborted twice', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 78–79why, one step back
78 const controller = new AbortController();
79 const subprocess = execa('graceful-send-twice.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 80why, one step back
80 assert.equal(await subprocess.getOneMessage(), false);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 81–82why, one step back
81 controller.abort(foobarString);
82 controller.abort('.');

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–88why, one step back
83 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
84 assert.equal(isCanceled, true);85 assert.equal(isGracefullyCanceled, true);86 assert.equal(isTerminated, false);87 assert.equal(exitCode, 0);88 assert.deepEqual(ipcOutput, [false, foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 89–90why, one step back
89});
90

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91test('Graceful cancelSignal cannot be manually aborted after disconnection', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 92–93why, one step back
92 const controller = new AbortController();
93 const subprocess = execa('empty.js', {cancelSignal: controller.signal, gracefulCancel: true});

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

line 94why, one step back
94 subprocess.nodeChildProcess.disconnect();

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 95why, one step back
95 controller.abort(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 96–102why, one step back
96 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput, originalMessage} = await assertRejects(subprocess);
97 assert.equal(isCanceled, false);98 assert.equal(isGracefullyCanceled, false);99 assert.equal(isTerminated, false);100 assert.equal(exitCode, 0);101 assert.deepEqual(ipcOutput, []);102 assert.equal(originalMessage, '`cancelSignal`\'s `controller.abort()` cannot be used: the subprocess has already exited or disconnected.');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 103–104why, one step back
103});
104

TweaksSindre Sorhusbefore adoption698a1b8

line 105why, one step back
105test('Graceful cancelSignal can disconnect after being manually aborted', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 106–108why, one step back
106 const controller = new AbortController();
107 const subprocess = execa('graceful-disconnect.js', {cancelSignal: controller.signal, gracefulCancel: true});108 controller.abort(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 109why, one step back
109 assert.equal(await subprocess.getOneMessage(), foobarString);

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

line 110why, one step back
110 subprocess.nodeChildProcess.disconnect();

TweaksSindre Sorhusbefore adoption698a1b8

lines 111–116why, one step back
111 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
112 assert.equal(isCanceled, true);113 assert.equal(isGracefullyCanceled, true);114 assert.equal(isTerminated, false);115 assert.equal(exitCode, 0);116 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 117–118why, one step back
117});
118

TweaksSindre Sorhusbefore adoption698a1b8

line 119why, one step back
119test('Graceful cancelSignal is automatically aborted on disconnection', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 120–121why, one step back
120 const controller = new AbortController();
121 const subprocess = execa('graceful-send-print.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 122why, one step back
122 assert.equal(await subprocess.getOneMessage(), false);

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

line 123why, one step back
123 subprocess.nodeChildProcess.disconnect();

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 124why, one step back
124 const {isCanceled, isGracefullyCanceled, ipcOutput, stdout} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

lines 125–128why, one step back
125 assert.equal(isCanceled, false);
126 assert.equal(isGracefullyCanceled, false);127 assert.deepEqual(ipcOutput, [false]);128 assert.ok(stdout.includes('Error: `cancelSignal` aborted: the parent process disconnected.'));

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 129–130why, one step back
129});
130

TweaksSindre Sorhusbefore adoption698a1b8

line 131why, one step back
131test('getCancelSignal() aborts if already disconnected', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 132–133why, one step back
132 const controller = new AbortController();
133 const subprocess = execa('graceful-print.js', {cancelSignal: controller.signal, gracefulCancel: true});

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

line 134why, one step back
134 subprocess.nodeChildProcess.disconnect();

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 135why, one step back
135 const {isCanceled, isGracefullyCanceled, ipcOutput, stdout} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

lines 136–139why, one step back
136 assert.equal(isCanceled, false);
137 assert.equal(isGracefullyCanceled, false);138 assert.deepEqual(ipcOutput, []);139 assert.ok(stdout.includes('Error: `cancelSignal` aborted: the parent process disconnected.'));

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 140–141why, one step back
140});
141

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 142–152why, one step back
142test('getCancelSignal() aborts if first called after disconnection', async () => {
143 const controller = new AbortController();144 const subprocess = execa('graceful-print-disconnected.js', {cancelSignal: controller.signal, gracefulCancel: true});145 assert.equal(await subprocess.getOneMessage(), '.');146 subprocess.nodeChildProcess.disconnect();147 const {isCanceled, ipcOutput, stdout} = await subprocess;148 assert.equal(isCanceled, false);149 assert.deepEqual(ipcOutput, ['.']);150 assert.ok(stdout.includes('Error: `cancelSignal` aborted: the parent process disconnected.'));151});152

TweaksSindre Sorhusbefore adoption698a1b8

lines 153–160why, one step back
153test('getCancelSignal() fails if no IPC', async () => {
154 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput, stderr} = await assertRejects(execa('graceful-none.js'));155 assert.equal(isCanceled, false);156 assert.equal(isGracefullyCanceled, false);157 assert.equal(isTerminated, false);158 assert.equal(exitCode, 1);159 assert.deepEqual(ipcOutput, []);160 assert.ok(stderr.includes('Error: `getCancelSignal()` cannot be used without setting the `gracefulCancel` option to `true`.'));

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 161–162why, one step back
161});
162

TweaksSindre Sorhusbefore adoption698a1b8

line 163why, one step back
163test('getCancelSignal() fails on every call if no IPC', async () => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 164why, one step back
164 const {exitCode, stdout} = await execa('graceful-none-twice.js', {stripFinalNewline: false});

TweaksSindre Sorhusbefore adoption698a1b8

lines 165–166why, one step back
165 assert.equal(exitCode, 0);
166 assert.equal(stdout, 'threw\nthrew\n');

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 167–168why, one step back
167});
168

TweaksSindre Sorhusbefore adoption698a1b8

line 169why, one step back
169test('getCancelSignal() hangs if cancelSignal without gracefulCancel', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 170why, one step back
170 const controller = new AbortController();

TweaksSindre Sorhusbefore adoption698a1b8

lines 171–176why, one step back
171 const {timedOut, isCanceled, isGracefullyCanceled, signal, ipcOutput} = await assertRejects(execa('graceful-wait.js', {ipc: true, cancelSignal: controller.signal, timeout: 1e3}));
172 assert.equal(timedOut, true);173 assert.equal(isCanceled, false);174 assert.equal(isGracefullyCanceled, false);175 assert.equal(signal, 'SIGTERM');176 assert.deepEqual(ipcOutput, []);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 177–178why, one step back
177});
178

TweaksSindre Sorhusbefore adoption698a1b8

line 179why, one step back
179test('Subprocess cancelSignal does not keep subprocess alive', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 180–181why, one step back
180 const controller = new AbortController();
181 const {ipcOutput} = await execa('graceful-ref.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 182why, one step back
182 assert.deepEqual(ipcOutput, []);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 183–184why, one step back
183});
184

TweaksSindre Sorhusbefore adoption698a1b8

line 185why, one step back
185test('Subprocess can send a message right away', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 186–187why, one step back
186 const controller = new AbortController();
187 const {ipcOutput} = await execa('graceful-send-string.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 188why, one step back
188 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 189–190why, one step back
189});
190

TweaksSindre Sorhusbefore adoption698a1b8

line 191why, one step back
191test('Subprocess can receive a message right away', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 192–193why, one step back
192 const controller = new AbortController();
193 const {ipcOutput} = await execa('graceful-echo.js', {cancelSignal: controller.signal, gracefulCancel: true, ipcInput: foobarString});

TweaksSindre Sorhusbefore adoption698a1b8

line 194why, one step back
194 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 195–196why, one step back
195});
196

TweaksSindre Sorhusbefore adoption698a1b8

lines 197–203why, one step back
197test('getCancelSignal() can be called twice', async () => {
198 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(execa('graceful-twice.js', {cancelSignal: AbortSignal.abort(foobarString), gracefulCancel: true, forceKillAfterDelay: false}));199 assert.equal(isCanceled, true);200 assert.equal(isGracefullyCanceled, true);201 assert.equal(isTerminated, false);202 assert.equal(exitCode, 0);203 assert.deepEqual(ipcOutput, [foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

line 206why, one step back
206test('Graceful cancelSignal can use cancelSignal.onabort', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 207–208why, one step back
207 const controller = new AbortController();
208 const subprocess = execa('graceful-listener.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});

TweaksSindre Sorhusbefore adoption698a1b8

line 209why, one step back
209 assert.equal(await subprocess.getOneMessage(), '.');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 210why, one step back
210 controller.abort(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 211–216why, one step back
211 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
212 assert.equal(isCanceled, true);213 assert.equal(isGracefullyCanceled, true);214 assert.equal(isTerminated, false);215 assert.equal(exitCode, 0);216 assert.deepEqual(ipcOutput, ['.', foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 217–218why, one step back
217});
218

TweaksSindre Sorhusbefore adoption698a1b8

line 219why, one step back
219test('Graceful cancelSignal abort reason cannot be directly received', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 220–222why, one step back
220 const subprocess = execa('graceful-send-echo.js', {cancelSignal: AbortSignal.abort(foobarString), gracefulCancel: true, forceKillAfterDelay: false});
221 await setTimeout(0);222 await subprocess.sendMessage('.');

TweaksSindre Sorhusbefore adoption698a1b8

lines 223–228why, one step back
223 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, ipcOutput} = await assertRejects(subprocess);
224 assert.equal(isCanceled, true);225 assert.equal(isGracefullyCanceled, true);226 assert.equal(isTerminated, false);227 assert.equal(exitCode, 0);228 assert.deepEqual(ipcOutput, ['.', foobarString]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 229–230why, one step back
229});
230

TweaksSindre Sorhusbefore adoption698a1b8

line 231why, one step back
231test('error.isGracefullyCanceled is always false with execaSync()', () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 232why, one step back
232 const {isCanceled, isGracefullyCanceled} = execaSync('empty.js');

TweaksSindre Sorhusbefore adoption698a1b8

lines 233–234why, one step back
233 assert.equal(isCanceled, false);
234 assert.equal(isGracefullyCanceled, false);

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 235–238why, one step back
235});
236237// Execa wraps `gracefulCancel` messages with an internal type, which is only ever sent by the current process to its subprocess.238// A subprocess must not be able to send it in the other direction, since this would abort the parent process' own `cancelSignal`, or silently drop the message.

TweaksSindre Sorhusbefore adoption698a1b8

line 239why, one step back
239test('Subprocess messages with the internal graceful cancellation shape are kept', async () => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 240why, one step back
240 const {ipcOutput} = await execa('ipc-send-cancel-shape.js', {ipc: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 241why, one step back
241 assert.deepEqual(ipcOutput, [{type: 'execa:ipc:cancel', message: foobarString}]);

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 242–243why, one step back
242});
243

TweaksSindre Sorhusbefore adoption698a1b8

line 244why, one step back
244test('Graceful cancelSignal is not aborted by the subprocess\' own subprocess', async () => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 245–246why, one step back
245 const controller = new AbortController();
246 const {ipcOutput} = await execa('graceful-nested.js', {cancelSignal: controller.signal, gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

line 247why, one step back
247 assert.deepEqual(ipcOutput, [false]);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 248why, one step back
248});