Skip to content
Myr

get-each.js

This file

Path

test/ipc/get-each.js

233 lines · 8.6 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';

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 5–7why, one step back
5import {execa} from '../../index.js';
6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';7import {foobarString, foobarArray} from '../helpers/input.js';

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

line 8why, one step back
8import {PARALLEL_COUNT} from '../helpers/parallel.js';

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 9why, one step back
9import {iterateAllMessages} from '../helpers/ipc.js';

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 10why, one step back
10import {assertMaxListeners} from '../helpers/listeners.js';

TweaksSindre Sorhusbefore adoption698a1b8

line 11why, one step back
11/* eslint-disable node-test/no-conditional-assertion -- shared helper functions are called from conditional paths on purpose */

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 12–13why, one step back
12
13setFixtureDirectory();

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 14–27why, one step back
14
15test('subprocess.getEachMessage() can be called many times at the same time', async () => {16 const checkMaxListeners = assertMaxListeners();17 const subprocess = execa('ipc-send.js', {ipc: true});18 const messages = await Promise.all(Array.from({length: PARALLEL_COUNT}, async () => {19 const iterator = subprocess.getEachMessage();20 const {value} = await iterator.next();21 await iterator.return();22 return value;23 }));24 assert.deepEqual(messages, Array.from({length: PARALLEL_COUNT}, () => foobarString));25 await subprocess;26 checkMaxListeners();27});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 28why, one step back
28

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29test('Can iterate over IPC messages', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 30–32why, one step back
30 let count = 0;
31 const subprocess = execa('ipc-send-twice.js', {ipc: true});32 for await (const message of subprocess.getEachMessage()) {

TweaksSindre Sorhusbefore adoption698a1b8

line 33why, one step back
33 assert.equal(message, foobarArray[count++]);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 34why, one step back
34 }

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 35why, one step back
35

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37 assert.deepEqual(ipcOutput, foobarArray);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

line 40why, one step back
40test('Can iterate over IPC messages in subprocess', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 41–46why, one step back
41 const subprocess = execa('ipc-iterate.js', {ipc: true});
4243 await subprocess.sendMessage('.');44 await subprocess.sendMessage('.');45 await subprocess.sendMessage(foobarString);46

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 49–50why, one step back
49});
50

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51test('subprocess.getEachMessage() can be called twice at the same time', async () => {

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

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

TweaksSindre Sorhusbefore adoption698a1b8

line 53why, one step back
53 assert.deepEqual(

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

lines 54–56why, one step back
54 await Promise.all([iterateAllMessages(subprocess), iterateAllMessages(subprocess)]),
55 [foobarArray, foobarArray],56 );

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 57why, one step back
57

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

line 59why, one step back
59 assert.deepEqual(ipcOutput, foobarArray);

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

lines 60–61why, one step back
60});
61

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62const iterateAndBreak = async subprocess => {

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 63–64why, one step back
63 // eslint-disable-next-line no-unreachable-loop
64 for await (const message of subprocess.getEachMessage()) {

TweaksSindre Sorhusbefore adoption698a1b8

line 65why, one step back
65 assert.equal(message, foobarString);

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 66–67why, one step back
66 break;
67 }

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

line 68why, one step back
68};

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

line 69why, one step back
69

TweaksSindre Sorhusbefore adoption698a1b8

line 70why, one step back
70test('Breaking in subprocess.getEachMessage() disconnects', async () => {

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

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

TweaksSindre Sorhusbefore adoption698a1b8

line 72why, one step back
72 await iterateAndBreak(subprocess);

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

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

TweaksSindre Sorhusbefore adoption698a1b8

line 77why, one step back
77test('Breaking from subprocess.getEachMessage() awaits the subprocess', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

line 79why, one step back
79 await iterateAndBreak(subprocess);

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

line 80why, one step back
80

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 82–83why, one step back
82 assert.deepEqual(ipcOutput, [foobarString]);
83 assert.equal(stdout, '.');

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

lines 84–85why, one step back
84});
85

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86test('Breaking from exports.getEachMessage() disconnects', async () => {

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

lines 87–88why, one step back
87 const subprocess = execa('ipc-iterate-break.js', {ipc: true});
88

TweaksSindre Sorhusbefore adoption698a1b8

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 91–92why, one step back
91 const ipcError = await assertRejects(subprocess.getOneMessage());
92 assert.ok(ipcError.message.includes('subprocess.getOneMessage() could not complete'));

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

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

TweaksSindre Sorhusbefore adoption698a1b8

line 98why, one step back
98const iterateAndThrow = async (subprocess, cause) => {

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 99–100why, one step back
99 // eslint-disable-next-line no-unreachable-loop
100 for await (const message of subprocess.getEachMessage()) {

TweaksSindre Sorhusbefore adoption698a1b8

line 101why, one step back
101 assert.equal(message, foobarString);

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 102–105why, one step back
102 throw cause;
103 }104};105

TweaksSindre Sorhusbefore adoption698a1b8

line 106why, one step back
106test('Throwing from subprocess.getEachMessage() disconnects', async () => {

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 107–109why, one step back
107 const subprocess = execa('ipc-iterate-send.js', {ipc: true});
108109 const cause = new Error(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110 assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 111–112why, one step back
111
112 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

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

Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492

lines 114–115why, one step back
114});
115

TweaksSindre Sorhusbefore adoption698a1b8

line 116why, one step back
116test('Throwing from subprocess.getEachMessage() awaits the subprocess', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

line 118why, one step back
118 const cause = new Error(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 119why, one step back
119 assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

line 120why, one step back
120

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 122–123why, one step back
122 assert.deepEqual(ipcOutput, [foobarString]);
123 assert.equal(stdout, '.');

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

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

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126test('Throwing from exports.getEachMessage() disconnects', async () => {

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

lines 127–128why, one step back
127 const subprocess = execa('ipc-iterate-throw.js', {ipc: true});
128

TweaksSindre Sorhusbefore adoption698a1b8

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 131–132why, one step back
131 const ipcError = await assertRejects(subprocess.getOneMessage());
132 assert.ok(ipcError.message.includes('subprocess.getOneMessage() could not complete'));

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

line 133why, one step back
133

TweaksSindre Sorhusbefore adoption698a1b8

lines 134–138why, one step back
134 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess);
135 assert.equal(exitCode, 1);136 assert.equal(isTerminated, false);137 assert.ok(message.includes(`Error: ${foobarString}`));138 assert.deepEqual(ipcOutput, [foobarString]);

Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18

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

TweaksSindre Sorhusbefore adoption698a1b8

line 141why, one step back
141test('Can send many messages at once with exports.getEachMessage()', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

line 143why, one step back
143 await Promise.all(Array.from({length: PARALLEL_COUNT}, (_, index) => subprocess.sendMessage(index)));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 145why, one step back
145

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

line 147why, one step back
147 assert.deepEqual(ipcOutput, Array.from({length: PARALLEL_COUNT}, (_, index) => index));

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

line 150why, one step back
150test('subprocess.getOneMessage() can be called multiple times in a row, buffer true', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 151–156why, one step back
151 const subprocess = execa('ipc-print-many-each.js', [`${PARALLEL_COUNT}`], {ipc: true});
152 const indexes = Array.from({length: PARALLEL_COUNT}, (_, index) => `${index}`);153 await Promise.all(indexes.map(index => subprocess.sendMessage(index)));154155 const {stdout} = await subprocess;156 const expectedOutput = indexes.join('\n');

TweaksSindre Sorhusbefore adoption698a1b8

line 157why, one step back
157 assert.equal(stdout, expectedOutput);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 158–159why, one step back
158});
159

TweaksSindre Sorhusbefore adoption698a1b8

line 160why, one step back
160test('Disconnecting in the current process stops exports.getEachMessage()', async () => {

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 161why, one step back
161 const subprocess = execa('ipc-iterate-print.js', {ipc: true});

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 163why, one step back
163 await subprocess.sendMessage('.');

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

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

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 165why, one step back
165

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 166why, one step back
166 const {stdout} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 167why, one step back
167 assert.equal(stdout, '.');

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

line 170why, one step back
170test('Disconnecting in the subprocess stops subprocess.getEachMessage()', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 171–172why, one step back
171 const subprocess = execa('ipc-send-disconnect.js', {ipc: true});
172 for await (const message of subprocess.getEachMessage()) {

TweaksSindre Sorhusbefore adoption698a1b8

line 173why, one step back
173 assert.equal(message, foobarString);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 174why, one step back
174 }

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 175why, one step back
175

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

line 180why, one step back
180test('Exiting the subprocess stops subprocess.getEachMessage()', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 181–182why, one step back
181 const subprocess = execa('ipc-send.js', {ipc: true});
182 for await (const message of subprocess.getEachMessage()) {

TweaksSindre Sorhusbefore adoption698a1b8

line 183why, one step back
183 assert.equal(message, foobarString);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 184why, one step back
184 }

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 185why, one step back
185

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 188why, one step back
188});

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 189why, one step back
189

TweaksSindre Sorhusbefore adoption698a1b8

line 190why, one step back
190const testCleanupListeners = async buffer => {

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 192why, one step back
192

TweaksSindre Sorhusbefore adoption698a1b8

lines 193–194why, one step back
193 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1);
194 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 195–196why, one step back
195
196 const promise = iterateAllMessages(subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

lines 197–199why, one step back
197 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1);
198 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);199 assert.deepEqual(await promise, [foobarString]);

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 200why, one step back
200

TweaksSindre Sorhusbefore adoption698a1b8

lines 201–202why, one step back
201 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 0);
202 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 0);

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 203why, one step back
203};

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 204why, one step back
204

TweaksSindre Sorhusbefore adoption698a1b8

lines 205–206why, one step back
205test('Cleans up subprocess.getEachMessage() listeners, buffer false', () => testCleanupListeners(false));
206test('Cleans up subprocess.getEachMessage() listeners, buffer true', () => testCleanupListeners(true));

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

lines 207–208why, one step back
207
208const sendContinuousMessages = async subprocess => {

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

line 209why, one step back
209 while (subprocess.nodeChildProcess.connected) {

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

line 210why, one step back
210 for (let index = 0; index < 10; index += 1) {

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

line 211why, one step back
211 subprocess.nodeChildProcess.emit('message', foobarString);

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

lines 212–218why, one step back
212 }
213214 // eslint-disable-next-line no-await-in-loop215 await scheduler.yield();216 }217};218

TweaksSindre Sorhusbefore adoption698a1b8

line 219why, one step back
219test('Handles buffered messages when disconnecting', async () => {

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

lines 220–222why, one step back
220 const subprocess = execa('ipc-send-fail.js', {ipc: true, buffer: false});
221222 const promise = subprocess.getOneMessage();

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

line 223why, one step back
223 subprocess.nodeChildProcess.emit('message', foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 224why, one step back
224 assert.equal(await promise, foobarString);

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

lines 225–226why, one step back
225 sendContinuousMessages(subprocess);
226

TweaksSindre Sorhusbefore adoption698a1b8

lines 227–230why, one step back
227 const {exitCode, isTerminated, ipcOutput} = await assertRejects(iterateAllMessages(subprocess));
228 assert.equal(exitCode, 1);229 assert.equal(isTerminated, false);230 assert.deepEqual(ipcOutput, []);

Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a

line 231why, one step back
231});

TweaksSindre Sorhusbefore adoption698a1b8

lines 232–233why, one step back
232
233/* eslint-enable node-test/no-conditional-assertion */