get-one.js
This file
Path
test/ipc/get-one.js
162 lines · 7.7 KiB · JavaScript
Last changed by
Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163import assert from 'node:assert/strict';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 {isAlwaysTrue} from '../helpers/ipc.js';import {PARALLEL_COUNT} from '../helpers/parallel.js';import {assertMaxListeners} from '../helpers/listeners.js';
setFixtureDirectory();
test('subprocess.getOneMessage() can filter messages', async () => { const subprocess = execa('ipc-send-twice.js', {ipc: true}); const message = await subprocess.getOneMessage({filter: message => message === foobarArray[1]}); assert.equal(message, foobarArray[1]);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, foobarArray);});
test('subprocess.getOneMessage() filter not matching any message rejects on exit', async () => { const subprocess = execa('ipc-send.js', {ipc: true}); const error = await assertRejects(subprocess.getOneMessage({filter: () => false})); assert.equal(error.message, 'subprocess.getOneMessage() could not complete: the subprocess exited or disconnected.'); await subprocess;});
test('exports.getOneMessage() can filter messages', async () => { const subprocess = execa('ipc-echo-filter.js', {ipc: true}); await subprocess.sendMessage(foobarArray[0]); await subprocess.sendMessage(foobarArray[1]);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarArray[1]]);});
test('Throwing from subprocess.getOneMessage() filter disconnects', async () => { const subprocess = execa('ipc-send-get.js', {ipc: true}); const error = new Error(foobarString); assert.equal(await assertRejects(subprocess.getOneMessage({ filter() { throw error; }, })), error);
const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(message.includes('Error: getOneMessage() could not complete')); assert.deepEqual(ipcOutput, [foobarString]);});
test('Throwing from exports.getOneMessage() filter disconnects', async () => { const subprocess = execa('ipc-get-filter-throw.js', {ipcInput: 0}); await assertRejects(subprocess.getOneMessage(), { message: /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, []);});
test('Can retrieve initial IPC messages under heavy load', async () => { await Promise.all(Array.from({length: PARALLEL_COUNT}, async (_, index) => { const subprocess = execa('ipc-send-argv.js', [`${index}`], {ipc: true, buffer: false}); assert.equal(await subprocess.getOneMessage(), `${index}`); await subprocess; }));});
// The listeners are added to an internal emitter, so users cannot increase its maximum number of listenersconst testConcurrentParent = async filter => { const checkMaxListeners = assertMaxListeners(); const subprocess = execa('ipc-send.js', {ipc: true}); const messages = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => subprocess.getOneMessage({filter}))); assert.deepEqual(messages, Array.from({length: PARALLEL_COUNT}, () => foobarString)); await subprocess; checkMaxListeners();};
test('subprocess.getOneMessage() can be called many times at the same time', () => testConcurrentParent(undefined));test('subprocess.getOneMessage() can be called many times at the same time, filter', () => testConcurrentParent(isAlwaysTrue));
test('exports.getOneMessage() can be called many times at the same time', async () => { const subprocess = execa('ipc-get-concurrent.js', [`${PARALLEL_COUNT}`], {ipc: true}); await subprocess.sendMessage(foobarString, {strict: true}); const {stdout, stderr} = await subprocess; assert.equal(stdout, `${PARALLEL_COUNT}`); assert.equal(stderr, '');});
const testTwice = async (buffer, filter) => { const subprocess = execa('ipc-send.js', {ipc: true, buffer}); assert.deepEqual( await Promise.all([subprocess.getOneMessage({filter}), subprocess.getOneMessage({filter})]), [foobarString, foobarString], ); await subprocess;};
test('subprocess.getOneMessage() can be called twice at the same time, buffer false', () => testTwice(false, undefined));test('subprocess.getOneMessage() can be called twice at the same time, buffer true', () => testTwice(true, undefined));test('subprocess.getOneMessage() can be called twice at the same time, buffer false, filter', () => testTwice(false, isAlwaysTrue));test('subprocess.getOneMessage() can be called twice at the same time, buffer true, filter', () => testTwice(true, isAlwaysTrue));
const testCleanupListeners = async (buffer, filter) => { 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 = subprocess.getOneMessage({filter}); assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1); assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);
assert.equal(await promise, foobarString); await subprocess;
assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 0); assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 0);};
test('Cleans up subprocess.getOneMessage() listeners, buffer false', () => testCleanupListeners(false, undefined));test('Cleans up subprocess.getOneMessage() listeners, buffer true', () => testCleanupListeners(true, undefined));test('Cleans up subprocess.getOneMessage() listeners, buffer false, filter', () => testCleanupListeners(false, isAlwaysTrue));test('Cleans up subprocess.getOneMessage() listeners, buffer true, filter', () => testCleanupListeners(true, isAlwaysTrue));
const testParentDisconnect = async (buffer, filter) => { const subprocess = execa('ipc-get-send-get.js', [`${filter}`], {ipc: true, buffer}); await subprocess.sendMessage(foobarString); assert.equal(await subprocess.getOneMessage(), foobarString);
subprocess.nodeChildProcess.disconnect();
const {exitCode, isTerminated, message} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); if (buffer) { assert.ok(message.includes('Error: getOneMessage() could not complete')); }};
test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer false', () => testParentDisconnect(false, false));test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer true', () => testParentDisconnect(true, false));test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer false, filter', () => testParentDisconnect(false, true));test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer true, filter', () => testParentDisconnect(true, false));
const testSubprocessDisconnect = async (buffer, filter) => { const subprocess = execa('empty.js', {ipc: true, buffer}); const {message} = await assertRejects(subprocess.getOneMessage({filter})); assert.ok(message.includes('subprocess.getOneMessage() could not complete')); await subprocess;};
test('Subprocess exit interrupts subprocess.getOneMessage(), buffer false', () => testSubprocessDisconnect(false, undefined));test('Subprocess exit interrupts subprocess.getOneMessage(), buffer true', () => testSubprocessDisconnect(true, undefined));test('Subprocess exit interrupts subprocess.getOneMessage(), buffer false, filter', () => testSubprocessDisconnect(false, isAlwaysTrue));test('Subprocess exit interrupts subprocess.getOneMessage(), buffer true, filter', () => testSubprocessDisconnect(true, isAlwaysTrue));