Add tests for I/O errors during `sendMessage()` (#1095)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent d8bdaf0 · Browse files · 5 files·+93 added−36 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+93 added−36 removed
- tests:+55 added−0 removed
- other:+38 added−36 removed
Provenance
- Commit
c6c66da- Tree
bf093af- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
d8bdaf0
5 files changed+93 added−36 removed
modified:lib/ipc/methods.js+19 added-27 removed
1223455 unmodified lines11121314161718192021222324252627282930313233343536373839404115161718192021222324252627282930313233import process from 'node:process';import {promisify} from 'node:util';import {sendMessage} from './send.js';import {getOneMessage} from './get-one.js';import {getEachMessage} from './get-each.js';
5 unmodified lines// Get promise-based IPC in the subprocessexport const getIpcExport = () => getIpcMethods(process, true, process.channel !== undefined);
// Retrieve the `ipc` shared by both the current process and the subprocessconst getIpcMethods = (anyProcess, isSubprocess, ipc) => { const anyProcessSend = anyProcess.send === undefined ? undefined : promisify(anyProcess.send.bind(anyProcess)); const {channel} = anyProcess; return { sendMessage: sendMessage.bind(undefined, { anyProcess, anyProcessSend, isSubprocess, ipc, }), getOneMessage: getOneMessage.bind(undefined, { anyProcess, channel, isSubprocess, ipc, }), getEachMessage: getEachMessage.bind(undefined, { anyProcess, channel, isSubprocess, ipc, }), };};const getIpcMethods = (anyProcess, isSubprocess, ipc) => ({ sendMessage: sendMessage.bind(undefined, { anyProcess, isSubprocess, ipc, }), getOneMessage: getOneMessage.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc, }), getEachMessage: getEachMessage.bind(undefined, { anyProcess, channel: anyProcess.channel, isSubprocess, ipc, }),});modified:lib/ipc/send.js+19 added-9 removed
123454 unmodified lines101112131314151617181920212122232425262223242925262728322930313233343536373839404142434445464748495051import {promisify} from 'node:util';import { validateIpcMethod, handleEpipeError, handleSerializationError,4 unmodified lines// Like `[sub]process.send()` but promise-based.// We do not `await subprocess` during `.sendMessage()` nor `.getOneMessage()` since those methods are transient.// Users would still need to `await subprocess` after the method is done.// Also, this would prevent `unhandledRejection` event from being emitted, making it silent.export const sendMessage = ({anyProcess, anyProcessSend, isSubprocess, ipc}, message) => {export const sendMessage = ({anyProcess, isSubprocess, ipc}, message) => { validateIpcMethod({ methodName: 'sendMessage', isSubprocess, ipc, isConnected: anyProcess.connected, });
return sendMessageAsync({ anyProcess, anyProcessSend, isSubprocess, message, }); return sendMessageAsync({anyProcess, isSubprocess, message});};
const sendMessageAsync = async ({anyProcess, anyProcessSend, isSubprocess, message}) => {const sendMessageAsync = async ({anyProcess, isSubprocess, message}) => { const outgoingMessagesState = startSendMessage(anyProcess); const sendMethod = getSendMethod(anyProcess); try { await anyProcessSend(message); await sendMethod(message); } catch (error) { disconnect(anyProcess); handleEpipeError(error, isSubprocess); handleSerializationError(error, isSubprocess, message); throw error; } finally { endSendMessage(outgoingMessagesState); }};
// [sub]process.send() promisified, memoizedconst getSendMethod = anyProcess => { if (PROCESS_SEND_METHODS.has(anyProcess)) { return PROCESS_SEND_METHODS.get(anyProcess); }
const sendMethod = promisify(anyProcess.send.bind(anyProcess)); PROCESS_SEND_METHODS.set(anyProcess, sendMethod); return sendMethod;};
const PROCESS_SEND_METHODS = new WeakMap();added:test/fixtures/ipc-send-io-error.js+7 added
1234567#!/usr/bin/env nodeimport process from 'node:process';import {sendMessage} from '../../index.js';import {mockSendIoError} from '../helpers/ipc.js';
mockSendIoError(process);await sendMessage('.');modified:test/helpers/ipc.js+13 added
12345618 unmodified lines25262728293031323334353637383940import {getEachMessage} from '../../index.js';import {foobarString} from './input.js';
// @todo: replace with Array.fromAsync(subprocess.getEachMessage()) after dropping support for Node <22.0.0export const iterateAllMessages = async subprocess => { const messages = [];18 unmodified lines
export const subprocessGetOne = (subprocess, options) => subprocess.getOneMessage(options);
export const alwaysPass = () => true;
// `process.send()` can fail due to I/O errors.// However, I/O errors are seldom and hard to trigger predictably.// So we mock them.export const mockSendIoError = anyProcess => { const error = new Error(foobarString); anyProcess.send = () => { throw error; };
return error;};modified:test/ipc/send.js+35 added
1 unmodified line2 import {execa} from '../../index.js';3 import {setFixtureDirectory} from '../helpers/fixtures-directory.js';4 import {foobarString} from '../helpers/input.js';5 import {PARALLEL_COUNT} from '../helpers/parallel.js';6+import {mockSendIoError} from '../helpers/ipc.js';7 8 setFixtureDirectory();9 10 test('Can exchange IPC messages', async t => {98 unmodified lines109 test.serial('Can send messages while the subprocess is closing', async t => {110 const {message} = await findEpipeError(t);111 t.is(message, 'subprocess.sendMessage() cannot be used: the subprocess is disconnecting.');112 });113+114+test('subprocess.sendMessage() handles I/O errors', async t => {115+ const subprocess = execa('ipc-echo.js', {ipc: true});116+ const error = mockSendIoError(subprocess);117+ t.is(await t.throwsAsync(subprocess.sendMessage('.')), error);118+119+ const {exitCode, isTerminated, message, ipcOutput} = await t.throwsAsync(subprocess);120+ t.is(exitCode, 1);121+ t.false(isTerminated);122+ t.true(message.includes('Error: getOneMessage() cannot be used: the parent process has already exited or disconnected'));123+ t.deepEqual(ipcOutput, []);124+});125+126+test('Does not hold message events on I/O errors', async t => {127+ const subprocess = execa('ipc-echo.js', {ipc: true});128+ const error = mockSendIoError(subprocess);129+ const promise = subprocess.sendMessage('.');130+ subprocess.emit('message', '.');131+ t.is(await t.throwsAsync(promise), error);132+133+ const {exitCode, isTerminated, message, ipcOutput} = await t.throwsAsync(subprocess);134+ t.is(exitCode, 1);135+ t.false(isTerminated);136+ t.true(message.includes('Error: getOneMessage() cannot be used: the parent process has already exited or disconnected'));137+ t.deepEqual(ipcOutput, ['.']);138+});139+140+test('exports.sendMessage() handles I/O errors', async t => {141+ const {exitCode, isTerminated, message, ipcOutput} = await t.throwsAsync(execa('ipc-send-io-error.js', {ipc: true}));142+ t.is(exitCode, 1);143+ t.false(isTerminated);144+ t.true(message.includes(`Error: ${foobarString}`));145+ t.deepEqual(ipcOutput, []);146+});