@@ -10,6 +10,8 @@ const os = require('os');
1010const { inspect } = require ( 'util' ) ;
1111const { Worker } = require ( 'worker_threads' ) ;
1212
13+ const workerPath = path . join ( __dirname , 'wpt/worker.js' ) ;
14+
1315function getBrowserProperties ( ) {
1416 const { node : version } = process . versions ; // e.g. 18.13.0, 20.0.0-nightly202302078e6e215481
1517 const release = / ^ \d + \. \d + \. \d + $ / . test ( version ) ;
@@ -402,6 +404,29 @@ const kIncomplete = 'incomplete';
402404const kUncaught = 'uncaught' ;
403405const NODE_UNCAUGHT = 100 ;
404406
407+ const limit = ( concurrency ) => {
408+ let running = 0 ;
409+ const queue = [ ] ;
410+
411+ const execute = async ( fn ) => {
412+ if ( running < concurrency ) {
413+ running ++ ;
414+ try {
415+ await fn ( ) ;
416+ } finally {
417+ running -- ;
418+ if ( queue . length > 0 ) {
419+ execute ( queue . shift ( ) ) ;
420+ }
421+ }
422+ } else {
423+ queue . push ( fn ) ;
424+ }
425+ } ;
426+
427+ return execute ;
428+ } ;
429+
405430class WPTRunner {
406431 constructor ( path ) {
407432 this . path = path ;
@@ -543,6 +568,8 @@ class WPTRunner {
543568
544569 this . inProgress = new Set ( queue . map ( ( spec ) => spec . filename ) ) ;
545570
571+ const run = limit ( os . availableParallelism ( ) ) ;
572+
546573 for ( const spec of queue ) {
547574 const testFileName = spec . filename ;
548575 const content = spec . getContent ( ) ;
@@ -576,15 +603,7 @@ class WPTRunner {
576603 this . scriptsModifier ?. ( obj ) ;
577604 scriptsToRun . push ( obj ) ;
578605
579- /**
580- * Example test with no META variant
581- * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/sign_verify/hmac.https.any.js#L1-L4
582- *
583- * Example test with multiple META variants
584- * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/generateKey/successes_RSASSA-PKCS1-v1_5.https.any.js#L1-L9
585- */
586- for ( const variant of meta . variant || [ '' ] ) {
587- const workerPath = path . join ( __dirname , 'wpt/worker.js' ) ;
606+ const runWorker = async ( variant ) => {
588607 const worker = new Worker ( workerPath , {
589608 execArgv : this . flags ,
590609 workerData : {
@@ -635,6 +654,17 @@ class WPTRunner {
635654 } ) ;
636655
637656 await events . once ( worker , 'exit' ) . catch ( ( ) => { } ) ;
657+ } ;
658+
659+ /**
660+ * Example test with no META variant
661+ * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/sign_verify/hmac.https.any.js#L1-L4
662+ *
663+ * Example test with multiple META variants
664+ * https://github.com/nodejs/node/blob/03854f6/test/fixtures/wpt/WebCryptoAPI/generateKey/successes_RSASSA-PKCS1-v1_5.https.any.js#L1-L9
665+ */
666+ for ( const variant of meta . variant || [ '' ] ) {
667+ run ( ( ) => runWorker ( variant ) ) ;
638668 }
639669 }
640670
0 commit comments