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 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–6why, one step back4import {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 back7import {PARALLEL_COUNT} from '../helpers/parallel.js';Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
line 8why, one step back8import {mockSendIoError} from '../helpers/ipc.js';Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 9–11why, one step back910setFixtureDirectory();11TweaksSindre Sorhusbefore adoption698a1b8
line 12why, one step back12test('Can exchange IPC messages', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 13why, one step back13 const subprocess = execa('ipc-echo.js', {ipc: true});Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 14why, one step back14 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 15why, one step back15 assert.equal(await subprocess.getOneMessage(), foobarString);Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 16why, one step back16 await subprocess;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 17why, one step back17});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 18why, one step back18TweaksSindre Sorhusbefore adoption698a1b8
line 19why, one step back19test('Can exchange IPC messages under heavy load', async () => {Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 20–22why, one step back20 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 back23 assert.equal(await subprocess.getOneMessage(), index);Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
lines 24–25why, one step back24 await subprocess;25 }));Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 26why, one step back26});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 27why, one step back27TweaksSindre Sorhusbefore adoption698a1b8
line 28why, one step back28test('The "serialization" option defaults to "advanced"', async () => {Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 29why, one step back29 const subprocess = execa('ipc-echo.js', {ipc: true});Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 30–31why, one step back30 await subprocess.sendMessage([0n]);31 const message = await subprocess.getOneMessage();TweaksSindre Sorhusbefore adoption698a1b8
line 32why, one step back32 assert.equal(message[0], 0n);Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 33why, one step back33 await subprocess;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 34why, one step back34});Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 35why, one step back35TweaksSindre Sorhusbefore adoption698a1b8
line 36why, one step back36test('Can use "serialization: json" option', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 37–38why, one step back37 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 back39 await subprocess.sendMessage(date);TweaksSindre Sorhusbefore adoption698a1b8
line 40why, one step back40 assert.equal(await subprocess.getOneMessage(), date.toJSON());Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 41why, one step back41 await subprocess;Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 42why, one step back42});Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 43why, one step back43Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
lines 44–53why, one step back44const 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'));53TweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54test('Validates JSON payload with serialization: "json"', async () => {Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 55why, one step back55 const subprocess = execa('ipc-echo.js', {ipc: true, serialization: 'json'});TweaksSindre Sorhusbefore adoption698a1b8
lines 56–57why, one step back56 await assertRejects(subprocess.sendMessage([0n]), {message: /serialize a BigInt/});57 await assertRejects(subprocess);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 58why, one step back58});Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 59why, one step back59Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
lines 60–61why, one step back60const BIG_PAYLOAD_SIZE = '.'.repeat(1e6);61TweaksSindre Sorhusbefore adoption698a1b8
line 62why, one step back62test('Handles backpressure', async () => {Add promise-based IPC (#1059)ehmickybefore adoptionacc2b68
line 63why, one step back63 const subprocess = execa('ipc-iterate.js', {ipc: true});Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 64why, one step back64 await subprocess.sendMessage(BIG_PAYLOAD_SIZE);TweaksSindre Sorhusbefore adoption698a1b8
lines 65–66why, one step back65 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 back67 const {ipcOutput} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 68why, one step back68 assert.deepEqual(ipcOutput, [BIG_PAYLOAD_SIZE]);Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 69why, one step back69});Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 70why, one step back70TweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71test('Disconnects IPC on exports.sendMessage() error', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
lines 72–73why, one step back72 const subprocess = execa('ipc-get-send-get.js', ['false'], {ipc: true});73 await subprocess.sendMessage(foobarString);TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74 assert.equal(await subprocess.getOneMessage(), foobarString);Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 75why, one step back75TweaksSindre Sorhusbefore adoption698a1b8
lines 76–78why, one step back76 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 back79TweaksSindre Sorhusbefore adoption698a1b8
lines 80–83why, one step back80 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 back84});Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 85why, one step back85TweaksSindre Sorhusbefore adoption698a1b8
line 86why, one step back86test('Disconnects IPC on subprocess.sendMessage() error', async () => {Debounce `message` events (#1087)ehmickybefore adoptione8bab97
line 87why, one step back87 const subprocess = execa('ipc-send-error.js', {ipc: true});TweaksSindre Sorhusbefore adoption698a1b8
lines 88–89why, one step back88 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 back90TweaksSindre Sorhusbefore adoption698a1b8
lines 91–95why, one step back91 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 back96});Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
lines 97–98why, one step back9798// EPIPE happens based on timing conditions, so we must repeat it until it happensTweaksSindre Sorhusbefore adoption698a1b8
line 99why, one step back99const findEpipeError = async () => {Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
lines 100–101why, one step back100 while (true) {101 // eslint-disable-next-line no-await-in-loopTweaksSindre Sorhusbefore adoption698a1b8
line 102why, one step back102 const error = await assertRejects(getEpipeError());Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
lines 103–109why, one step back103 if (error.cause?.code === 'EPIPE') {104 return error;105 }106 }107};108109const getEpipeError = async () => {Fix CI tests (#1207)ehmickybefore adoption5587ae1
line 110why, one step back110 const subprocess = execa('delay.js', ['0'], {ipc: true});Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 111why, one step back111Better error message on IPC `EPIPE` (#1088)ehmickybefore adoption97b17e6
lines 112–117why, one step back112 while (true) {113 // eslint-disable-next-line no-await-in-loop114 await subprocess.sendMessage('.');115 }116};117TweaksSindre Sorhusbefore adoption698a1b8
lines 118–120why, one step back118test('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 back121});122TweaksSindre Sorhusbefore adoption698a1b8
line 123why, one step back123test('subprocess.sendMessage() handles I/O errors', async () => {Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
lines 124–125why, one step back124 const subprocess = execa('ipc-echo.js', {ipc: true});125 const error = mockSendIoError(subprocess);TweaksSindre Sorhusbefore adoption698a1b8
line 126why, one step back126 assert.equal(await assertRejects(subprocess.sendMessage('.')), error);Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
line 127why, one step back127TweaksSindre Sorhusbefore adoption698a1b8
lines 128–132why, one step back128 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 back133});134TweaksSindre Sorhusbefore adoption698a1b8
line 135why, one step back135test('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 back136 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 back139 subprocess.nodeChildProcess.emit('message', '.');TweaksSindre Sorhusbefore adoption698a1b8
line 140why, one step back140 assert.equal(await assertRejects(promise), error);Add tests for I/O errors during `sendMessage()` (#1095)ehmickybefore adoptionc6c66da
line 141why, one step back141TweaksSindre Sorhusbefore adoption698a1b8
lines 142–146why, one step back142 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 back147});148TweaksSindre Sorhusbefore adoption698a1b8
lines 149–154why, one step back149test('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 back155});Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 156–161why, one step back156157test('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});