@@ -680,9 +680,9 @@ function cleanTestDirs() {
680680}
681681
682682// used to pass data from jake command line directly to run.js
683- function writeTestConfigFile ( tests , light , testConfigFile ) {
684- console . log ( 'Running test(s): ' + tests ) ;
685- var testConfigContents = JSON . stringify ( { test : [ tests ] , light : light } ) ;
683+ function writeTestConfigFile ( tests , light , taskConfigsFolder , workerCount , testConfigFile ) {
684+ var testConfigContents = JSON . stringify ( { test : tests ? [ tests ] : undefined , light : light , workerCount : workerCount , taskConfigsFolder : taskConfigsFolder } ) ;
685+ console . log ( 'Running tests with config: ' + testConfigContents ) ;
686686 fs . writeFileSync ( 'test.config' , testConfigContents ) ;
687687}
688688
@@ -692,7 +692,7 @@ function deleteTemporaryProjectOutput() {
692692 }
693693}
694694
695- function runConsoleTests ( defaultReporter , defaultSubsets ) {
695+ function runConsoleTests ( defaultReporter , runInParallel ) {
696696 cleanTestDirs ( ) ;
697697 var debug = process . env . debug || process . env . d ;
698698 tests = process . env . test || process . env . tests || process . env . t ;
@@ -701,9 +701,22 @@ function runConsoleTests(defaultReporter, defaultSubsets) {
701701 if ( fs . existsSync ( testConfigFile ) ) {
702702 fs . unlinkSync ( testConfigFile ) ;
703703 }
704+ var workerCount , taskConfigsFolder ;
705+ if ( runInParallel ) {
706+ // generate name to store task configuration files
707+ var prefix = os . tmpdir ( ) + "/ts-tests" ;
708+ var i = 1 ;
709+ do {
710+ taskConfigsFolder = prefix + i ;
711+ i ++ ;
712+ } while ( fs . existsSync ( taskConfigsFolder ) ) ;
713+ fs . mkdirSync ( taskConfigsFolder ) ;
714+
715+ workerCount = process . env . workerCount || os . cpus ( ) . length ;
716+ }
704717
705- if ( tests || light ) {
706- writeTestConfigFile ( tests , light , testConfigFile ) ;
718+ if ( tests || light || taskConfigsFolder ) {
719+ writeTestConfigFile ( tests , light , taskConfigsFolder , workerCount , testConfigFile ) ;
707720 }
708721
709722 if ( tests && tests . toLocaleLowerCase ( ) === "rwc" ) {
@@ -717,45 +730,71 @@ function runConsoleTests(defaultReporter, defaultSubsets) {
717730
718731 // timeout normally isn't necessary but Travis-CI has been timing out on compiler baselines occasionally
719732 // default timeout is 2sec which really should be enough, but maybe we just need a small amount longer
720- var subsetRegexes ;
721- if ( defaultSubsets . length === 0 ) {
722- subsetRegexes = [ tests ] ;
723- }
724- else {
725- var subsets = tests ? tests . split ( "|" ) : defaultSubsets ;
726- subsetRegexes = subsets . map ( function ( sub ) { return "^" + sub + ".*$" ; } ) ;
727- subsetRegexes . push ( "^(?!" + subsets . join ( "|" ) + ").*$" ) ;
728- }
729- subsetRegexes . forEach ( function ( subsetRegex , i ) {
730- tests = subsetRegex ? ' -g "' + subsetRegex + '"' : '' ;
733+ if ( ! runInParallel ) {
734+ tests = tests ? ' -g "' + tests + '"' : '' ;
731735 var cmd = "mocha" + ( debug ? " --debug-brk" : "" ) + " -R " + reporter + tests + colors + ' -t ' + testTimeout + ' ' + run ;
732736 console . log ( cmd ) ;
733- function finish ( ) {
734- deleteTemporaryProjectOutput ( ) ;
735- complete ( ) ;
736- }
737737 exec ( cmd , function ( ) {
738- if ( lintFlag && i === 0 ) {
739- var lint = jake . Task [ 'lint' ] ;
740- lint . addListener ( 'complete' , function ( ) {
741- complete ( ) ;
742- } ) ;
743- lint . invoke ( ) ;
738+ if ( i === 0 ) {
739+ runLinter ( ) ;
744740 }
745741 finish ( ) ;
746742 } , finish ) ;
747- } ) ;
743+
744+ }
745+ else {
746+ // run task to load all tests and partition then between workers
747+ var cmd = "mocha " + " -R min " + colors + run ;
748+ console . log ( cmd ) ;
749+ exec ( cmd , function ( ) {
750+ // read all configuration files and spawn a worker for every config
751+ var configFiles = fs . readdirSync ( taskConfigsFolder ) ;
752+ var counter = configFiles . length ;
753+ // schedule work for chunks
754+ configFiles . forEach ( function ( f ) {
755+ var configPath = path . join ( taskConfigsFolder , f ) ;
756+ var workerCmd = "mocha" + " -t " + testTimeout + " -R " + reporter + " " + colors + " " + run + " --config='" + configPath + "'" ;
757+ console . log ( workerCmd ) ;
758+ exec ( workerCmd , finishWorker , finishWorker )
759+ } ) ;
760+
761+ function finishWorker ( ) {
762+ counter -- ;
763+ if ( counter === 0 ) {
764+ // last worker clean everything and runs linter
765+ runLinter ( ) ;
766+ deleteTemporaryProjectOutput ( ) ;
767+ jake . rmRf ( taskConfigsFolder ) ;
768+ }
769+ complete ( ) ;
770+ }
771+ } ) ;
772+ }
773+ function finish ( ) {
774+ deleteTemporaryProjectOutput ( ) ;
775+ complete ( ) ;
776+ }
777+ function runLinter ( ) {
778+ if ( ! lintFlag ) {
779+ return ;
780+ }
781+ var lint = jake . Task [ 'lint' ] ;
782+ lint . addListener ( 'complete' , function ( ) {
783+ complete ( ) ;
784+ } ) ;
785+ lint . invoke ( ) ;
786+ }
748787}
749788
750789var testTimeout = 20000 ;
751790desc ( "Runs all the tests in parallel using the built run.js file. Optional arguments are: t[ests]=category1|category2|... d[ebug]=true." ) ;
752791task ( "runtests-parallel" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
753- runConsoleTests ( 'min' , [ 'compiler' , 'conformance' , 'Projects' , 'fourslash' ] ) ;
792+ runConsoleTests ( 'min' , /*runInParallel*/ true ) ;
754793} , { async : true } ) ;
755794
756795desc ( "Runs the tests using the built run.js file. Optional arguments are: t[ests]=regex r[eporter]=[list|spec|json|<more>] d[ebug]=true color[s]=false lint=true." ) ;
757796task ( "runtests" , [ "build-rules" , "tests" , builtLocalDirectory ] , function ( ) {
758- runConsoleTests ( 'mocha-fivemat-progress-reporter' , [ ] ) ;
797+ runConsoleTests ( 'mocha-fivemat-progress-reporter' , /*runInParallel*/ false ) ;
759798} , { async : true } ) ;
760799
761800desc ( "Generates code coverage data via instanbul" ) ;
0 commit comments