Skip to content

Commit

Permalink
chore: enable parallel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Oct 4, 2023
1 parent 8466f1d commit 4e6d15c
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
const chalk = require('chalk');
const globby = require('globby');
const path = require('path');
const utils = require('./utils.js');
const { cpus } = require('os');
const { spawn } = require('child_process');
const host = 'node' + process.version.match(/^v(\d+)/)[1];
let target = process.argv[2] || 'host';
if (target === 'host') target = host;
Expand Down Expand Up @@ -64,18 +65,44 @@ if (flavor.match(/^test/)) {

const files = globby.sync(list);

files.sort().some(function (file) {
file = path.resolve(file);
try {
utils.spawn.sync('node', [path.basename(file), target], {
const maxConcurrency = Math.min(cpus().length, 4);

function runTest(file) {
return new Promise((resolve, reject) => {
const process = spawn('node', [path.basename(file), target], {
cwd: path.dirname(file),
stdio: 'inherit',
});
} catch (error) {
console.log();
console.log(`> ${chalk.red('Error!')} ${error.message}`);
console.log(`> ${chalk.red('Error!')} ${file} FAILED (in ${target})`);
process.exit(2);

process.on('close', (code) => {
if (code !== 0) {
reject(new Error(`Process exited with code ${code}`));
} else {
resolve();
}
});

process.on('error', reject);
});
}

async function run() {
const promises = files.sort().map(async (file) => {
file = path.resolve(file);
try {
await runTest(file);
} catch (error) {
console.log();
console.log(`> ${chalk.red('Error!')} ${error.message}`);
console.log(`> ${chalk.red('Error!')} ${file} FAILED (in ${target})`);
process.exit(2);
}
console.log(file, 'ok');
});

for (let i = 0; i < promises.length; i += maxConcurrency) {
await Promise.all(promises.slice(i, i + maxConcurrency));
}
console.log(file, 'ok');
});
}

run();

0 comments on commit 4e6d15c

Please sign in to comment.