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
TweaksSindre Sorhusbefore adoption698a1b8
lines 1–3why, one step back1import assert from 'node:assert/strict';2import test from 'node:test';3import {assertRejects} from '../helpers/assert.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 4–5why, one step back4import {execa} from '../../index.js';5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 6why, one step back6import {foobarString, foobarArray} from '../helpers/input.js';Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d
line 7why, one step back7import {isAlwaysTrue} from '../helpers/ipc.js';Refactor parallel tests (#1084)ehmickybefore adoption5cf6312
line 8why, one step back8import {PARALLEL_COUNT} from '../helpers/parallel.js';Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
line 9why, one step back9import {assertMaxListeners} from '../helpers/listeners.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 10–12why, one step back1011setFixtureDirectory();12TweaksSindre Sorhusbefore adoption698a1b8
line 13why, one step back13test('subprocess.getOneMessage() can filter messages', async () => {Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 14why, one step back14 const subprocess = execa('ipc-send-twice.js', {ipc: true});Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 15why, one step back15 const message = await subprocess.getOneMessage({filter: message => message === foobarArray[1]});TweaksSindre Sorhusbefore adoption698a1b8
line 16why, one step back16 assert.equal(message, foobarArray[1]);Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 17–18why, one step back1718 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 19why, one step back19 assert.deepEqual(ipcOutput, foobarArray);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 20why, one step back20});Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 21why, one step back21Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 22–28why, one step back22test('subprocess.getOneMessage() filter not matching any message rejects on exit', async () => {23 const subprocess = execa('ipc-send.js', {ipc: true});24 const error = await assertRejects(subprocess.getOneMessage({filter: () => false}));25 assert.equal(error.message, 'subprocess.getOneMessage() could not complete: the subprocess exited or disconnected.');26 await subprocess;27});28TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29test('exports.getOneMessage() can filter messages', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 30why, one step back30 const subprocess = execa('ipc-echo-filter.js', {ipc: true});Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
lines 31–34why, one step back31 await subprocess.sendMessage(foobarArray[0]);32 await subprocess.sendMessage(foobarArray[1]);3334 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35 assert.deepEqual(ipcOutput, [foobarArray[1]]);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 36why, one step back36});Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052
line 37why, one step back37TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38test('Throwing from subprocess.getOneMessage() filter disconnects', async () => {Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
lines 39–40why, one step back39 const subprocess = execa('ipc-send-get.js', {ipc: true});40 const error = new Error(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 41why, one step back41 assert.equal(await assertRejects(subprocess.getOneMessage({Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
lines 42–46why, one step back42 filter() {43 throw error;44 },45 })), error);46TweaksSindre Sorhusbefore adoption698a1b8
lines 47–51why, one step back47 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess);48 assert.equal(exitCode, 1);49 assert.equal(isTerminated, false);50 assert.ok(message.includes('Error: getOneMessage() could not complete'));51 assert.deepEqual(ipcOutput, [foobarString]);Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
lines 52–53why, one step back52});53TweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54test('Throwing from exports.getOneMessage() filter disconnects', async () => {Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 55why, one step back55 const subprocess = execa('ipc-get-filter-throw.js', {ipcInput: 0});TweaksSindre Sorhusbefore adoption698a1b8
line 56why, one step back56 await assertRejects(subprocess.getOneMessage(), {Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
lines 57–59why, one step back57 message: /subprocess.getOneMessage\(\) could not complete/,58 });59TweaksSindre Sorhusbefore adoption698a1b8
lines 60–64why, one step back60 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess);61 assert.equal(exitCode, 1);62 assert.equal(isTerminated, false);63 assert.ok(message.includes(`Error: ${foobarString}`));64 assert.deepEqual(ipcOutput, []);Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998
lines 65–66why, one step back65});66TweaksSindre Sorhusbefore adoption698a1b8
line 67why, one step back67test('Can retrieve initial IPC messages under heavy load', async () => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 68–69why, one step back68 await Promise.all(Array.from({length: PARALLEL_COUNT}, async (_, index) => {69 const subprocess = execa('ipc-send-argv.js', [`${index}`], {ipc: true, buffer: false});TweaksSindre Sorhusbefore adoption698a1b8
line 70why, one step back70 assert.equal(await subprocess.getOneMessage(), `${index}`);Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 71–72why, one step back71 await subprocess;72 }));Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 73–93why, one step back73});7475// The listeners are added to an internal emitter, so users cannot increase its maximum number of listeners76const testConcurrentParent = async filter => {77 const checkMaxListeners = assertMaxListeners();78 const subprocess = execa('ipc-send.js', {ipc: true});79 const messages = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => subprocess.getOneMessage({filter})));80 assert.deepEqual(messages, Array.from({length: PARALLEL_COUNT}, () => foobarString));81 await subprocess;82 checkMaxListeners();83};8485test('subprocess.getOneMessage() can be called many times at the same time', () => testConcurrentParent(undefined));86test('subprocess.getOneMessage() can be called many times at the same time, filter', () => testConcurrentParent(isAlwaysTrue));8788test('exports.getOneMessage() can be called many times at the same time', async () => {89 const subprocess = execa('ipc-get-concurrent.js', [`${PARALLEL_COUNT}`], {ipc: true});90 await subprocess.sendMessage(foobarString, {strict: true});91 const {stdout, stderr} = await subprocess;92 assert.equal(stdout, `${PARALLEL_COUNT}`);93 assert.equal(stderr, '');Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 94why, one step back94});Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
line 95why, one step back95TweaksSindre Sorhusbefore adoption698a1b8
line 96why, one step back96const testTwice = async (buffer, filter) => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 97why, one step back97 const subprocess = execa('ipc-send.js', {ipc: true, buffer});TweaksSindre Sorhusbefore adoption698a1b8
line 98why, one step back98 assert.deepEqual(Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 99why, one step back99 await Promise.all([subprocess.getOneMessage({filter}), subprocess.getOneMessage({filter})]),Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2
lines 100–101why, one step back100 [foobarString, foobarString],101 );Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 102why, one step back102 await subprocess;Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 103why, one step back103};Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 104why, one step back104TweaksSindre Sorhusbefore adoption698a1b8
lines 105–108why, one step back105test('subprocess.getOneMessage() can be called twice at the same time, buffer false', () => testTwice(false, undefined));106test('subprocess.getOneMessage() can be called twice at the same time, buffer true', () => testTwice(true, undefined));107test('subprocess.getOneMessage() can be called twice at the same time, buffer false, filter', () => testTwice(false, isAlwaysTrue));108test('subprocess.getOneMessage() can be called twice at the same time, buffer true, filter', () => testTwice(true, isAlwaysTrue));Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 109why, one step back109TweaksSindre Sorhusbefore adoption698a1b8
line 110why, one step back110const testCleanupListeners = async (buffer, filter) => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 111why, one step back111 const subprocess = execa('ipc-send.js', {ipc: true, buffer});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 112why, one step back112TweaksSindre Sorhusbefore adoption698a1b8
lines 113–114why, one step back113 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1);114 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 115why, one step back115Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 116why, one step back116 const promise = subprocess.getOneMessage({filter});TweaksSindre Sorhusbefore adoption698a1b8
lines 117–118why, one step back117 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 1);118 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 1);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 119why, one step back119TweaksSindre Sorhusbefore adoption698a1b8
line 120why, one step back120 assert.equal(await promise, foobarString);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 121–122why, one step back121 await subprocess;122TweaksSindre Sorhusbefore adoption698a1b8
lines 123–124why, one step back123 assert.equal(subprocess.nodeChildProcess.listenerCount('message'), 0);124 assert.equal(subprocess.nodeChildProcess.listenerCount('disconnect'), 0);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 125why, one step back125};Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 126why, one step back126TweaksSindre Sorhusbefore adoption698a1b8
lines 127–130why, one step back127test('Cleans up subprocess.getOneMessage() listeners, buffer false', () => testCleanupListeners(false, undefined));128test('Cleans up subprocess.getOneMessage() listeners, buffer true', () => testCleanupListeners(true, undefined));129test('Cleans up subprocess.getOneMessage() listeners, buffer false, filter', () => testCleanupListeners(false, isAlwaysTrue));130test('Cleans up subprocess.getOneMessage() listeners, buffer true, filter', () => testCleanupListeners(true, isAlwaysTrue));Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 131why, one step back131TweaksSindre Sorhusbefore adoption698a1b8
line 132why, one step back132const testParentDisconnect = async (buffer, filter) => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 133why, one step back133 const subprocess = execa('ipc-get-send-get.js', [`${filter}`], {ipc: true, buffer});Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 134why, one step back134 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 135why, one step back135 assert.equal(await subprocess.getOneMessage(), foobarString);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 136why, one step back136Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 137why, one step back137 subprocess.nodeChildProcess.disconnect();Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 138why, one step back138TweaksSindre Sorhusbefore adoption698a1b8
lines 139–141why, one step back139 const {exitCode, isTerminated, message} = await assertRejects(subprocess);140 assert.equal(exitCode, 1);141 assert.equal(isTerminated, false);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 142why, one step back142 if (buffer) {TweaksSindre Sorhusbefore adoption698a1b8
line 143why, one step back143 assert.ok(message.includes('Error: getOneMessage() could not complete'));Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 144–146why, one step back144 }145};146TweaksSindre Sorhusbefore adoption698a1b8
lines 147–150why, one step back147test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer false', () => testParentDisconnect(false, false));148test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer true', () => testParentDisconnect(true, false));149test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer false, filter', () => testParentDisconnect(false, true));150test('subprocess.disconnect() interrupts exports.getOneMessage(), buffer true, filter', () => testParentDisconnect(true, false));Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 151why, one step back151TweaksSindre Sorhusbefore adoption698a1b8
line 152why, one step back152const testSubprocessDisconnect = async (buffer, filter) => {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 153why, one step back153 const subprocess = execa('empty.js', {ipc: true, buffer});TweaksSindre Sorhusbefore adoption698a1b8
lines 154–155why, one step back154 const {message} = await assertRejects(subprocess.getOneMessage({filter}));155 assert.ok(message.includes('subprocess.getOneMessage() could not complete'));Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 156–158why, one step back156 await subprocess;157};158TweaksSindre Sorhusbefore adoption698a1b8
lines 159–162why, one step back159test('Subprocess exit interrupts subprocess.getOneMessage(), buffer false', () => testSubprocessDisconnect(false, undefined));160test('Subprocess exit interrupts subprocess.getOneMessage(), buffer true', () => testSubprocessDisconnect(true, undefined));161test('Subprocess exit interrupts subprocess.getOneMessage(), buffer false, filter', () => testSubprocessDisconnect(false, isAlwaysTrue));162test('Subprocess exit interrupts subprocess.getOneMessage(), buffer true, filter', () => testSubprocessDisconnect(true, isAlwaysTrue));