Skip to content
Myr

strict.js

This file

Path

test/ipc/strict.js

378 lines · 18.3 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 `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 2why, one step back
2import {once} from 'node:events';

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 6–14why, one step back
6import {execa} from '../../index.js';
7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {foobarString} from '../helpers/input.js';9import {assertMaxListeners} from '../helpers/listeners.js';10import {subprocessGetOne, subprocessGetFirst, mockSendIoError} from '../helpers/ipc.js';11import {PARALLEL_COUNT} from '../helpers/parallel.js';1213setFixtureDirectory();14

TweaksSindre Sorhusbefore adoption698a1b8

line 15why, one step back
15const testStrictSuccessParentOne = async buffer => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 16–17why, one step back
16 const subprocess = execa('ipc-echo.js', {ipc: true, buffer});
17 await subprocess.sendMessage(foobarString, {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 19–20why, one step back
19
20 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 21why, one step back
21 assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 22–23why, one step back
22};
23

TweaksSindre Sorhusbefore adoption698a1b8

lines 24–25why, one step back
24test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getOneMessage(), buffer false', () => testStrictSuccessParentOne(false));
25test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getOneMessage(), buffer true', () => testStrictSuccessParentOne(true));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 26why, one step back
26

TweaksSindre Sorhusbefore adoption698a1b8

line 27why, one step back
27const testStrictSuccessParentEach = async buffer => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 28–29why, one step back
28 const subprocess = execa('ipc-iterate.js', {ipc: true, buffer});
29 await subprocess.sendMessage('.', {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 31–33why, one step back
31 await subprocess.sendMessage(foobarString);
3233 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 34why, one step back
34 assert.deepEqual(ipcOutput, buffer ? ['.'] : []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 35–36why, one step back
35};
36

TweaksSindre Sorhusbefore adoption698a1b8

lines 37–38why, one step back
37test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getEachMessage(), buffer false', () => testStrictSuccessParentEach(false));
38test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getEachMessage(), buffer true', () => testStrictSuccessParentEach(true));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 39why, one step back
39

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 40–73why, one step back
40// The message must not be wrapped with the `strict` metadata
41test('subprocess.sendMessage() "strict" delivers the message if the subprocess disconnects before handling it', async () => {42 const subprocess = execa('ipc-get-strict-disconnect.js', {ipc: true});43 assert.equal(await subprocess.getOneMessage(), '.');44 await assertRejects(subprocess.sendMessage(foobarString, {strict: true}), {message: /^subprocess\.sendMessage\(\) failed: /});45 const {stdout} = await subprocess;46 assert.equal(stdout, JSON.stringify(foobarString));47});4849test('exports.sendMessage() "strict" delivers the message if the parent disconnects before handling it', async () => {50 const subprocess = execa('ipc-send-strict.js', {ipc: true, reject: false});51 subprocess.nodeChildProcess.once('message', () => {52 subprocess.nodeChildProcess.disconnect();53 });54 const {ipcOutput} = await subprocess;55 assert.deepEqual(ipcOutput, [foobarString]);56});5758// The acknowledgment cannot be sent back, but the message is still received59test('subprocess.sendMessage() "strict" delivers the message if the current process disconnects right after sending it', async () => {60 const subprocess = execa('ipc-get-print-ready.js', {ipc: true});61 assert.equal(await subprocess.getOneMessage(), '.');62 const sendPromise = subprocess.sendMessage(foobarString, {strict: true});63 subprocess.nodeChildProcess.disconnect();64 await assertRejects(sendPromise);65 const {stdout} = await subprocess;66 assert.equal(stdout, foobarString);67});6869test('exports.sendMessage() "strict" delivers the message if the subprocess disconnects right after sending it', async () => {70 const {ipcOutput} = await execa('ipc-send-strict-disconnect.js', {ipc: true});71 assert.deepEqual(ipcOutput, [foobarString]);72});73

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74const testStrictMissingParent = async buffer => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 75–77why, one step back
75 const subprocess = execa('ipc-echo-twice.js', {ipcInput: foobarString, buffer});
76 const promise = subprocess.getOneMessage();77 const secondPromise = subprocess.sendMessage(foobarString, {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

lines 78–80why, one step back
78 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
79 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess is not listening to incoming messages.');80 assert.equal(await promise, foobarString);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 81–83why, one step back
81 await secondPromise;
8283 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 84why, one step back
84 assert.deepEqual(ipcOutput, buffer ? [foobarString, foobarString] : []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 85–86why, one step back
85};
86

TweaksSindre Sorhusbefore adoption698a1b8

lines 87–88why, one step back
87test('subprocess.sendMessage() "strict" fails if the subprocess is not listening, buffer false', () => testStrictMissingParent(false));
88test('subprocess.sendMessage() "strict" fails if the subprocess is not listening, buffer true', () => testStrictMissingParent(true));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 89why, one step back
89

TweaksSindre Sorhusbefore adoption698a1b8

line 90why, one step back
90const testStrictExit = async buffer => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 91why, one step back
91 const subprocess = execa('ipc-send.js', {ipc: true, buffer});

TweaksSindre Sorhusbefore adoption698a1b8

lines 92–93why, one step back
92 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
93 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 94–95why, one step back
94
95 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 96why, one step back
96 assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 97–98why, one step back
97};
98

TweaksSindre Sorhusbefore adoption698a1b8

lines 99–100why, one step back
99test('subprocess.sendMessage() "strict" fails if the subprocess exits, buffer false', () => testStrictExit(false));
100test('subprocess.sendMessage() "strict" fails if the subprocess exits, buffer true', () => testStrictExit(true));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 101why, one step back
101

TweaksSindre Sorhusbefore adoption698a1b8

line 102why, one step back
102const testStrictSuccessSubprocess = async (getMessage, buffer) => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 103why, one step back
103 const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer});

TweaksSindre Sorhusbefore adoption698a1b8

line 104why, one step back
104 assert.equal(await getMessage(subprocess), foobarString);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 105–106why, one step back
105
106 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 107why, one step back
107 assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 108–109why, one step back
108};
109

TweaksSindre Sorhusbefore adoption698a1b8

lines 110–113why, one step back
110test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getOneMessage(), buffer false', () => testStrictSuccessSubprocess(subprocessGetOne, false));
111test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getOneMessage(), buffer true', () => testStrictSuccessSubprocess(subprocessGetOne, true));112test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getEachMessage(), buffer false', () => testStrictSuccessSubprocess(subprocessGetFirst, false));113test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getEachMessage(), buffer true', () => testStrictSuccessSubprocess(subprocessGetFirst, true));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 114why, one step back
114

TweaksSindre Sorhusbefore adoption698a1b8

line 115why, one step back
115test('exports.sendMessage() "strict" succeeds if the current process uses result.ipcOutput', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 120–125why, one step back
120test('exports.sendMessage() "strict" fails if the current process is not listening, buffer false', async () => {
121 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}}));122 assert.equal(exitCode, 1);123 assert.equal(isTerminated, false);124 assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is not listening to incoming messages.'));125 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 128–129why, one step back
128test('Multiple subprocess.sendMessage() "strict" at once', async () => {
129 const checkMaxListeners = assertMaxListeners();

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 130–136why, one step back
130
131 const subprocess = execa('ipc-iterate.js', {ipc: true});132 const messages = Array.from({length: PARALLEL_COUNT}, (_, index) => index);133 await Promise.all(messages.map(message => subprocess.sendMessage(message, {strict: true})));134 await subprocess.sendMessage(foobarString);135136 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 137why, one step back
137 assert.deepEqual(ipcOutput, messages);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 138–141why, one step back
138
139 checkMaxListeners();140});141

TweaksSindre Sorhusbefore adoption698a1b8

line 142why, one step back
142test('subprocess.sendMessage() "strict" fails if the subprocess uses once()', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 143why, one step back
143 const subprocess = execa('ipc-once-message.js', {ipc: true});

TweaksSindre Sorhusbefore adoption698a1b8

lines 144–145why, one step back
144 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
145 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 146–147why, one step back
146
147 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 149–150why, one step back
149});
150

TweaksSindre Sorhusbefore adoption698a1b8

line 151why, one step back
151test('exports.sendMessage() "strict" fails if the current process uses once() and buffer false', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 152why, one step back
152 const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}});

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

line 153why, one step back
153 const [message] = await once(subprocess.nodeChildProcess, 'message');

TweaksSindre Sorhusbefore adoption698a1b8

line 154why, one step back
154 assert.deepEqual(message, {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

line 155why, one step back
155 id: 0,

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 156–159why, one step back
156 type: 'execa:ipc:request',
157 message: foobarString,158 hasListeners: false,159 });

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 160why, one step back
160

TweaksSindre Sorhusbefore adoption698a1b8

lines 161–165why, one step back
161 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess);
162 assert.equal(exitCode, 1);163 assert.equal(isTerminated, false);164 assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is not listening to incoming messages.'));165 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

line 168why, one step back
168test('subprocess.sendMessage() "strict" failure disconnects', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 169–171why, one step back
169 const subprocess = execa('ipc-echo-twice-wait.js', {ipcInput: foobarString});
170 const promise = subprocess.getOneMessage();171 const secondPromise = subprocess.sendMessage(foobarString, {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

lines 172–174why, one step back
172 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
173 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess is not listening to incoming messages.');174 assert.equal(await promise, foobarString);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 175–176why, one step back
175 await secondPromise;
176

TweaksSindre Sorhusbefore adoption698a1b8

lines 177–181why, one step back
177 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess);
178 assert.equal(exitCode, 1);179 assert.equal(isTerminated, false);180 assert.ok(stderr.includes('Error: sendMessage() cannot be used: the parent process has already exited or disconnected.'));181 assert.deepEqual(ipcOutput, [foobarString, foobarString]);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 184–189why, one step back
184test('exports.sendMessage() "strict" failure disconnects', async () => {
185 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(execa('ipc-send-strict-catch.js', {ipc: true, buffer: {ipc: false}}));186 assert.equal(exitCode, 1);187 assert.equal(isTerminated, false);188 assert.ok(stderr.includes('Error: sendMessage() cannot be used: the parent process has already exited or disconnected.'));189 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

line 192why, one step back
192const testIoErrorParent = async getMessage => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 193–194why, one step back
193 const subprocess = execa('ipc-send-strict.js', {ipc: true});
194 const cause = mockSendIoError(subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

lines 195–197why, one step back
195 const error = await assertRejects(getMessage(subprocess));
196 assert.ok(error.message.includes('subprocess.sendMessage() failed when sending an acknowledgment response to the subprocess.'));197 assert.equal(getMessage === subprocessGetOne ? error.cause : error.cause.cause, cause);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 198why, one step back
198

TweaksSindre Sorhusbefore adoption698a1b8

lines 199–203why, one step back
199 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess);
200 assert.equal(exitCode, 1);201 assert.equal(isTerminated, false);202 assert.ok(stderr.includes('Error: sendMessage() failed: the parent process exited without listening to incoming messages.'));203 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 206–207why, one step back
206test('subprocess.getOneMessage() acknowledgment I/O error', () => testIoErrorParent(subprocessGetOne));
207test('subprocess.getEachMessage() acknowledgment I/O error', () => testIoErrorParent(subprocessGetFirst));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 208why, one step back
208

TweaksSindre Sorhusbefore adoption698a1b8

line 209why, one step back
209const testIoErrorSubprocess = async fixtureName => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 210why, one step back
210 const subprocess = execa(fixtureName, {ipc: true});

TweaksSindre Sorhusbefore adoption698a1b8

lines 211–212why, one step back
211 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
212 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 213why, one step back
213

TweaksSindre Sorhusbefore adoption698a1b8

lines 214–220why, one step back
214 const {exitCode, isTerminated, stdout, stderr, ipcOutput} = await assertRejects(subprocess);
215 assert.equal(exitCode, 1);216 assert.equal(isTerminated, false);217 assert.equal(stdout, '');218 assert.ok(stderr.includes('Error: sendMessage() failed when sending an acknowledgment response to the parent process.'));219 assert.ok(stderr.includes(`Error: ${foobarString}`));220 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 221–222why, one step back
221};
222

TweaksSindre Sorhusbefore adoption698a1b8

lines 223–224why, one step back
223test('exports.getOneMessage() acknowledgment I/O error', () => testIoErrorSubprocess('ipc-get-io-error.js'));
224test('exports.getEachMessage() acknowledgment I/O error', () => testIoErrorSubprocess('ipc-iterate-io-error.js'));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 225why, one step back
225

TweaksSindre Sorhusbefore adoption698a1b8

line 226why, one step back
226test('Opposite sendMessage() "strict", buffer true', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 227–230why, one step back
227 const subprocess = execa('ipc-send-strict-get.js', {ipc: true});
228 await subprocess.sendMessage(foobarString, {strict: true});229230 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 232–233why, one step back
232});
233

TweaksSindre Sorhusbefore adoption698a1b8

line 234why, one step back
234test('Opposite sendMessage() "strict", current process listening, buffer false', async () => {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 235why, one step back
235 const subprocess = execa('ipc-send-strict-get.js', {ipc: true, buffer: {ipc: false}});

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 236–239why, one step back
236 const [message] = await Promise.all([
237 subprocess.getOneMessage(),238 subprocess.sendMessage(foobarString, {strict: true}),239 ]);

TweaksSindre Sorhusbefore adoption698a1b8

lines 240–241why, one step back
240 assert.equal(message, foobarString);
241 assert.equal(await subprocess.getOneMessage(), foobarString);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 242–243why, one step back
242
243 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 245–246why, one step back
245});
246

TweaksSindre Sorhusbefore adoption698a1b8

line 247why, one step back
247test('Opposite sendMessage() "strict", subprocess listening, buffer false', async () => {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 248why, one step back
248 const subprocess = execa('ipc-send-strict-listen.js', {ipc: true, buffer: {ipc: false}});

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 249why, one step back
249 await subprocess.sendMessage(foobarString, {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 251–252why, one step back
251
252 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 254–255why, one step back
254});
255

TweaksSindre Sorhusbefore adoption698a1b8

line 256why, one step back
256test('Opposite sendMessage() "strict", not listening, buffer false', async () => {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 257why, one step back
257 const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}});

TweaksSindre Sorhusbefore adoption698a1b8

lines 258–259why, one step back
258 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
259 assert.ok(message.startsWith('subprocess.sendMessage() failed: the subprocess is sending a message too, instead of listening to incoming messages.'));

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 260why, one step back
260

TweaksSindre Sorhusbefore adoption698a1b8

lines 261–265why, one step back
261 const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess);
262 assert.equal(exitCode, 1);263 assert.equal(isTerminated, false);264 assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is sending a message too, instead of listening to incoming messages.'));265 assert.deepEqual(ipcOutput, []);

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 266why, one step back
266});

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 267why, one step back
267

TweaksSindre Sorhusbefore adoption698a1b8

line 268why, one step back
268test('Ignores "strict" responses with an Object.prototype property as id', async () => {

Harden some thingsSindre Sorhusbefore adoption4a0a121

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 271–272why, one step back
271});
272

TweaksSindre Sorhusbefore adoption698a1b8

lines 273–289why, one step back
273// `forever.js` does not use Execa, so it never acknowledges the message
274test('Ignores "strict" responses sent by another subprocess', async () => {275 const subprocess = execa('forever.js', {ipc: true});276 const sendPromise = subprocess.sendMessage(foobarString, {strict: true});277278 try {279 const {ipcOutput} = await execa('ipc-send-strict-forged.js', {ipc: true});280 assert.deepEqual(ipcOutput, []);281 assert.equal(await Promise.race([sendPromise.then(() => 'resolved'), setTimeout(500, 'pending')]), 'pending');282 } finally {283 subprocess.kill();284 }285286 await Promise.all([assertRejects(sendPromise), assertRejects(subprocess)]);287});288289const testStrictJson = async buffer => {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 290–291why, one step back
290 const subprocess = execa('ipc-echo.js', {ipc: true, serialization: 'json', buffer});
291 await subprocess.sendMessage(foobarString, {strict: true});

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 293–294why, one step back
293
294 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 295why, one step back
295 assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 296–297why, one step back
296};
297

TweaksSindre Sorhusbefore adoption698a1b8

lines 298–299why, one step back
298test('subprocess.sendMessage() "strict" works with serialization "json", buffer false', () => testStrictJson(false));
299test('subprocess.sendMessage() "strict" works with serialization "json", buffer true', () => testStrictJson(true));

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

line 300why, one step back
300

TweaksSindre Sorhusbefore adoption698a1b8

line 301why, one step back
301test('exports.sendMessage() "strict" works with serialization "json"', async () => {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 302–305why, one step back
302 const subprocess = execa('ipc-send-strict-get.js', {ipc: true, serialization: 'json'});
303 await subprocess.sendMessage(foobarString, {strict: true});304305 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 307–308why, one step back
307});
308

TweaksSindre Sorhusbefore adoption698a1b8

line 309why, one step back
309test('subprocess.sendMessage() "strict" fails with serialization "json" if the subprocess is not listening', async () => {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

line 310why, one step back
310 const subprocess = execa('ipc-send.js', {ipc: true, serialization: 'json'});

TweaksSindre Sorhusbefore adoption698a1b8

lines 311–312why, one step back
311 const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true}));
312 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 313–314why, one step back
313
314 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 316–322why, one step back
316});
317318const noop = () => {};319320// Messages received in the same batch are processed one after the other.321// The deadlock must be detected on the message being processed, not on the first one received.322// Without the detection, the subprocess would deadlock, so the `timeout` turns that hang into a failed assertion.

TweaksSindre Sorhusbefore adoption698a1b8

line 323why, one step back
323test('Detects a "strict" deadlock on a later message of the same batch', async () => {

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 324–327why, one step back
324 const subprocess = execa('ipc-echo-strict-deadlock.js', {ipc: true, buffer: {ipc: false}, timeout: 1e4});
325 await subprocess.sendMessage(foobarString);326 subprocess.sendMessage(foobarString, {strict: true}).catch(noop);327

TweaksSindre Sorhusbefore adoption698a1b8

lines 328–332why, one step back
328 const {exitCode, timedOut, stderr, ipcOutput} = await assertRejects(subprocess);
329 assert.equal(timedOut, false);330 assert.equal(exitCode, 1);331 assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is sending a message too, instead of listening to incoming messages.'));332 assert.deepEqual(ipcOutput, []);

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 333–377why, one step back
333});
334335// Messages which look like Execa's internal `strict` messages but are sent without the `strict` option are user messages.336// They must not wait for an acknowledgment response, and must be delivered as is.337test('sendMessage() does not wait for an acknowledgment response unless the "strict" option is true', async () => {338 const subprocess = execa('forever.js', {ipc: true});339 const message = {340 type: 'execa:ipc:request',341 id: 0,342 message: foobarString,343 hasListeners: false,344 };345346 try {347 await subprocess.sendMessage(message);348 } finally {349 subprocess.kill();350 }351352 await assertRejects(subprocess);353});354355test('Messages with the internal "strict" request type are kept as is', async () => {356 const message = {357 type: 'execa:ipc:request',358 };359 const {ipcOutput} = await execa('ipc-send-object.js', [JSON.stringify(message)], {ipc: true});360 assert.deepEqual(ipcOutput, [message]);361});362363test('Messages with the internal "strict" request type are kept as is, from the parent', async () => {364 const message = {365 type: 'execa:ipc:request',366 message: foobarString,367 };368 const {ipcOutput} = await execa('ipc-echo.js', {ipcInput: message});369 assert.deepEqual(ipcOutput, [message]);370});371372test('Messages with the internal "strict" request type do not fail pending "strict" sends', async () => {373 const subprocess = execa('ipc-send-object.js', [JSON.stringify({type: 'execa:ipc:request'})], {ipc: true, buffer: {ipc: false}});374 const sendPromise = subprocess.sendMessage(foobarString, {strict: true});375 const {message} = await assertRejects(sendPromise);376 assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');377 await subprocess;

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 378why, one step back
378});