Skip to content

Commit

Permalink
Refactor parallel tests (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 21, 2024
1 parent 7143492 commit 5cf6312
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"ava": "^6.0.1",
"c8": "^9.1.0",
"get-node": "^15.0.0",
"is-in-ci": "^0.1.0",
"is-running": "^2.1.0",
"path-exists": "^5.0.0",
"path-key": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions test/helpers/parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import isInCi from 'is-in-ci';

export const PARALLEL_COUNT = isInCi ? 10 : 100;
7 changes: 3 additions & 4 deletions test/io/output-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {STANDARD_STREAMS} from '../helpers/stdio.js';
import {foobarString} from '../helpers/input.js';
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {assertMaxListeners} from '../helpers/listeners.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand Down Expand Up @@ -39,11 +40,9 @@ const testListenersCleanup = async (t, isMultiple) => {
test.serial('process.std* listeners are cleaned up on success with a single input', testListenersCleanup, false);
test.serial('process.std* listeners are cleaned up on success with multiple inputs', testListenersCleanup, true);

const subprocessesCount = 100;

test.serial('Can spawn many subprocesses in parallel', async t => {
const results = await Promise.all(
Array.from({length: subprocessesCount}, () => execa('noop.js', [foobarString])),
Array.from({length: PARALLEL_COUNT}, () => execa('noop.js', [foobarString])),
);
t.true(results.every(({stdout}) => stdout === foobarString));
});
Expand All @@ -57,7 +56,7 @@ const testMaxListeners = async (t, isMultiple, maxListenersCount) => {

try {
const results = await Promise.all(
Array.from({length: subprocessesCount}, () => execa('empty.js', getComplexStdio(isMultiple))),
Array.from({length: PARALLEL_COUNT}, () => execa('empty.js', getComplexStdio(isMultiple))),
);
t.true(results.every(({exitCode}) => exitCode === 0));
} finally {
Expand Down
5 changes: 2 additions & 3 deletions test/ipc/buffer-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import {execa, execaSync} from '../../index.js';
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {foobarString, foobarArray} from '../helpers/input.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand Down Expand Up @@ -57,11 +58,9 @@ test('Sets empty error.ipcOutput, sync', t => {
t.deepEqual(ipcOutput, []);
});

const HIGH_CONCURRENCY_COUNT = 10;

test.serial('Can retrieve initial IPC messages under heavy load', async t => {
await Promise.all(
Array.from({length: HIGH_CONCURRENCY_COUNT}, async (_, index) => {
Array.from({length: PARALLEL_COUNT}, async (_, index) => {
const {ipcOutput} = await execa('ipc-send-argv.js', [`${index}`], {ipc: true});
t.deepEqual(ipcOutput, [`${index}`]);
}),
Expand Down
7 changes: 3 additions & 4 deletions test/ipc/get-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {execa} from '../../index.js';
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {foobarString, foobarArray} from '../helpers/input.js';
import {iterateAllMessages} from '../helpers/ipc.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand Down Expand Up @@ -82,15 +83,13 @@ test('Exceptions in subprocess.getEachMessage() disconnect', async t => {
t.deepEqual(ipcOutput, [foobarString]);
});

const HIGH_CONCURRENCY_COUNT = 10;

test.serial('Can send many messages at once with exports.getEachMessage()', async t => {
const subprocess = execa('ipc-iterate.js', {ipc: true});
await Promise.all(Array.from({length: HIGH_CONCURRENCY_COUNT}, (_, index) => subprocess.sendMessage(index)));
await Promise.all(Array.from({length: PARALLEL_COUNT}, (_, index) => subprocess.sendMessage(index)));
await subprocess.sendMessage(foobarString);

const {ipcOutput} = await subprocess;
t.deepEqual(ipcOutput, Array.from({length: HIGH_CONCURRENCY_COUNT}, (_, index) => index));
t.deepEqual(ipcOutput, Array.from({length: PARALLEL_COUNT}, (_, index) => index));
});

test('Disconnecting in the current process stops exports.getEachMessage()', async t => {
Expand Down
5 changes: 2 additions & 3 deletions test/ipc/get-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
subprocessExchange,
alwaysPass,
} from '../helpers/ipc.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand Down Expand Up @@ -80,11 +81,9 @@ const testFilterSubprocess = async (t, fixtureName, expectedOutput) => {
test('exports.getOneMessage() can filter messages', testFilterSubprocess, 'ipc-echo-filter.js', [foobarArray[1]]);
test('exports.exchangeMessage() can filter messages', testFilterSubprocess, 'ipc-echo-filter-exchange.js', ['.', foobarArray[1]]);

const HIGH_CONCURRENCY_COUNT = 10;

const testHeavyLoad = async (t, exchangeMethod) => {
await Promise.all(
Array.from({length: HIGH_CONCURRENCY_COUNT}, async (_, index) => {
Array.from({length: PARALLEL_COUNT}, async (_, index) => {
const subprocess = execa('ipc-send-argv.js', [`${index}`], {ipc: true, buffer: false});
t.is(await exchangeMethod(subprocess, {}), `${index}`);
await subprocess;
Expand Down
5 changes: 2 additions & 3 deletions test/ipc/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {execa} from '../../index.js';
import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {foobarString} from '../helpers/input.js';
import {subprocessSendGetOne, subprocessExchange} from '../helpers/ipc.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand All @@ -15,11 +16,9 @@ const testExchange = async (t, exchangeMethod) => {
test('Can exchange IPC messages', testExchange, subprocessSendGetOne);
test('Can exchange IPC messages, exchangeMessage()', testExchange, subprocessExchange);

const HIGH_CONCURRENCY_COUNT = 10;

const testHeavyLoad = async (t, exchangeMethod) => {
await Promise.all(
Array.from({length: HIGH_CONCURRENCY_COUNT}, async (_, index) => {
Array.from({length: PARALLEL_COUNT}, async (_, index) => {
const subprocess = execa('ipc-echo.js', {ipc: true});
t.is(await exchangeMethod(subprocess, index), index);
await subprocess;
Expand Down
7 changes: 3 additions & 4 deletions test/pipe/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
import {foobarString} from '../helpers/input.js';
import {assertMaxListeners} from '../helpers/listeners.js';
import {fullReadableStdio} from '../helpers/stdio.js';
import {PARALLEL_COUNT} from '../helpers/parallel.js';

setFixtureDirectory();

Expand Down Expand Up @@ -41,12 +42,10 @@ test('Can pipe three sources to same destination', async t => {
t.is(await thirdPromise, await destination);
});

const subprocessesCount = 100;

test.serial('Can pipe many sources to same destination', async t => {
const checkMaxListeners = assertMaxListeners(t);

const expectedResults = Array.from({length: subprocessesCount}, (_, index) => `${index}`).sort();
const expectedResults = Array.from({length: PARALLEL_COUNT}, (_, index) => `${index}`).sort();
const sources = expectedResults.map(expectedResult => execa('noop.js', [expectedResult]));
const destination = execa('stdin.js');
const pipePromises = sources.map(source => source.pipe(destination));
Expand All @@ -64,7 +63,7 @@ test.serial('Can pipe same source to many destinations', async t => {
const checkMaxListeners = assertMaxListeners(t);

const source = execa('noop-fd.js', ['1', foobarString]);
const expectedResults = Array.from({length: subprocessesCount}, (_, index) => `${index}`);
const expectedResults = Array.from({length: PARALLEL_COUNT}, (_, index) => `${index}`);
const destinations = expectedResults.map(expectedResult => execa('noop-stdin-double.js', [expectedResult]));
const pipePromises = destinations.map(destination => source.pipe(destination));

Expand Down

0 comments on commit 5cf6312

Please sign in to comment.