reference.js
This file
Path
test/ipc/reference.js
132 lines · 5.2 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import assert from 'node:assert/strict';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 {PARALLEL_COUNT} from '../helpers/parallel.js';
setFixtureDirectory();
const testReference = async fixtureName => { const {timedOut} = await assertRejects(execa(fixtureName, {ipc: true, timeout: 1e3})); assert.equal(timedOut, true);};
test('exports.getOneMessage() keeps the subprocess alive', () => testReference('ipc-get-ref.js'));test('exports.getEachMessage() keeps the subprocess alive', () => testReference('ipc-iterate-ref.js'));
const testUnreference = async fixtureName => { const {ipcOutput} = await execa(fixtureName, {ipc: true}); assert.deepEqual(ipcOutput, []);};
test('exports.getOneMessage() does not keep the subprocess alive, reference false', () => testUnreference('ipc-get-unref.js'));test('exports.getEachMessage() does not keep the subprocess alive, reference false', () => testUnreference('ipc-iterate-unref.js'));
test('exports.sendMessage() keeps the subprocess alive', async () => { const {ipcOutput} = await execa('ipc-send-repeat.js', [`${PARALLEL_COUNT}`], {ipc: true}); const expectedOutput = Array.from({length: PARALLEL_COUNT}, (_, index) => index); assert.deepEqual(ipcOutput, expectedOutput);});
test('process.send() keeps the subprocess alive', async () => { const {ipcOutput, stdout} = await execa('ipc-process-send.js', {ipc: true}); assert.deepEqual(ipcOutput, [foobarString]); assert.equal(stdout, '.');});
test('process.send() keeps the subprocess alive, after getOneMessage()', async () => { const {ipcOutput, stdout} = await execa('ipc-process-send-get.js', {ipcInput: 0}); assert.deepEqual(ipcOutput, [foobarString]); assert.equal(stdout, '.');});
test('process.send() keeps the subprocess alive, after sendMessage()', async () => { const {ipcOutput, stdout} = await execa('ipc-process-send-send.js', {ipc: true}); assert.deepEqual(ipcOutput, ['.', foobarString]); assert.equal(stdout, '.');});
test('process.once("message") keeps the subprocess alive', async () => { const subprocess = execa('ipc-once-message.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); await subprocess.sendMessage(foobarString);
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.']); assert.equal(stdout, foobarString);});
test('process.once("message") keeps the subprocess alive, after sendMessage()', async () => { const subprocess = execa('ipc-once-message-send.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); await subprocess.sendMessage(foobarString);
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.']); assert.equal(stdout, foobarString);});
test('process.once("message") keeps the subprocess alive, after getOneMessage()', async () => { const subprocess = execa('ipc-once-message-get.js', {ipc: true}); await subprocess.sendMessage('.'); assert.equal(await subprocess.getOneMessage(), '.'); await subprocess.sendMessage(foobarString);
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.']); assert.equal(stdout, foobarString);});
test('process.once("disconnect") keeps the subprocess alive', async () => { const subprocess = execa('ipc-once-disconnect.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); subprocess.nodeChildProcess.disconnect();
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.']); assert.equal(stdout, '.');});
test('process.once("disconnect") keeps the subprocess alive, after sendMessage()', async () => { const subprocess = execa('ipc-once-disconnect-send.js', {ipc: true}); assert.equal(await subprocess.getOneMessage(), '.'); // eslint-disable-next-line node-test/no-duplicate-assertions -- the same message is read twice on purpose assert.equal(await subprocess.getOneMessage(), '.'); subprocess.nodeChildProcess.disconnect();
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.', '.']); assert.equal(stdout, '.');});
test('process.once("disconnect") does not keep the subprocess alive, after getOneMessage()', async () => { const subprocess = execa('ipc-once-disconnect-get.js', {ipc: true}); await subprocess.sendMessage('.'); assert.equal(await subprocess.getOneMessage(), '.'); subprocess.nodeChildProcess.disconnect();
const {ipcOutput, stdout} = await subprocess; assert.deepEqual(ipcOutput, ['.']); assert.equal(stdout, '.');});
test('Can call subprocess.disconnect() right away', async () => { const subprocess = execa('ipc-send.js', {ipc: true}); subprocess.nodeChildProcess.disconnect(); assert.equal(subprocess.nodeChildProcess.channel, null);
await assertRejects(subprocess.getOneMessage(), { message: /subprocess.getOneMessage\(\) could not complete/, }); await assertRejects(subprocess, { message: /Error: sendMessage\(\) cannot be used/, });});
test('Can call process.disconnect() right away', async () => { const {stdout, stderr} = await assertRejects(execa('ipc-disconnect-get.js', {ipc: true})); assert.equal(stdout, 'null'); assert.ok(stderr.includes('Error: getOneMessage() cannot be used'));});