Add more IPC-related tests (#1061)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent ecc00ed · Browse files · 5 files·+58 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+58 added−1 removed
- tests:+56 added−0 removed
- other:+2 added−1 removed
Provenance
- Commit
55a33c2- Tree
44ed5a1- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ecc00ed
5 files changed+58 added−1 removed
modified:lib/ipc/validation.js+2 added-1 removed
3 unmodified lines45678891011121334 unmodified lines3 unmodified lines throw new Error(`${getNamespaceName(isSubprocess)}${methodName}() can only be used if the \`ipc\` option is \`true\`.`); }};
// Better error message when one process does not send/receive messages once the other process has disconnected// Better error message when one process does not send/receive messages once the other process has disconnected.// This also makes it clear that any buffered messages are lost once either process has disconnected.export const validateConnection = (methodName, isSubprocess, isConnected) => { if (!isConnected) { throw new Error(`${getNamespaceName(isSubprocess)}${methodName}() cannot be used: the ${getOtherProcessName(isSubprocess)} has already exited or disconnected.`); }34 unmodified linesadded:test/fixtures/ipc-send-argv.js+6 added
123456#!/usr/bin/env nodeimport {argv} from 'node:process';import {sendMessage} from '../../index.js';
const message = argv[2];await sendMessage(message);added:test/fixtures/ipc-send-print.js+10 added
12345678910#!/usr/bin/env nodeimport process from 'node:process';import {sendMessage, getOneMessage} from '../../index.js';import {foobarString} from '../helpers/input.js';
await sendMessage(foobarString);
process.stdout.write('.');
await getOneMessage();modified:test/ipc/get-each.js+9 added
46 unmodified lines474849505152535455565758596061626366 unmodified lines46 unmodified lines const {stdout} = await subprocess; t.is(stdout, '..');});
test('subprocess.getEachMessage() can be called twice at the same time', async t => { const subprocess = execa('ipc-send-twice.js', {ipc: true}); t.deepEqual( await Promise.all([iterateAllMessages(subprocess), iterateAllMessages(subprocess)]), [foobarArray, foobarArray], ); await subprocess;});
const HIGH_CONCURRENCY_COUNT = 100;
test('Can send many messages at once with exports.getEachMessage()', async t => { const subprocess = execa('ipc-iterate.js', {ipc: true});66 unmodified linesmodified:test/ipc/get-one.js+31 added
1+import {once} from 'node:events';2 import test from 'ava';3 import {execa} from '../../index.js';4 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5 import {foobarString} from '../helpers/input.js';15 unmodified lines21 t.is(await subprocess.getOneMessage(), foobarString);22 await subprocess;23 });24 25+test('Buffers initial message to current process', async t => {26+ const subprocess = execa('ipc-send-print.js', {ipc: true});27+ const [chunk] = await once(subprocess.stdout, 'data');28+ t.is(chunk.toString(), '.');29+ t.is(await subprocess.getOneMessage(), foobarString);30+ await subprocess.sendMessage('.');31+ await subprocess;32+});33+34+const HIGH_CONCURRENCY_COUNT = 100;35+36+test.serial('Can retrieve initial IPC messages under heavy load', async t => {37+ await Promise.all(38+ Array.from({length: HIGH_CONCURRENCY_COUNT}, async (_, index) => {39+ const subprocess = execa('ipc-send-argv.js', [`${index}`], {ipc: true});40+ t.is(await subprocess.getOneMessage(), `${index}`);41+ await subprocess;42+ }),43+ );44+});45+46+test('subprocess.getOneMessage() can be called twice at the same time', async t => {47+ const subprocess = execa('ipc-send.js', {ipc: true});48+ t.deepEqual(49+ await Promise.all([subprocess.getOneMessage(), subprocess.getOneMessage()]),50+ [foobarString, foobarString],51+ );52+ await subprocess;53+});54+55 test('Cleans up subprocess.getOneMessage() listeners', async t => {56 const subprocess = execa('ipc-send.js', {ipc: true});57 58 t.is(subprocess.listenerCount('message'), 0);29 unmodified lines