Skip to content
Myr

graceful.js

This file

Path

test/terminate/graceful.js

180 lines · 8.5 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

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

line 2why, one step back
2import {setTimeout} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 12–13why, one step back
12test('cancelSignal cannot be undefined with gracefulCancel', () => {
13 assertThrows(() => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 14–17why, one step back
14 execa('empty.js', {gracefulCancel: true});
15 }, {message: /The `cancelSignal` option must be defined/});16});17

TweaksSindre Sorhusbefore adoption698a1b8

lines 18–19why, one step back
18test('ipc cannot be false with gracefulCancel', () => {
19 assertThrows(() => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 20–23why, one step back
20 execa('empty.js', {gracefulCancel: true, cancelSignal: AbortSignal.abort(), ipc: false});
21 }, {message: /The `ipc` option cannot be false/});22});23

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–25why, one step back
24test('serialization cannot be "json" with gracefulCancel', () => {
25 assertThrows(() => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 26–29why, one step back
26 execa('empty.js', {gracefulCancel: true, cancelSignal: AbortSignal.abort(), serialization: 'json'});
27 }, {message: /The `serialization` option cannot be 'json'/});28});29

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 31–34why, one step back
31 const controller = new AbortController();
32 const subprocess = execa('ipc-echo.js', {cancelSignal: controller.signal, gracefulCancel: true});33 await subprocess.sendMessage(foobarString);34 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 36–37why, one step back
36});
37

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 42why, one step back
42 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 44–45why, one step back
44});
45

TweaksSindre Sorhusbefore adoption698a1b8

line 46why, one step back
46test('Does not disconnect during I/O errors when sending the abort reason', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 47–51why, one step back
47 const controller = new AbortController();
48 const subprocess = execa('ipc-echo.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});49 const error = mockSendIoError(subprocess);50 controller.abort(foobarString);51 await setTimeout(0);

TweaksSindre Sorhusbefore adoption698a1b8

line 52why, one step back
52 assert.equal(subprocess.nodeChildProcess.connected, true);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 54–59why, one step back
54 const {isCanceled, isGracefullyCanceled, signal, ipcOutput, cause} = await assertRejects(subprocess);
55 assert.equal(isCanceled, false);56 assert.equal(isGracefullyCanceled, false);57 assert.equal(signal, 'SIGTERM');58 assert.deepEqual(ipcOutput, []);59 assert.equal(cause, error);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 60–65why, one step back
60});
6162class AbortError extends Error {63 name = 'AbortError';64}65

TweaksSindre Sorhusbefore adoption698a1b8

line 66why, one step back
66test('Abort reason is sent to the subprocess', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 67–70why, one step back
67 const controller = new AbortController();
68 const subprocess = execa('graceful-send.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});69 const error = new AbortError(foobarString);70 controller.abort(error);

TweaksSindre Sorhusbefore adoption698a1b8

lines 71–79why, one step back
71 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, cause, ipcOutput} = await assertRejects(subprocess);
72 assert.equal(isCanceled, true);73 assert.equal(isGracefullyCanceled, true);74 assert.equal(isTerminated, false);75 assert.equal(exitCode, 0);76 assert.equal(cause, error);77 assert.equal(ipcOutput[0].message, error.message);78 assert.equal(ipcOutput[0].stack, error.stack);79 assert.equal(ipcOutput[0].name, 'Error');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 80–81why, one step back
80});
81

TweaksSindre Sorhusbefore adoption698a1b8

line 82why, one step back
82test('Abort default reason is sent to the subprocess', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 86–90why, one step back
86 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, cause, ipcOutput} = await assertRejects(subprocess);
87 assert.equal(isCanceled, true);88 assert.equal(isGracefullyCanceled, true);89 assert.equal(isTerminated, false);90 assert.equal(exitCode, 0);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 91why, one step back
91 const {reason} = controller.signal;

TweaksSindre Sorhusbefore adoption698a1b8

lines 92–94why, one step back
92 assert.equal(cause.stack, reason.stack);
93 assert.equal(ipcOutput[0].message, reason.message);94 assert.equal(ipcOutput[0].stack, reason.stack);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 95–96why, one step back
95});
96

TweaksSindre Sorhusbefore adoption698a1b8

line 97why, one step back
97test('Fail when sending non-serializable abort reason', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 98–101why, one step back
98 const controller = new AbortController();
99 const subprocess = execa('ipc-echo.js', {cancelSignal: controller.signal, gracefulCancel: true, forceKillAfterDelay: false});100 controller.abort(() => {});101 await setTimeout(0);

TweaksSindre Sorhusbefore adoption698a1b8

line 102why, one step back
102 assert.equal(subprocess.nodeChildProcess.connected, true);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 103why, one step back
103 await subprocess.sendMessage(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

lines 104–111why, one step back
104 const {isCanceled, isGracefullyCanceled, isTerminated, exitCode, cause, ipcOutput} = await assertRejects(subprocess);
105 assert.equal(isCanceled, false);106 assert.equal(isGracefullyCanceled, false);107 assert.equal(isTerminated, false);108 assert.equal(exitCode, 0);109 assert.deepEqual(ipcOutput, [foobarString]);110 assert.equal(cause.message, '`cancelSignal`\'s `controller.abort()`\'s argument type is invalid: the message cannot be serialized: () => {}.');111 assert.equal(cause.cause.message, '() => {} could not be cloned.');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 112–113why, one step back
112});
113

TweaksSindre Sorhusbefore adoption698a1b8

line 114why, one step back
114test('timeout does not use graceful cancelSignal', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 116–124why, one step back
116 const {timedOut, isCanceled, isGracefullyCanceled, isTerminated, signal, exitCode, shortMessage, ipcOutput} = await assertRejects(execa('graceful-send.js', {cancelSignal: controller.signal, gracefulCancel: true, timeout: 1}));
117 assert.equal(timedOut, true);118 assert.equal(isCanceled, false);119 assert.equal(isGracefullyCanceled, false);120 assert.equal(isTerminated, true);121 assert.equal(signal, 'SIGTERM');122 assert.equal(exitCode, undefined);123 assert.equal(shortMessage, 'Command timed out after 1 milliseconds: graceful-send.js');124 assert.deepEqual(ipcOutput, []);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 125–126why, one step back
125});
126

TweaksSindre Sorhusbefore adoption698a1b8

lines 127–134why, one step back
127test('error on graceful cancelSignal on non-0 exit code', async () => {
128 const {isCanceled, isGracefullyCanceled, isTerminated, isForcefullyTerminated, exitCode, shortMessage} = await assertRejects(execa('wait-fail.js', {cancelSignal: AbortSignal.abort(''), gracefulCancel: true, forceKillAfterDelay: false}));129 assert.equal(isCanceled, true);130 assert.equal(isGracefullyCanceled, true);131 assert.equal(isTerminated, false);132 assert.equal(isForcefullyTerminated, false);133 assert.equal(exitCode, 2);134 assert.equal(shortMessage, 'Command was gracefully canceled with exit code 2: wait-fail.js');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 135–136why, one step back
135});
136

TweaksSindre Sorhusbefore adoption698a1b8

lines 137–145why, one step back
137test('error on graceful cancelSignal on forceful termination', async () => {
138 const {isCanceled, isGracefullyCanceled, isTerminated, signal, isForcefullyTerminated, exitCode, shortMessage} = await assertRejects(execa('forever.js', {cancelSignal: AbortSignal.abort(''), gracefulCancel: true, forceKillAfterDelay: 1}));139 assert.equal(isCanceled, true);140 assert.equal(isGracefullyCanceled, true);141 assert.equal(isTerminated, true);142 assert.equal(signal, 'SIGKILL');143 assert.equal(isForcefullyTerminated, true);144 assert.equal(exitCode, undefined);145 assert.equal(shortMessage, 'Command was gracefully canceled and was forcefully terminated after 1 milliseconds: forever.js');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 146–147why, one step back
146});
147

TweaksSindre Sorhusbefore adoption698a1b8

line 148why, one step back
148test('error on graceful cancelSignal on non-forceful termination', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 152–159why, one step back
152 const {isCanceled, isGracefullyCanceled, isTerminated, signal, isForcefullyTerminated, exitCode, shortMessage} = await assertRejects(subprocess);
153 assert.equal(isCanceled, true);154 assert.equal(isGracefullyCanceled, true);155 assert.equal(isTerminated, true);156 assert.equal(signal, 'SIGTERM');157 assert.equal(isForcefullyTerminated, false);158 assert.equal(exitCode, undefined);159 assert.equal(shortMessage, 'Command was gracefully canceled with SIGTERM (Termination): ipc-send-get.js');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

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

TweaksSindre Sorhusbefore adoption698a1b8

line 162why, one step back
162test('`forceKillAfterDelay: false` with the "cancelSignal" option when graceful', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 163–165why, one step back
163 const subprocess = execa('forever.js', {cancelSignal: AbortSignal.abort(''), gracefulCancel: true, forceKillAfterDelay: false});
164 await setTimeout(6e3);165 subprocess.kill('SIGKILL');

TweaksSindre Sorhusbefore adoption698a1b8

lines 166–173why, one step back
166 const {isCanceled, isGracefullyCanceled, isTerminated, signal, isForcefullyTerminated, exitCode, shortMessage} = await assertRejects(subprocess);
167 assert.equal(isCanceled, true);168 assert.equal(isGracefullyCanceled, true);169 assert.equal(isTerminated, true);170 assert.equal(signal, 'SIGKILL');171 assert.equal(isForcefullyTerminated, false);172 assert.equal(exitCode, undefined);173 assert.equal(shortMessage, 'Command was gracefully canceled with SIGKILL (Forced termination): forever.js');

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 174–175why, one step back
174});
175

TweaksSindre Sorhusbefore adoption698a1b8

line 176why, one step back
176test('subprocess.getCancelSignal() is not defined', async () => {

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 177why, one step back
177 const subprocess = execa('empty.js', {cancelSignal: AbortSignal.abort(''), gracefulCancel: true});

TweaksSindre Sorhusbefore adoption698a1b8

lines 178–179why, one step back
178 assert.equal(subprocess.getCancelSignal, undefined);
179 await assertRejects(subprocess);

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 180why, one step back
180});