Handle exceptions from the `filter` option of `getOneMessage()` (#1091)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent e6b012a · Browse files · 4 files·+50 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
4 files+50 added−1 removed
- tests:+46 added−0 removed
- other:+4 added−1 removed
Provenance
- Commit
1bda998- Tree
2b12f25- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
e6b012a
4 files changed+50 added−1 removed
modified:lib/ipc/get-one.js+4 added-1 removed
122345621 unmodified lines282930313233343536373819 unmodified linesimport {once, on} from 'node:events';import {validateIpcMethod, throwOnEarlyDisconnect} from './validation.js';import {validateIpcMethod, throwOnEarlyDisconnect, disconnect} from './validation.js';import {getIpcEmitter, isConnected} from './forward.js';import {addReference, removeReference} from './reference.js';
// Like `[sub]process.once('message')` but promise-based21 unmodified lines return await Promise.race([ getMessage(ipcEmitter, filter, controller), throwOnDisconnect(ipcEmitter, isSubprocess, controller), ]); } catch (error) { disconnect(anyProcess); throw error; } finally { controller.abort(); removeReference(channel); }19 unmodified linesadded:test/fixtures/ipc-get-filter-throw.js+9 added
123456789#!/usr/bin/env nodeimport {getOneMessage} from '../../index.js';import {foobarString} from '../helpers/input.js';
await getOneMessage({ filter() { throw new Error(foobarString); },});added:test/fixtures/ipc-send-get.js+8 added
12345678#!/usr/bin/env nodeimport {sendMessage, getOneMessage} from '../../index.js';import {foobarString} from '../helpers/input.js';
await Promise.all([ getOneMessage(), sendMessage(foobarString),]);modified:test/ipc/get-one.js+29 added
55 unmodified lines56 const {ipcOutput} = await subprocess;57 t.deepEqual(ipcOutput, [foobarArray[1]]);58 });59 60+test('Throwing from subprocess.getOneMessage() filter disconnects', async t => {61+ const subprocess = execa('ipc-send-get.js', {ipc: true});62+ const error = new Error(foobarString);63+ t.is(await t.throwsAsync(subprocess.getOneMessage({64+ filter() {65+ throw error;66+ },67+ })), error);68+69+ const {exitCode, isTerminated, message, ipcOutput} = await t.throwsAsync(subprocess);70+ t.is(exitCode, 1);71+ t.false(isTerminated);72+ t.true(message.includes('Error: getOneMessage() could not complete'));73+ t.deepEqual(ipcOutput, [foobarString]);74+});75+76+test('Throwing from exports.getOneMessage() filter disconnects', async t => {77+ const subprocess = execa('ipc-get-filter-throw.js', {ipc: true, ipcInput: 0});78+ await t.throwsAsync(subprocess.getOneMessage(), {79+ message: /subprocess.getOneMessage\(\) could not complete/,80+ });81+82+ const {exitCode, isTerminated, message, ipcOutput} = await t.throwsAsync(subprocess);83+ t.is(exitCode, 1);84+ t.false(isTerminated);85+ t.true(message.includes(`Error: ${foobarString}`));86+ t.deepEqual(ipcOutput, []);87+});88+89 test.serial('Can retrieve initial IPC messages under heavy load', async t => {90 await Promise.all(91 Array.from({length: PARALLEL_COUNT}, async (_, index) => {92 const subprocess = execa('ipc-send-argv.js', [`${index}`], {ipc: true, buffer: false});73 unmodified lines