From 77fed52a0d9e7842b768c92641b012f993ae08a2 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 28 Mar 2023 17:02:18 +0200 Subject: [PATCH] test: run WPT files in parallel again --- test/common/wpt.js | 48 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/test/common/wpt.js b/test/common/wpt.js index 57c409f5cc8a0f4..9192983f6bd4e86 100644 --- a/test/common/wpt.js +++ b/test/common/wpt.js @@ -10,6 +10,8 @@ const os = require('os'); const { inspect } = require('util'); const { Worker } = require('worker_threads'); +const workerPath = path.join(__dirname, 'wpt/worker.js'); + function getBrowserProperties() { const { node: version } = process.versions; // e.g. 18.13.0, 20.0.0-nightly202302078e6e215481 const release = /^\d+\.\d+\.\d+$/.test(version); @@ -402,6 +404,29 @@ const kIncomplete = 'incomplete'; const kUncaught = 'uncaught'; const NODE_UNCAUGHT = 100; +const limit = (concurrency) => { + let running = 0; + const queue = []; + + const execute = async (fn) => { + if (running < concurrency) { + running++; + try { + await fn(); + } finally { + running--; + if (queue.length > 0) { + execute(queue.shift()); + } + } + } else { + queue.push(fn); + } + }; + + return execute; +}; + class WPTRunner { constructor(path) { this.path = path; @@ -543,6 +568,8 @@ class WPTRunner { this.inProgress = new Set(queue.map((spec) => spec.filename)); + const run = limit(os.availableParallelism()); + for (const spec of queue) { const testFileName = spec.filename; const content = spec.getContent(); @@ -576,15 +603,7 @@ class WPTRunner { this.scriptsModifier?.(obj); scriptsToRun.push(obj); - /** - * Example test with no META variant - * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/sign_verify/hmac.https.any.js#L1-L4 - * - * Example test with multiple META variants - * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/generateKey/successes_RSASSA-PKCS1-v1_5.https.any.js#L1-L9 - */ - for (const variant of meta.variant || ['']) { - const workerPath = path.join(__dirname, 'wpt/worker.js'); + const runWorker = async (variant) => { const worker = new Worker(workerPath, { execArgv: this.flags, workerData: { @@ -635,6 +654,17 @@ class WPTRunner { }); await events.once(worker, 'exit').catch(() => {}); + }; + + /** + * Example test with no META variant + * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/sign_verify/hmac.https.any.js#L1-L4 + * + * Example test with multiple META variants + * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/generateKey/successes_RSASSA-PKCS1-v1_5.https.any.js#L1-L9 + */ + for (const variant of meta.variant || ['']) { + run(() => runWorker(variant)); } }