Skip to content
Myr

send.js

This file

Path

test/ipc/send.js

161 lines · 6.3 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

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

Refactor parallel tests (#1084)ehmickybefore adoption5cf6312

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

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

TweaksSindre Sorhusbefore adoption698a1b8

line 12why, one step back
12test('Can exchange IPC messages', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

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

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 16why, one step back
16 await subprocess;

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 17why, one step back
17});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 18why, one step back
18

TweaksSindre Sorhusbefore adoption698a1b8

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

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

lines 20–22why, one step back
20 await Promise.all(Array.from({length: PARALLEL_COUNT}, async (_, index) => {
21 const subprocess = execa('ipc-echo.js', {ipc: true});22 await subprocess.sendMessage(index);

TweaksSindre Sorhusbefore adoption698a1b8

line 23why, one step back
23 assert.equal(await subprocess.getOneMessage(), index);

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

lines 24–25why, one step back
24 await subprocess;
25 }));

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 26why, one step back
26});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 27why, one step back
27

TweaksSindre Sorhusbefore adoption698a1b8

line 28why, one step back
28test('The "serialization" option defaults to "advanced"', async () => {

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 30–31why, one step back
30 await subprocess.sendMessage([0n]);
31 const message = await subprocess.getOneMessage();

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32 assert.equal(message[0], 0n);

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 33why, one step back
33 await subprocess;

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 34why, one step back
34});

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 35why, one step back
35

TweaksSindre Sorhusbefore adoption698a1b8

line 36why, one step back
36test('Can use "serialization: json" option', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 37–38why, one step back
37 const subprocess = execa('ipc-echo.js', {ipc: true, serialization: 'json'});
38 const date = new Date();

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 39why, one step back
39 await subprocess.sendMessage(date);

TweaksSindre Sorhusbefore adoption698a1b8

line 40why, one step back
40 assert.equal(await subprocess.getOneMessage(), date.toJSON());

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 41why, one step back
41 await subprocess;

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 42why, one step back
42});

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

line 43why, one step back
43

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 44–53why, one step back
44const testNullPrototypeMessage = async serialization => {
45 const subprocess = execa('ipc-echo.js', {ipc: true, serialization});46 await subprocess.sendMessage({__proto__: null, foo: foobarString});47 assert.deepEqual({...await subprocess.getOneMessage()}, {foo: foobarString});48 await subprocess;49};5051test('Can send null-prototype objects', () => testNullPrototypeMessage('advanced'));52test('Can send null-prototype objects, serialization: "json"', () => testNullPrototypeMessage('json'));53

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54test('Validates JSON payload with serialization: "json"', async () => {

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 56–57why, one step back
56 await assertRejects(subprocess.sendMessage([0n]), {message: /serialize a BigInt/});
57 await assertRejects(subprocess);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 58why, one step back
58});

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 59why, one step back
59

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

lines 60–61why, one step back
60const BIG_PAYLOAD_SIZE = '.'.repeat(1e6);
61

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62test('Handles backpressure', async () => {

Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68

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

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 64why, one step back
64 await subprocess.sendMessage(BIG_PAYLOAD_SIZE);

TweaksSindre Sorhusbefore adoption698a1b8

lines 65–66why, one step back
65 assert.ok(subprocess.nodeChildProcess.send(foobarString));
66 assert.equal(await subprocess.getOneMessage(), BIG_PAYLOAD_SIZE);

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 67why, one step back
67 const {ipcOutput} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 68why, one step back
68 assert.deepEqual(ipcOutput, [BIG_PAYLOAD_SIZE]);

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 69why, one step back
69});

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

line 70why, one step back
70

TweaksSindre Sorhusbefore adoption698a1b8

line 71why, one step back
71test('Disconnects IPC on exports.sendMessage() error', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

lines 72–73why, one step back
72 const subprocess = execa('ipc-get-send-get.js', ['false'], {ipc: true});
73 await subprocess.sendMessage(foobarString);

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6

line 75why, one step back
75

TweaksSindre Sorhusbefore adoption698a1b8

lines 76–78why, one step back
76 const {message, cause} = await assertRejects(subprocess.sendMessage(0n));
77 assert.equal(message, 'subprocess.sendMessage()\'s argument type is invalid: the message cannot be serialized: 0.');78 assert.ok(cause.message.includes('The "message" argument must be one of type string'));

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

line 79why, one step back
79

TweaksSindre Sorhusbefore adoption698a1b8

lines 80–83why, one step back
80 const {exitCode, isTerminated, stderr} = await assertRejects(subprocess);
81 assert.equal(exitCode, 1);82 assert.equal(isTerminated, false);83 assert.ok(stderr.includes('Error: getOneMessage() could not complete'));

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 84why, one step back
84});

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

line 85why, one step back
85

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86test('Disconnects IPC on subprocess.sendMessage() error', async () => {

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 88–89why, one step back
88 const ipcError = await assertRejects(subprocess.getOneMessage());
89 assert.ok(ipcError.message.includes('subprocess.getOneMessage() could not complete'));

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

line 90why, one step back
90

TweaksSindre Sorhusbefore adoption698a1b8

lines 91–95why, one step back
91 const {exitCode, isTerminated, stderr} = await assertRejects(subprocess);
92 assert.equal(exitCode, 1);93 assert.equal(isTerminated, false);94 assert.ok(stderr.includes('Error: sendMessage()\'s argument type is invalid: the message cannot be serialized: 0.'));95 assert.ok(stderr.includes('The "message" argument must be one of type string'));

Debounce `message` events (#1087)ehmickybefore adoptione8bab97

line 96why, one step back
96});

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

lines 97–98why, one step back
97
98// EPIPE happens based on timing conditions, so we must repeat it until it happens

TweaksSindre Sorhusbefore adoption698a1b8

line 99why, one step back
99const findEpipeError = async () => {

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

lines 100–101why, one step back
100 while (true) {
101 // eslint-disable-next-line no-await-in-loop

TweaksSindre Sorhusbefore adoption698a1b8

line 102why, one step back
102 const error = await assertRejects(getEpipeError());

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

lines 103–109why, one step back
103 if (error.cause?.code === 'EPIPE') {
104 return error;105 }106 }107};108109const getEpipeError = async () => {

Fix CI tests (#1207)ehmickybefore adoption5587ae1

line 110why, one step back
110 const subprocess = execa('delay.js', ['0'], {ipc: true});

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

line 111why, one step back
111

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

lines 112–117why, one step back
112 while (true) {
113 // eslint-disable-next-line no-await-in-loop114 await subprocess.sendMessage('.');115 }116};117

TweaksSindre Sorhusbefore adoption698a1b8

lines 118–120why, one step back
118test('Can send messages while the subprocess is closing', async () => {
119 const {message} = await findEpipeError();120 assert.equal(message, 'subprocess.sendMessage() cannot be used: the subprocess is disconnecting.');

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 121–122why, one step back
121});
122

TweaksSindre Sorhusbefore adoption698a1b8

line 123why, one step back
123test('subprocess.sendMessage() handles I/O errors', async () => {

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 124–125why, one step back
124 const subprocess = execa('ipc-echo.js', {ipc: true});
125 const error = mockSendIoError(subprocess);

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126 assert.equal(await assertRejects(subprocess.sendMessage('.')), error);

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

line 127why, one step back
127

TweaksSindre Sorhusbefore adoption698a1b8

lines 128–132why, one step back
128 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess);
129 assert.equal(exitCode, 1);130 assert.equal(isTerminated, false);131 assert.ok(message.includes('Error: getOneMessage()'));132 assert.deepEqual(ipcOutput, []);

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 133–134why, one step back
133});
134

TweaksSindre Sorhusbefore adoption698a1b8

line 135why, one step back
135test('Does not hold message events on I/O errors', async () => {

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 136–138why, one step back
136 const subprocess = execa('ipc-echo.js', {ipc: true});
137 const error = mockSendIoError(subprocess);138 const promise = subprocess.sendMessage('.');

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

line 139why, one step back
139 subprocess.nodeChildProcess.emit('message', '.');

TweaksSindre Sorhusbefore adoption698a1b8

line 140why, one step back
140 assert.equal(await assertRejects(promise), error);

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

line 141why, one step back
141

TweaksSindre Sorhusbefore adoption698a1b8

lines 142–146why, one step back
142 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(subprocess);
143 assert.equal(exitCode, 1);144 assert.equal(isTerminated, false);145 assert.ok(message.includes('Error: getOneMessage()'));146 assert.deepEqual(ipcOutput, ['.']);

Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da

lines 147–148why, one step back
147});
148

TweaksSindre Sorhusbefore adoption698a1b8

lines 149–154why, one step back
149test('exports.sendMessage() handles I/O errors', async () => {
150 const {exitCode, isTerminated, message, ipcOutput} = await assertRejects(execa('ipc-send-io-error.js', {ipc: true}));151 assert.equal(exitCode, 1);152 assert.equal(isTerminated, false);153 assert.ok(message.includes(`Error: ${foobarString}`));154 assert.deepEqual(ipcOutput, []);

Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6

line 155why, one step back
155});

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 156–161why, one step back
156
157test('The "strict" sendMessage() option must use a boolean', async () => {158 const subprocess = execa('empty.js', {ipc: true});159 assert.throws(() => subprocess.sendMessage(foobarString, {strict: 'true'}), {message: /`strict` option must use a boolean/});160 await subprocess;161});