Skip to content
Myr

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 file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import 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 back
4import {execa} from '../../index.js';
5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 6why, one step back
6import {foobarString, foobarArray} from '../helpers/input.js';

Update dev dependencies (#1257)Sindre Sorhusbefore adoption5944b2d

line 7why, one step back
7import {isAlwaysTrue} from '../helpers/ipc.js';

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

line 8why, one step back
8import {PARALLEL_COUNT} from '../helpers/parallel.js';

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 9why, one step back
9import {assertMaxListeners} from '../helpers/listeners.js';

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 10–12why, one step back
10
11setFixtureDirectory();12

TweaksSindre Sorhusbefore adoption698a1b8

line 13why, one step back
13test('subprocess.getOneMessage() can filter messages', async () => {

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 14why, one step back
14 const subprocess = execa('ipc-send-twice.js', {ipc: true});

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 15why, one step back
15 const message = await subprocess.getOneMessage({filter: message => message === foobarArray[1]});

TweaksSindre Sorhusbefore adoption698a1b8

line 16why, one step back
16 assert.equal(message, foobarArray[1]);

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 17–18why, one step back
17
18 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 19why, one step back
19 assert.deepEqual(ipcOutput, foobarArray);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 20why, one step back
20});

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 21why, one step back
21

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 22–28why, one step back
22test('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});28

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29test('exports.getOneMessage() can filter messages', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 30why, one step back
30 const subprocess = execa('ipc-echo-filter.js', {ipc: true});

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

lines 31–34why, one step back
31 await subprocess.sendMessage(foobarArray[0]);
32 await subprocess.sendMessage(foobarArray[1]);3334 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 35why, one step back
35 assert.deepEqual(ipcOutput, [foobarArray[1]]);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 36why, one step back
36});

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 37why, one step back
37

TweaksSindre Sorhusbefore adoption698a1b8

line 38why, one step back
38test('Throwing from subprocess.getOneMessage() filter disconnects', async () => {

Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998

lines 39–40why, one step back
39 const subprocess = execa('ipc-send-get.js', {ipc: true});
40 const error = new Error(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 41why, one step back
41 assert.equal(await assertRejects(subprocess.getOneMessage({

Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998

lines 42–46why, one step back
42 filter() {
43 throw error;44 },45 })), error);46

TweaksSindre Sorhusbefore adoption698a1b8

lines 47–51why, one step back
47 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 back
52});
53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('Throwing from exports.getOneMessage() filter disconnects', async () => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 55why, one step back
55 const subprocess = execa('ipc-get-filter-throw.js', {ipcInput: 0});

TweaksSindre Sorhusbefore adoption698a1b8

line 56why, one step back
56 await assertRejects(subprocess.getOneMessage(), {

Handle exceptions from the `filter` option of `getOneMessage()` (#1091)ehmickybefore adoption1bda998

lines 57–59why, one step back
57 message: /subprocess.getOneMessage\(\) could not complete/,
58 });59

TweaksSindre Sorhusbefore adoption698a1b8

lines 60–64why, one step back
60 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 back
65});
66

TweaksSindre Sorhusbefore adoption698a1b8

line 67why, one step back
67test('Can retrieve initial IPC messages under heavy load', async () => {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 68–69why, one step back
68 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 back
70 assert.equal(await subprocess.getOneMessage(), `${index}`);

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 71–72why, one step back
71 await subprocess;
72 }));

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 73–93why, one step back
73});
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 back
94});

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

line 95why, one step back
95

TweaksSindre Sorhusbefore adoption698a1b8

line 96why, one step back
96const testTwice = async (buffer, filter) => {

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 97why, one step back
97 const subprocess = execa('ipc-send.js', {ipc: true, buffer});

TweaksSindre Sorhusbefore adoption698a1b8

line 98why, one step back
98 assert.deepEqual(

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 99why, one step back
99 await Promise.all([subprocess.getOneMessage({filter}), subprocess.getOneMessage({filter})]),

Add more IPC-related tests (#1061)ehmickybefore adoption55a33c2

lines 100–101why, one step back
100 [foobarString, foobarString],
101 );

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 102why, one step back
102 await subprocess;

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 103why, one step back
103};

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 104why, one step back
104

TweaksSindre Sorhusbefore adoption698a1b8

lines 105–108why, one step back
105test('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 back
109

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110const testCleanupListeners = async (buffer, filter) => {

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 111why, one step back
111 const subprocess = execa('ipc-send.js', {ipc: true, buffer});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 112why, one step back
112

TweaksSindre Sorhusbefore adoption698a1b8

lines 113–114why, one step back
113 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 back
115

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 116why, one step back
116 const promise = subprocess.getOneMessage({filter});

TweaksSindre Sorhusbefore adoption698a1b8

lines 117–118why, one step back
117 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 back
119

TweaksSindre Sorhusbefore adoption698a1b8

line 120why, one step back
120 assert.equal(await promise, foobarString);

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 121–122why, one step back
121 await subprocess;
122

TweaksSindre Sorhusbefore adoption698a1b8

lines 123–124why, one step back
123 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 back
125};

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 126why, one step back
126

TweaksSindre Sorhusbefore adoption698a1b8

lines 127–130why, one step back
127test('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 back
131

TweaksSindre Sorhusbefore adoption698a1b8

line 132why, one step back
132const testParentDisconnect = async (buffer, filter) => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 133why, one step back
133 const subprocess = execa('ipc-get-send-get.js', [`${filter}`], {ipc: true, buffer});

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 134why, one step back
134 await subprocess.sendMessage(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

line 135why, one step back
135 assert.equal(await subprocess.getOneMessage(), foobarString);

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 136why, one step back
136

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 137why, one step back
137 subprocess.nodeChildProcess.disconnect();

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 138why, one step back
138

TweaksSindre Sorhusbefore adoption698a1b8

lines 139–141why, one step back
139 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 back
142 if (buffer) {

TweaksSindre Sorhusbefore adoption698a1b8

line 143why, one step back
143 assert.ok(message.includes('Error: getOneMessage() could not complete'));

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 144–146why, one step back
144 }
145};146

TweaksSindre Sorhusbefore adoption698a1b8

lines 147–150why, one step back
147test('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 back
151

TweaksSindre Sorhusbefore adoption698a1b8

line 152why, one step back
152const testSubprocessDisconnect = async (buffer, filter) => {

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 153why, one step back
153 const subprocess = execa('empty.js', {ipc: true, buffer});

TweaksSindre Sorhusbefore adoption698a1b8

lines 154–155why, one step back
154 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 back
156 await subprocess;
157};158

TweaksSindre Sorhusbefore adoption698a1b8

lines 159–162why, one step back
159test('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));