Skip to content
Myr

async.js

This file

Path

test/stdio/async.js

84 lines · 4.0 KiB · JavaScript

Last changed by

Fix race condition with `subprocess.stdin.destroy()` (#904)ehmickybefore adoption588bf16

History of this file

Take it

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 1–2why, one step back
1import {once, defaultMaxListeners} from 'node:events';
2import process from 'node:process';

Fix error handling of `stdin` (#787)ehmickybefore adoption4328670

line 3why, one step back
3import {setImmediate} from 'node:timers/promises';

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 4–8why, one step back
4import test from 'ava';
5import {execa} from '../../index.js';6import {STANDARD_STREAMS} from '../helpers/stdio.js';7import {foobarString} from '../helpers/input.js';8import {setFixtureDir} from '../helpers/fixtures-dir.js';

Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0

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

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 10–21why, one step back
10
11setFixtureDir();1213const getStandardStreamListeners = stream => Object.fromEntries(stream.eventNames().map(eventName => [eventName, stream.listeners(eventName)]));14const getStandardStreamsListeners = () => STANDARD_STREAMS.map(stream => getStandardStreamListeners(stream));1516const getComplexStdio = isMultiple => ({17 stdin: ['pipe', 'inherit', ...(isMultiple ? [0, process.stdin] : [])],18 stdout: ['pipe', 'inherit', ...(isMultiple ? [1, process.stdout] : [])],19 stderr: ['pipe', 'inherit', ...(isMultiple ? [2, process.stderr] : [])],20});21

Fix race condition with `subprocess.stdin.destroy()` (#904)ehmickybefore adoption588bf16

line 22why, one step back
22const onStdinRemoveListener = () => once(process.stdin, 'removeListener');

Fix randomly failing tests (#793)ehmickybefore adoption45475e7

line 23why, one step back
23

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 24–25why, one step back
24const testListenersCleanup = async (t, isMultiple) => {
25 const streamsPreviousListeners = getStandardStreamsListeners();

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 26why, one step back
26 const subprocess = execa('empty.js', getComplexStdio(isMultiple));

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 27why, one step back
27 t.notDeepEqual(getStandardStreamsListeners(), streamsPreviousListeners);

Fix race condition with `subprocess.stdin.destroy()` (#904)ehmickybefore adoption588bf16

line 28why, one step back
28 await Promise.all([subprocess, onStdinRemoveListener()]);

Fix randomly failing tests (#793)ehmickybefore adoption45475e7

lines 29–31why, one step back
29 if (isMultiple) {
30 await onStdinRemoveListener();31 }

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 32why, one step back
32

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 33why, one step back
33 for (const [fdNumber, streamNewListeners] of Object.entries(getStandardStreamsListeners())) {

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 34why, one step back
34 const defaultListeners = Object.fromEntries(Reflect.ownKeys(streamNewListeners).map(eventName => [eventName, []]));

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 35why, one step back
35 t.deepEqual(streamNewListeners, {...defaultListeners, ...streamsPreviousListeners[fdNumber]});

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 36–41why, one step back
36 }
37};3839test.serial('process.std* listeners are cleaned up on success with a single input', testListenersCleanup, false);40test.serial('process.std* listeners are cleaned up on success with multiple inputs', testListenersCleanup, true);41

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 42why, one step back
42const subprocessesCount = 100;

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 43why, one step back
43

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 44why, one step back
44test.serial('Can spawn many subprocesses in parallel', async t => {

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 45why, one step back
45 const results = await Promise.all(

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 46why, one step back
46 Array.from({length: subprocessesCount}, () => execa('noop.js', [foobarString])),

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 47–51why, one step back
47 );
48 t.true(results.every(({stdout}) => stdout === foobarString));49});5051const testMaxListeners = async (t, isMultiple, maxListenersCount) => {

Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0

line 52why, one step back
52 const checkMaxListeners = assertMaxListeners(t);

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 53–59why, one step back
53
54 for (const standardStream of STANDARD_STREAMS) {55 standardStream.setMaxListeners(maxListenersCount);56 }5758 try {59 const results = await Promise.all(

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 60why, one step back
60 Array.from({length: subprocessesCount}, () => execa('empty.js', getComplexStdio(isMultiple))),

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 61–63why, one step back
61 );
62 t.true(results.every(({exitCode}) => exitCode === 0));63 } finally {

Fix randomly failing tests (#793)ehmickybefore adoption45475e7

lines 64–65why, one step back
64 await setImmediate();
65 await setImmediate();

Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0

line 66why, one step back
66 checkMaxListeners();

Fix randomly failing tests (#793)ehmickybefore adoption45475e7

line 67why, one step back
67

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

lines 68–84why, one step back
68 for (const standardStream of STANDARD_STREAMS) {
69 t.is(standardStream.getMaxListeners(), maxListenersCount);70 standardStream.setMaxListeners(defaultMaxListeners);71 }72 }73};7475test.serial('No warning with maxListeners 1 and ["pipe", "inherit"]', testMaxListeners, false, 1);76test.serial('No warning with maxListeners default and ["pipe", "inherit"]', testMaxListeners, false, defaultMaxListeners);77test.serial('No warning with maxListeners 100 and ["pipe", "inherit"]', testMaxListeners, false, 100);78test.serial('No warning with maxListeners Infinity and ["pipe", "inherit"]', testMaxListeners, false, Number.POSITIVE_INFINITY);79test.serial('No warning with maxListeners 0 and ["pipe", "inherit"]', testMaxListeners, false, 0);80test.serial('No warning with maxListeners 1 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 1);81test.serial('No warning with maxListeners default and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, defaultMaxListeners);82test.serial('No warning with maxListeners 100 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 100);83test.serial('No warning with maxListeners Infinity and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, Number.POSITIVE_INFINITY);84test.serial('No warning with maxListeners 0 and ["pipe", "inherit"], multiple inputs', testMaxListeners, true, 0);