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 fileTake it
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 2–3why, one step back2import {getEventListeners} from 'node:events';3import {setTimeout} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
lines 4–5why, one step back4import test from 'node:test';5import {assertRejects} from '../helpers/assert.js';Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 6–11why, one step back6import {execa, execaSync} from '../../index.js';7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {foobarString} from '../helpers/input.js';910setFixtureDirectory();11TweaksSindre Sorhusbefore adoption698a1b8
lines 12–18why, one step back12test('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 back19});20TweaksSindre Sorhusbefore adoption698a1b8
line 21why, one step back21test('Graceful cancelSignal can be aborted', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 22–23why, one step back22 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 back24 assert.equal(await subprocess.getOneMessage(), false);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 25why, one step back25 controller.abort(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 26–31why, one step back26 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 back32});33TweaksSindre Sorhusbefore adoption698a1b8
line 34why, one step back34test('Graceful cancelSignal can be never aborted', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 35–36why, one step back35 const controller = new AbortController();36 const subprocess = execa('graceful-send-fast.js', {cancelSignal: controller.signal, gracefulCancel: true});TweaksSindre Sorhusbefore adoption698a1b8
line 37why, one step back37 assert.equal(await subprocess.getOneMessage(), false);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 38–40why, one step back38 await subprocess;39});40TweaksSindre Sorhusbefore adoption698a1b8
line 41why, one step back41test('Graceful cancelSignal can be already aborted but not used', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 42why, one step back42 const subprocess = execa('ipc-send-get.js', {cancelSignal: AbortSignal.abort(foobarString), gracefulCancel: true, forceKillAfterDelay: false});TweaksSindre Sorhusbefore adoption698a1b8
line 43why, one step back43 assert.equal(await subprocess.getOneMessage(), foobarString);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 44–45why, one step back44 await setTimeout(1e3);45 await subprocess.sendMessage('.');TweaksSindre Sorhusbefore adoption698a1b8
lines 46–51why, one step back46 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 back52});53TweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54test('Graceful cancelSignal can be aborted but not used', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 55–56why, one step back55 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 back57 assert.equal(await subprocess.getOneMessage(), foobarString);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 58–60why, one step back58 controller.abort(foobarString);59 await setTimeout(1e3);60 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 61–66why, one step back61 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 back67});68TweaksSindre Sorhusbefore adoption698a1b8
line 69why, one step back69test('Graceful cancelSignal can be never aborted nor used', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 70–71why, one step back70 const controller = new AbortController();71 const subprocess = execa('empty.js', {cancelSignal: controller.signal, gracefulCancel: true});TweaksSindre Sorhusbefore adoption698a1b8
line 72why, one step back72 assert.equal(getEventListeners(controller.signal, 'abort').length, 1);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 73why, one step back73 await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74 assert.equal(getEventListeners(controller.signal, 'abort').length, 0);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 75–76why, one step back75});76TweaksSindre Sorhusbefore adoption698a1b8
line 77why, one step back77test('Graceful cancelSignal can be aborted twice', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 78–79why, one step back78 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 back80 assert.equal(await subprocess.getOneMessage(), false);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 81–82why, one step back81 controller.abort(foobarString);82 controller.abort('.');TweaksSindre Sorhusbefore adoption698a1b8
lines 83–88why, one step back83 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 back89});90TweaksSindre Sorhusbefore adoption698a1b8
line 91why, one step back91test('Graceful cancelSignal cannot be manually aborted after disconnection', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 92–93why, one step back92 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 back94 subprocess.nodeChildProcess.disconnect();Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 95why, one step back95 controller.abort(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 96–102why, one step back96 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 back103});104TweaksSindre Sorhusbefore adoption698a1b8
line 105why, one step back105test('Graceful cancelSignal can disconnect after being manually aborted', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 106–108why, one step back106 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 back109 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 back110 subprocess.nodeChildProcess.disconnect();TweaksSindre Sorhusbefore adoption698a1b8
lines 111–116why, one step back111 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 back117});118TweaksSindre Sorhusbefore adoption698a1b8
line 119why, one step back119test('Graceful cancelSignal is automatically aborted on disconnection', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 120–121why, one step back120 const controller = new AbortController();121 const subprocess = execa('graceful-send-print.js', {cancelSignal: controller.signal, gracefulCancel: true});TweaksSindre Sorhusbefore adoption698a1b8
line 122why, one step back122 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 back123 subprocess.nodeChildProcess.disconnect();Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 124why, one step back124 const {isCanceled, isGracefullyCanceled, ipcOutput, stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 125–128why, one step back125 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 back129});130TweaksSindre Sorhusbefore adoption698a1b8
line 131why, one step back131test('getCancelSignal() aborts if already disconnected', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 132–133why, one step back132 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 back134 subprocess.nodeChildProcess.disconnect();Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 135why, one step back135 const {isCanceled, isGracefullyCanceled, ipcOutput, stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 136–139why, one step back136 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 back140});141Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 142–152why, one step back142test('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});152TweaksSindre Sorhusbefore adoption698a1b8
lines 153–160why, one step back153test('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 back161});162TweaksSindre Sorhusbefore adoption698a1b8
line 163why, one step back163test('getCancelSignal() fails on every call if no IPC', async () => {Harden some thingsSindre Sorhusbefore adoption4a0a121
line 164why, one step back164 const {exitCode, stdout} = await execa('graceful-none-twice.js', {stripFinalNewline: false});TweaksSindre Sorhusbefore adoption698a1b8
lines 165–166why, one step back165 assert.equal(exitCode, 0);166 assert.equal(stdout, 'threw\nthrew\n');Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 167–168why, one step back167});168TweaksSindre Sorhusbefore adoption698a1b8
line 169why, one step back169test('getCancelSignal() hangs if cancelSignal without gracefulCancel', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 170why, one step back170 const controller = new AbortController();TweaksSindre Sorhusbefore adoption698a1b8
lines 171–176why, one step back171 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 back177});178TweaksSindre Sorhusbefore adoption698a1b8
line 179why, one step back179test('Subprocess cancelSignal does not keep subprocess alive', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 180–181why, one step back180 const controller = new AbortController();181 const {ipcOutput} = await execa('graceful-ref.js', {cancelSignal: controller.signal, gracefulCancel: true});TweaksSindre Sorhusbefore adoption698a1b8
line 182why, one step back182 assert.deepEqual(ipcOutput, []);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 183–184why, one step back183});184TweaksSindre Sorhusbefore adoption698a1b8
line 185why, one step back185test('Subprocess can send a message right away', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 186–187why, one step back186 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 back188 assert.deepEqual(ipcOutput, [foobarString]);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 189–190why, one step back189});190TweaksSindre Sorhusbefore adoption698a1b8
line 191why, one step back191test('Subprocess can receive a message right away', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 192–193why, one step back192 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 back194 assert.deepEqual(ipcOutput, [foobarString]);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 195–196why, one step back195});196TweaksSindre Sorhusbefore adoption698a1b8
lines 197–203why, one step back197test('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 back204});205TweaksSindre Sorhusbefore adoption698a1b8
line 206why, one step back206test('Graceful cancelSignal can use cancelSignal.onabort', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 207–208why, one step back207 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 back209 assert.equal(await subprocess.getOneMessage(), '.');Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 210why, one step back210 controller.abort(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 211–216why, one step back211 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 back217});218TweaksSindre Sorhusbefore adoption698a1b8
line 219why, one step back219test('Graceful cancelSignal abort reason cannot be directly received', async () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 220–222why, one step back220 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 back223 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 back229});230TweaksSindre Sorhusbefore adoption698a1b8
line 231why, one step back231test('error.isGracefullyCanceled is always false with execaSync()', () => {Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 232why, one step back232 const {isCanceled, isGracefullyCanceled} = execaSync('empty.js');TweaksSindre Sorhusbefore adoption698a1b8
lines 233–234why, one step back233 assert.equal(isCanceled, false);234 assert.equal(isGracefullyCanceled, false);Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 235–238why, one step back235});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 back239test('Subprocess messages with the internal graceful cancellation shape are kept', async () => {Harden some thingsSindre Sorhusbefore adoption4a0a121
line 240why, one step back240 const {ipcOutput} = await execa('ipc-send-cancel-shape.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
line 241why, one step back241 assert.deepEqual(ipcOutput, [{type: 'execa:ipc:cancel', message: foobarString}]);Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 242–243why, one step back242});243TweaksSindre Sorhusbefore adoption698a1b8
line 244why, one step back244test('Graceful cancelSignal is not aborted by the subprocess\' own subprocess', async () => {Harden some thingsSindre Sorhusbefore adoption4a0a121
lines 245–246why, one step back245 const controller = new AbortController();246 const {ipcOutput} = await execa('graceful-nested.js', {cancelSignal: controller.signal, gracefulCancel: true});TweaksSindre Sorhusbefore adoption698a1b8
line 247why, one step back247 assert.deepEqual(ipcOutput, [false]);Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 248why, one step back248});