strict.js
This file
Path
test/ipc/strict.js
378 lines · 18.3 KiB · JavaScript
Last changed by
Fix IPC edge casesSindre Sorhusbefore adoption5f80cde
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379import assert from 'node:assert/strict';import {once} from 'node:events';import {setTimeout} from 'node:timers/promises';import test from 'node:test';import {assertRejects} from '../helpers/assert.js';import {execa} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {foobarString} from '../helpers/input.js';import {assertMaxListeners} from '../helpers/listeners.js';import {subprocessGetOne, subprocessGetFirst, mockSendIoError} from '../helpers/ipc.js';import {PARALLEL_COUNT} from '../helpers/parallel.js';
setFixtureDirectory();
const testStrictSuccessParentOne = async buffer => { const subprocess = execa('ipc-echo.js', {ipc: true, buffer}); await subprocess.sendMessage(foobarString, {strict: true}); assert.equal(await subprocess.getOneMessage(), foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);};
test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getOneMessage(), buffer false', () => testStrictSuccessParentOne(false));test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getOneMessage(), buffer true', () => testStrictSuccessParentOne(true));
const testStrictSuccessParentEach = async buffer => { const subprocess = execa('ipc-iterate.js', {ipc: true, buffer}); await subprocess.sendMessage('.', {strict: true}); assert.equal(await subprocess.getOneMessage(), '.'); await subprocess.sendMessage(foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? ['.'] : []);};
test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getEachMessage(), buffer false', () => testStrictSuccessParentEach(false));test('subprocess.sendMessage() "strict" succeeds if the subprocess uses exports.getEachMessage(), buffer true', () => testStrictSuccessParentEach(true));
// The message must not be wrapped with the `strict` metadatatest('subprocess.sendMessage() "strict" delivers the message if the subprocess disconnects before handling it', async () => { const subprocess = execa('ipc-get-strict-disconnect.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); await assertRejects(subprocess.sendMessage(foobarString, {strict: true}), {message: /^subprocess\.sendMessage\(\) failed: /}); const {stdout} = await subprocess; assert.equal(stdout, JSON.stringify(foobarString));});
test('exports.sendMessage() "strict" delivers the message if the parent disconnects before handling it', async () => { const subprocess = execa('ipc-send-strict.js', {ipc: true, reject: false}); subprocess.nodeChildProcess.once('message', () => { subprocess.nodeChildProcess.disconnect(); }); const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
// The acknowledgment cannot be sent back, but the message is still receivedtest('subprocess.sendMessage() "strict" delivers the message if the current process disconnects right after sending it', async () => { const subprocess = execa('ipc-get-print-ready.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); const sendPromise = subprocess.sendMessage(foobarString, {strict: true}); subprocess.nodeChildProcess.disconnect(); await assertRejects(sendPromise); const {stdout} = await subprocess; assert.equal(stdout, foobarString);});
test('exports.sendMessage() "strict" delivers the message if the subprocess disconnects right after sending it', async () => { const {ipcOutput} = await execa('ipc-send-strict-disconnect.js', {ipc: true}); assert.deepEqual(ipcOutput, [foobarString]);});
const testStrictMissingParent = async buffer => { const subprocess = execa('ipc-echo-twice.js', {ipcInput: foobarString, buffer}); const promise = subprocess.getOneMessage(); const secondPromise = subprocess.sendMessage(foobarString, {strict: true}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess is not listening to incoming messages.'); assert.equal(await promise, foobarString); await secondPromise;
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? [foobarString, foobarString] : []);};
test('subprocess.sendMessage() "strict" fails if the subprocess is not listening, buffer false', () => testStrictMissingParent(false));test('subprocess.sendMessage() "strict" fails if the subprocess is not listening, buffer true', () => testStrictMissingParent(true));
const testStrictExit = async buffer => { const subprocess = execa('ipc-send.js', {ipc: true, buffer}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);};
test('subprocess.sendMessage() "strict" fails if the subprocess exits, buffer false', () => testStrictExit(false));test('subprocess.sendMessage() "strict" fails if the subprocess exits, buffer true', () => testStrictExit(true));
const testStrictSuccessSubprocess = async (getMessage, buffer) => { const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer}); assert.equal(await getMessage(subprocess), foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);};
test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getOneMessage(), buffer false', () => testStrictSuccessSubprocess(subprocessGetOne, false));test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getOneMessage(), buffer true', () => testStrictSuccessSubprocess(subprocessGetOne, true));test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getEachMessage(), buffer false', () => testStrictSuccessSubprocess(subprocessGetFirst, false));test('exports.sendMessage() "strict" succeeds if the current process uses subprocess.getEachMessage(), buffer true', () => testStrictSuccessSubprocess(subprocessGetFirst, true));
test('exports.sendMessage() "strict" succeeds if the current process uses result.ipcOutput', async () => { const {ipcOutput} = await execa('ipc-send-strict.js', {ipc: true}); assert.deepEqual(ipcOutput, [foobarString]);});
test('exports.sendMessage() "strict" fails if the current process is not listening, buffer false', async () => { const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}})); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is not listening to incoming messages.')); assert.deepEqual(ipcOutput, []);});
test('Multiple subprocess.sendMessage() "strict" at once', async () => { const checkMaxListeners = assertMaxListeners();
const subprocess = execa('ipc-iterate.js', {ipc: true}); const messages = Array.from({length: PARALLEL_COUNT}, (_, index) => index); await Promise.all(messages.map(message => subprocess.sendMessage(message, {strict: true}))); await subprocess.sendMessage(foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, messages);
checkMaxListeners();});
test('subprocess.sendMessage() "strict" fails if the subprocess uses once()', async () => { const subprocess = execa('ipc-once-message.js', {ipc: true}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, ['.']);});
test('exports.sendMessage() "strict" fails if the current process uses once() and buffer false', async () => { const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}}); const [message] = await once(subprocess.nodeChildProcess, 'message'); assert.deepEqual(message, { id: 0, type: 'execa:ipc:request', message: foobarString, hasListeners: false, });
const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is not listening to incoming messages.')); assert.deepEqual(ipcOutput, []);});
test('subprocess.sendMessage() "strict" failure disconnects', async () => { const subprocess = execa('ipc-echo-twice-wait.js', {ipcInput: foobarString}); const promise = subprocess.getOneMessage(); const secondPromise = subprocess.sendMessage(foobarString, {strict: true}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess is not listening to incoming messages.'); assert.equal(await promise, foobarString); await secondPromise;
const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() cannot be used: the parent process has already exited or disconnected.')); assert.deepEqual(ipcOutput, [foobarString, foobarString]);});
test('exports.sendMessage() "strict" failure disconnects', async () => { const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(execa('ipc-send-strict-catch.js', {ipc: true, buffer: {ipc: false}})); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() cannot be used: the parent process has already exited or disconnected.')); assert.deepEqual(ipcOutput, []);});
const testIoErrorParent = async getMessage => { const subprocess = execa('ipc-send-strict.js', {ipc: true}); const cause = mockSendIoError(subprocess); const error = await assertRejects(getMessage(subprocess)); assert.ok(error.message.includes('subprocess.sendMessage() failed when sending an acknowledgment response to the subprocess.')); assert.equal(getMessage === subprocessGetOne ? error.cause : error.cause.cause, cause);
const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() failed: the parent process exited without listening to incoming messages.')); assert.deepEqual(ipcOutput, []);};
test('subprocess.getOneMessage() acknowledgment I/O error', () => testIoErrorParent(subprocessGetOne));test('subprocess.getEachMessage() acknowledgment I/O error', () => testIoErrorParent(subprocessGetFirst));
const testIoErrorSubprocess = async fixtureName => { const subprocess = execa(fixtureName, {ipc: true}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');
const {exitCode, isTerminated, stdout, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.equal(stdout, ''); assert.ok(stderr.includes('Error: sendMessage() failed when sending an acknowledgment response to the parent process.')); assert.ok(stderr.includes(`Error: ${foobarString}`)); assert.deepEqual(ipcOutput, []);};
test('exports.getOneMessage() acknowledgment I/O error', () => testIoErrorSubprocess('ipc-get-io-error.js'));test('exports.getEachMessage() acknowledgment I/O error', () => testIoErrorSubprocess('ipc-iterate-io-error.js'));
test('Opposite sendMessage() "strict", buffer true', async () => { const subprocess = execa('ipc-send-strict-get.js', {ipc: true}); await subprocess.sendMessage(foobarString, {strict: true});
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString, foobarString]);});
test('Opposite sendMessage() "strict", current process listening, buffer false', async () => { const subprocess = execa('ipc-send-strict-get.js', {ipc: true, buffer: {ipc: false}}); const [message] = await Promise.all([ subprocess.getOneMessage(), subprocess.sendMessage(foobarString, {strict: true}), ]); assert.equal(message, foobarString); assert.equal(await subprocess.getOneMessage(), foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, []);});
test('Opposite sendMessage() "strict", subprocess listening, buffer false', async () => { const subprocess = execa('ipc-send-strict-listen.js', {ipc: true, buffer: {ipc: false}}); await subprocess.sendMessage(foobarString, {strict: true}); assert.equal(await subprocess.getOneMessage(), foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, []);});
test('Opposite sendMessage() "strict", not listening, buffer false', async () => { const subprocess = execa('ipc-send-strict.js', {ipc: true, buffer: {ipc: false}}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.ok(message.startsWith('subprocess.sendMessage() failed: the subprocess is sending a message too, instead of listening to incoming messages.'));
const {exitCode, isTerminated, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(exitCode, 1); assert.equal(isTerminated, false); assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is sending a message too, instead of listening to incoming messages.')); assert.deepEqual(ipcOutput, []);});
test('Ignores "strict" responses with an Object.prototype property as id', async () => { const {ipcOutput} = await execa('ipc-send-strict-proto.js', {ipc: true}); assert.deepEqual(ipcOutput, []);});
// `forever.js` does not use Execa, so it never acknowledges the messagetest('Ignores "strict" responses sent by another subprocess', async () => { const subprocess = execa('forever.js', {ipc: true}); const sendPromise = subprocess.sendMessage(foobarString, {strict: true});
try { const {ipcOutput} = await execa('ipc-send-strict-forged.js', {ipc: true}); assert.deepEqual(ipcOutput, []); assert.equal(await Promise.race([sendPromise.then(() => 'resolved'), setTimeout(500, 'pending')]), 'pending'); } finally { subprocess.kill(); }
await Promise.all([assertRejects(sendPromise), assertRejects(subprocess)]);});
const testStrictJson = async buffer => { const subprocess = execa('ipc-echo.js', {ipc: true, serialization: 'json', buffer}); await subprocess.sendMessage(foobarString, {strict: true}); assert.equal(await subprocess.getOneMessage(), foobarString);
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, buffer ? [foobarString] : []);};
test('subprocess.sendMessage() "strict" works with serialization "json", buffer false', () => testStrictJson(false));test('subprocess.sendMessage() "strict" works with serialization "json", buffer true', () => testStrictJson(true));
test('exports.sendMessage() "strict" works with serialization "json"', async () => { const subprocess = execa('ipc-send-strict-get.js', {ipc: true, serialization: 'json'}); await subprocess.sendMessage(foobarString, {strict: true});
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString, foobarString]);});
test('subprocess.sendMessage() "strict" fails with serialization "json" if the subprocess is not listening', async () => { const subprocess = execa('ipc-send.js', {ipc: true, serialization: 'json'}); const {message} = await assertRejects(subprocess.sendMessage(foobarString, {strict: true})); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.');
const {ipcOutput} = await subprocess; assert.deepEqual(ipcOutput, [foobarString]);});
const noop = () => {};
// Messages received in the same batch are processed one after the other.// The deadlock must be detected on the message being processed, not on the first one received.// Without the detection, the subprocess would deadlock, so the `timeout` turns that hang into a failed assertion.test('Detects a "strict" deadlock on a later message of the same batch', async () => { const subprocess = execa('ipc-echo-strict-deadlock.js', {ipc: true, buffer: {ipc: false}, timeout: 1e4}); await subprocess.sendMessage(foobarString); subprocess.sendMessage(foobarString, {strict: true}).catch(noop);
const {exitCode, timedOut, stderr, ipcOutput} = await assertRejects(subprocess); assert.equal(timedOut, false); assert.equal(exitCode, 1); assert.ok(stderr.includes('Error: sendMessage() failed: the parent process is sending a message too, instead of listening to incoming messages.')); assert.deepEqual(ipcOutput, []);});
// Messages which look like Execa's internal `strict` messages but are sent without the `strict` option are user messages.// They must not wait for an acknowledgment response, and must be delivered as is.test('sendMessage() does not wait for an acknowledgment response unless the "strict" option is true', async () => { const subprocess = execa('forever.js', {ipc: true}); const message = { type: 'execa:ipc:request', id: 0, message: foobarString, hasListeners: false, };
try { await subprocess.sendMessage(message); } finally { subprocess.kill(); }
await assertRejects(subprocess);});
test('Messages with the internal "strict" request type are kept as is', async () => { const message = { type: 'execa:ipc:request', }; const {ipcOutput} = await execa('ipc-send-object.js', [JSON.stringify(message)], {ipc: true}); assert.deepEqual(ipcOutput, [message]);});
test('Messages with the internal "strict" request type are kept as is, from the parent', async () => { const message = { type: 'execa:ipc:request', message: foobarString, }; const {ipcOutput} = await execa('ipc-echo.js', {ipcInput: message}); assert.deepEqual(ipcOutput, [message]);});
test('Messages with the internal "strict" request type do not fail pending "strict" sends', async () => { const subprocess = execa('ipc-send-object.js', [JSON.stringify({type: 'execa:ipc:request'})], {ipc: true, buffer: {ipc: false}}); const sendPromise = subprocess.sendMessage(foobarString, {strict: true}); const {message} = await assertRejects(sendPromise); assert.equal(message, 'subprocess.sendMessage() failed: the subprocess exited without listening to incoming messages.'); await subprocess;});