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 fileTake it
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
line 2why, one step back2import {scheduler} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
lines 3–4why, one step back3import test from 'node:test';4import {assertRejects} from '../helpers/assert.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 5–7why, one step back5import {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 back8import {PARALLEL_COUNT} from '../helpers/parallel.js';Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 9why, one step back9import {iterateAllMessages} from '../helpers/ipc.js';Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 10why, one step back10import {assertMaxListeners} from '../helpers/listeners.js';TweaksSindre Sorhusbefore adoption698a1b8
line 11why, one step back11/* 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 back1213setFixtureDirectory();Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 14–27why, one step back1415test('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 back28TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29test('Can iterate over IPC messages', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 30–32why, one step back30 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 back33 assert.equal(message, foobarArray[count++]);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 34why, one step back34 }Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 35why, one step back35Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 36why, one step back36 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 37why, one step back37 assert.deepEqual(ipcOutput, foobarArray);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 38–39why, one step back38});39TweaksSindre Sorhusbefore adoption698a1b8
line 40why, one step back40test('Can iterate over IPC messages in subprocess', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 41–46why, one step back41 const subprocess = execa('ipc-iterate.js', {ipc: true});4243 await subprocess.sendMessage('.');44 await subprocess.sendMessage('.');45 await subprocess.sendMessage(foobarString);46Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 47why, one step back47 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 48why, one step back48 assert.deepEqual(ipcOutput, ['.', '.']);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 49–50why, one step back49});50TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51test('subprocess.getEachMessage() can be called twice at the same time', async () => {Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
line 52why, one step back52 const subprocess = execa('ipc-send-twice.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
line 53why, one step back53 assert.deepEqual(Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
lines 54–56why, one step back54 await Promise.all([iterateAllMessages(subprocess), iterateAllMessages(subprocess)]),55 [foobarArray, foobarArray],56 );Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 57why, one step back57Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 58why, one step back58 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 59why, one step back59 assert.deepEqual(ipcOutput, foobarArray);Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
lines 60–61why, one step back60});61TweaksSindre Sorhusbefore adoption698a1b8
line 62why, one step back62const iterateAndBreak = async subprocess => {Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 63–64why, one step back63 // eslint-disable-next-line no-unreachable-loop64 for await (const message of subprocess.getEachMessage()) {TweaksSindre Sorhusbefore adoption698a1b8
line 65why, one step back65 assert.equal(message, foobarString);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 66–67why, one step back66 break;67 }Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
line 68why, one step back68};Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
line 69why, one step back69TweaksSindre Sorhusbefore adoption698a1b8
line 70why, one step back70test('Breaking in subprocess.getEachMessage() disconnects', async () => {Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
line 71why, one step back71 const subprocess = execa('ipc-iterate-send.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
line 72why, one step back72 await iterateAndBreak(subprocess);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
line 73why, one step back73 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74 assert.deepEqual(ipcOutput, [foobarString]);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 75–76why, one step back75});76TweaksSindre Sorhusbefore adoption698a1b8
line 77why, one step back77test('Breaking from subprocess.getEachMessage() awaits the subprocess', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 78why, one step back78 const subprocess = execa('ipc-send-wait-print.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
line 79why, one step back79 await iterateAndBreak(subprocess);Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
line 80why, one step back80Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 81why, one step back81 const {ipcOutput, stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 82–83why, one step back82 assert.deepEqual(ipcOutput, [foobarString]);83 assert.equal(stdout, '.');Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
lines 84–85why, one step back84});85TweaksSindre Sorhusbefore adoption698a1b8
line 86why, one step back86test('Breaking from exports.getEachMessage() disconnects', async () => {Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
lines 87–88why, one step back87 const subprocess = execa('ipc-iterate-break.js', {ipc: true});88TweaksSindre Sorhusbefore adoption698a1b8
line 89why, one step back89 assert.equal(await subprocess.getOneMessage(), foobarString);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 90why, one step back90 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 91–92why, one step back91 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 back9394 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 95why, one step back95 assert.deepEqual(ipcOutput, [foobarString]);Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
lines 96–97why, one step back96});97TweaksSindre Sorhusbefore adoption698a1b8
line 98why, one step back98const iterateAndThrow = async (subprocess, cause) => {Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 99–100why, one step back99 // eslint-disable-next-line no-unreachable-loop100 for await (const message of subprocess.getEachMessage()) {TweaksSindre Sorhusbefore adoption698a1b8
line 101why, one step back101 assert.equal(message, foobarString);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 102–105why, one step back102 throw cause;103 }104};105TweaksSindre Sorhusbefore adoption698a1b8
line 106why, one step back106test('Throwing from subprocess.getEachMessage() disconnects', async () => {Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 107–109why, one step back107 const subprocess = execa('ipc-iterate-send.js', {ipc: true});108109 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 110why, one step back110 assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 111–112why, one step back111112 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 113why, one step back113 assert.deepEqual(ipcOutput, [foobarString]);Fix `break` statement with `subprocess.getEachMessage()` (#1082)ehmickybefore adoption7143492
lines 114–115why, one step back114});115TweaksSindre Sorhusbefore adoption698a1b8
line 116why, one step back116test('Throwing from subprocess.getEachMessage() awaits the subprocess', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 117why, one step back117 const subprocess = execa('ipc-send-wait-print.js', {ipc: true});Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
line 118why, one step back118 const cause = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 119why, one step back119 assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
line 120why, one step back120Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 121why, one step back121 const {ipcOutput, stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
lines 122–123why, one step back122 assert.deepEqual(ipcOutput, [foobarString]);123 assert.equal(stdout, '.');Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
lines 124–125why, one step back124});125TweaksSindre Sorhusbefore adoption698a1b8
line 126why, one step back126test('Throwing from exports.getEachMessage() disconnects', async () => {Add more IPC-related tests (#1083)ehmickybefore adoptionc09bb18
lines 127–128why, one step back127 const subprocess = execa('ipc-iterate-throw.js', {ipc: true});128TweaksSindre Sorhusbefore adoption698a1b8
line 129why, one step back129 assert.equal(await subprocess.getOneMessage(), foobarString);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 130why, one step back130 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
lines 131–132why, one step back131 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 back133TweaksSindre Sorhusbefore adoption698a1b8
lines 134–138why, one step back134 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 back139});140TweaksSindre Sorhusbefore adoption698a1b8
line 141why, one step back141test('Can send many messages at once with exports.getEachMessage()', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 142why, one step back142 const subprocess = execa('ipc-iterate.js', {ipc: true});Refactor parallel tests (#1084)ehmickybefore adoption5cf6312
line 143why, one step back143 await Promise.all(Array.from({length: PARALLEL_COUNT}, (_, index) => subprocess.sendMessage(index)));Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 144why, one step back144 await subprocess.sendMessage(foobarString);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 145why, one step back145Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 146why, one step back146 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 147why, one step back147 assert.deepEqual(ipcOutput, Array.from({length: PARALLEL_COUNT}, (_, index) => index));Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 148–149why, one step back148});149TweaksSindre Sorhusbefore adoption698a1b8
line 150why, one step back150test('subprocess.getOneMessage() can be called multiple times in a row, buffer true', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 151–156why, one step back151 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 back157 assert.equal(stdout, expectedOutput);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 158–159why, one step back158});159TweaksSindre Sorhusbefore adoption698a1b8
line 160why, one step back160test('Disconnecting in the current process stops exports.getEachMessage()', async () => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 161why, one step back161 const subprocess = execa('ipc-iterate-print.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
line 162why, one step back162 assert.equal(await subprocess.getOneMessage(), foobarString);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 163why, one step back163 await subprocess.sendMessage('.');Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 164why, one step back164 subprocess.nodeChildProcess.disconnect();Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 165why, one step back165Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 166why, one step back166 const {stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 167why, one step back167 assert.equal(stdout, '.');Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 168–169why, one step back168});169TweaksSindre Sorhusbefore adoption698a1b8
line 170why, one step back170test('Disconnecting in the subprocess stops subprocess.getEachMessage()', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 171–172why, one step back171 const subprocess = execa('ipc-send-disconnect.js', {ipc: true});172 for await (const message of subprocess.getEachMessage()) {TweaksSindre Sorhusbefore adoption698a1b8
line 173why, one step back173 assert.equal(message, foobarString);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 174why, one step back174 }Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 175why, one step back175Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 176why, one step back176 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 177why, one step back177 assert.deepEqual(ipcOutput, [foobarString]);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 178–179why, one step back178});179TweaksSindre Sorhusbefore adoption698a1b8
line 180why, one step back180test('Exiting the subprocess stops subprocess.getEachMessage()', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 181–182why, one step back181 const subprocess = execa('ipc-send.js', {ipc: true});182 for await (const message of subprocess.getEachMessage()) {TweaksSindre Sorhusbefore adoption698a1b8
line 183why, one step back183 assert.equal(message, foobarString);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 184why, one step back184 }Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 185why, one step back185Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 186why, one step back186 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 187why, one step back187 assert.deepEqual(ipcOutput, [foobarString]);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 188why, one step back188});Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 189why, one step back189TweaksSindre Sorhusbefore adoption698a1b8
line 190why, one step back190const testCleanupListeners = async buffer => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 191why, one step back191 const subprocess = execa('ipc-send.js', {ipc: true, buffer});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 192why, one step back192TweaksSindre Sorhusbefore adoption698a1b8
lines 193–194why, one step back193 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 back195196 const promise = iterateAllMessages(subprocess);TweaksSindre Sorhusbefore adoption698a1b8
lines 197–199why, one step back197 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 back200TweaksSindre Sorhusbefore adoption698a1b8
lines 201–202why, one step back201 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 back203};Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 204why, one step back204TweaksSindre Sorhusbefore adoption698a1b8
lines 205–206why, one step back205test('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 back207208const sendContinuousMessages = async subprocess => {Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 209why, one step back209 while (subprocess.nodeChildProcess.connected) {Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
line 210why, one step back210 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 back211 subprocess.nodeChildProcess.emit('message', foobarString);Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
lines 212–218why, one step back212 }213214 // eslint-disable-next-line no-await-in-loop215 await scheduler.yield();216 }217};218TweaksSindre Sorhusbefore adoption698a1b8
line 219why, one step back219test('Handles buffered messages when disconnecting', async () => {Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
lines 220–222why, one step back220 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 back223 subprocess.nodeChildProcess.emit('message', foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 224why, one step back224 assert.equal(await promise, foobarString);Improve `buffer: false` behavior with IPC (#1092)ehmickybefore adoption76d637a
lines 225–226why, one step back225 sendContinuousMessages(subprocess);226TweaksSindre Sorhusbefore adoption698a1b8
lines 227–230why, one step back227 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 back231});TweaksSindre Sorhusbefore adoption698a1b8
lines 232–233why, one step back232233/* eslint-enable node-test/no-conditional-assertion */