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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234import assert from 'node:assert/strict';import {scheduler} from 'node:timers/promises';import test from 'node:test';import {assertRejects} from '../helpers/assert.js';import {execa} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {foobarString, foobarArray} from '../helpers/input.js';import {PARALLEL_COUNT} from '../helpers/parallel.js';import {iterateAllMessages} from '../helpers/ipc.js';import {assertMaxListeners} from '../helpers/listeners.js';/* eslint-disable node-test/no-conditional-assertion -- shared helper functions are called from conditional paths on purpose */
setFixtureDirectory();
test('subprocess.getEachMessage() can be called many times at the same time', async () => { const checkMaxListeners = assertMaxListeners(); const subprocess = execa('ipc-send.js', {ipc: true}); const messages = await Promise.all(Array.from({length: PARALLEL_COUNT}, async () => { const iterator = subprocess.getEachMessage(); const {value} = await iterator.next(); await iterator.return(); return value; })); assert.deepEqual(messages, Array.from({length: PARALLEL_COUNT}, () => foobarString)); await subprocess; checkMaxListeners();});
test('Can iterate over IPC messages', async () => { let count = 0; const subprocess = execa('ipc-send-twice.js', {ipc: true}); for await (const message of subprocess.getEachMessage()) { assert.equal(message, foobarArray[count++]); }
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, foobarArray);});
test('Can iterate over IPC messages in subprocess', async () => { const subprocess = execa('ipc-iterate.js', {ipc: true});
await subprocess.sendMessage('.'); await subprocess.sendMessage('.'); await subprocess.sendMessage(foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, ['.', '.']);});
test('subprocess.getEachMessage() can be called twice at the same time', async () => { const subprocess = execa('ipc-send-twice.js', {ipc: true}); assert.deepEqual( await Promise.all([iterateAllMessages(subprocess), iterateAllMessages(subprocess)]), [foobarArray, foobarArray], );
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, foobarArray);});
const iterateAndBreak = async subprocess => { // eslint-disable-next-line no-unreachable-loop for await (const message of subprocess.getEachMessage()) { assert.equal(message, foobarString); break; }};
test('Breaking in subprocess.getEachMessage() disconnects', async () => { const subprocess = execa('ipc-iterate-send.js', {ipc: true}); await iterateAndBreak(subprocess); const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
test('Breaking from subprocess.getEachMessage() awaits the subprocess', async () => { const subprocess = execa('ipc-send-wait-print.js', {ipc: true}); await iterateAndBreak(subprocess);
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]); assert.equal(stdout, '.');});
test('Breaking from exports.getEachMessage() disconnects', async () => { const subprocess = execa('ipc-iterate-break.js', {ipc: true});
assert.equal(await subprocess.getOneMessage(), foobarString); await subprocess.sendMessage(foobarString); const ipcError = await assertRejects(subprocess.getOneMessage()); assert.ok(ipcError.message.includes('subprocess.getOneMessage() could not complete'));
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
const iterateAndThrow = async (subprocess, cause) => { // eslint-disable-next-line no-unreachable-loop for await (const message of subprocess.getEachMessage()) { assert.equal(message, foobarString); throw cause; }};
test('Throwing from subprocess.getEachMessage() disconnects', async () => { const subprocess = execa('ipc-iterate-send.js', {ipc: true});
const cause = new Error(foobarString); assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
test('Throwing from subprocess.getEachMessage() awaits the subprocess', async () => { const subprocess = execa('ipc-send-wait-print.js', {ipc: true}); const cause = new Error(foobarString); assert.equal(await assertRejects(iterateAndThrow(subprocess, cause)), cause);
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]); assert.equal(stdout, '.');});
test('Throwing from exports.getEachMessage() disconnects', async () => { const subprocess = execa('ipc-iterate-throw.js', {ipc: true});
assert.equal(await subprocess.getOneMessage(), foobarString); await subprocess.sendMessage(foobarString); const ipcError = await assertRejects(subprocess.getOneMessage()); assert.ok(ipcError.message.includes('subprocess.getOneMessage() could not complete'));
const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(message.includes(`Error: ${foobarString}`)); assert.deepEqual(ipcOutput, [foobarString]);});
test('Can send many messages at once with exports.getEachMessage()', async () => { const subprocess = execa('ipc-iterate.js', {ipc: true}); await Promise.all(Array.from({length: PARALLEL_COUNT}, (_, index) => subprocess.sendMessage(index))); await subprocess.sendMessage(foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, Array.from({length: PARALLEL_COUNT}, (_, index) => index));});
test('subprocess.getOneMessage() can be called multiple times in a row, buffer true', async () => { const subprocess = execa('ipc-print-many-each.js', [`${PARALLEL_COUNT}`], {ipc: true}); const indexes = Array.from({length: PARALLEL_COUNT}, (_, index) => `${index}`); await Promise.all(indexes.map(index => subprocess.sendMessage(index)));
const {stdout} = await subprocess; const expectedOutput = indexes.join('\n'); assert.equal(stdout, expectedOutput);});
test('Disconnecting in the current process stops exports.getEachMessage()', async () => { const subprocess = execa('ipc-iterate-print.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), foobarString); await subprocess.sendMessage('.'); subprocess.nodeChildProcess.disconnect();
const {stdout} = await subprocess; assert.equal(stdout, '.');});
test('Disconnecting in the subprocess stops subprocess.getEachMessage()', async () => { const subprocess = execa('ipc-send-disconnect.js', {ipc: true}); for await (const message of subprocess.getEachMessage()) { assert.equal(message, foobarString); }
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
test('Exiting the subprocess stops subprocess.getEachMessage()', async () => { const subprocess = execa('ipc-send.js', {ipc: true}); for await (const message of subprocess.getEachMessage()) { assert.equal(message, foobarString); }
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
const testCleanupListeners = async buffer => { const subprocess = execa('ipc-send.js', {ipc: true, buffer});
assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1); assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);
const promise = iterateAllMessages(subprocess); assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1); assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1); assert.deepEqual(await promise, [foobarString]);
assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 0); assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 0);};
test('Cleans up subprocess.getEachMessage() listeners, buffer false', () => testCleanupListeners(false));test('Cleans up subprocess.getEachMessage() listeners, buffer true', () => testCleanupListeners(true));
const sendContinuousMessages = async subprocess => { while (subprocess.nodeChildProcess.connected) { for (let index = 0; index < 10; index += 1) { subprocess.nodeChildProcess.emit('message', foobarString); }
// eslint-disable-next-line no-await-in-loop await scheduler.yield(); }};
test('Handles buffered messages when disconnecting', async () => { const subprocess = execa('ipc-send-fail.js', {ipc: true, buffer: false});
const promise = subprocess.getOneMessage(); subprocess.nodeChildProcess.emit('message', foobarString); assert.equal(await promise, foobarString); sendContinuousMessages(subprocess);
const {exitCode, isTerminated, ipcOutput} = await assertRejects(iterateAllMessages(subprocess)); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.deepEqual(ipcOutput, []);});
/* eslint-enable node-test/no-conditional-assertion */