@@ -4,6 +4,20 @@ import * as os from "os";
44import * as path from "path" ;
55import { config , Pool } from "threads" ;
66
7+ function fileArrToString ( fileArr : string [ ] ) : string {
8+ return fileArr . map ( val => path . basename ( val ) . replace ( ".spec.js" , "" ) ) . join ( ", " ) ;
9+ }
10+
11+ function printTestStats ( testCount : number , failedTestCount : number , header : string , footer : string ) : void {
12+ console . log ( "-----------------" ) ;
13+ console . log ( header ) ;
14+ console . log ( `Total: ${ testCount } ` ) ;
15+ console . log ( `Passed: ${ testCount - failedTestCount } ` ) ;
16+ console . log ( `Failed: ${ failedTestCount } ` ) ;
17+ console . log ( footer ) ;
18+ console . log ( "-----------------" ) ;
19+ }
20+
721config . set ( {
822 basepath : {
923 node : __dirname ,
@@ -12,45 +26,58 @@ config.set({
1226
1327let cpuCount = os . cpus ( ) . length + 1 ;
1428if ( "TRAVIS" in process . env && "CI" in process . env ) {
15- // fixed thread count for CI
16- cpuCount = 8 ;
29+ // fixed thread count for CI
30+ cpuCount = 8 ;
1731}
18- const testFiles : string [ ] = glob . sync ( "./test/**/*.spec.ts " ) ;
32+ const testFiles : string [ ] = glob . sync ( "./test/**/*.spec.js " ) ;
1933const pool = new Pool ( cpuCount ) ;
2034let jobCounter = 0 ;
21-
22- const fileArrToString = ( fileArr : string [ ] ) =>
23- fileArr . map ( val => path . basename ( val ) . replace ( ".spec.ts" , "" ) ) . join ( ", " ) ;
35+ const testStartTime = new Date ( ) ;
36+ const fileCount = testFiles . length ;
37+ let exitWithError = false ;
38+ let totalTestCount = 0 ;
39+ let totalFailedTestCount = 0 ;
2440
2541console . log (
2642 `Running tests: ${ fileArrToString ( testFiles ) } with ${ cpuCount } threads` ) ;
2743
28- const filesPerThread = Math . floor ( testFiles . length / cpuCount ) ;
29- const threadsWithMoreWork = testFiles . length % cpuCount ;
30-
31- for ( let i = 1 ; i <= cpuCount ; i ++ ) {
32- let files : string [ ] = [ ] ;
33- if ( i <= threadsWithMoreWork ) {
34- files = testFiles . splice ( 0 , filesPerThread + 1 ) ;
35- } else {
36- files = testFiles . splice ( 0 , filesPerThread ) ;
37- }
38- console . log ( `Running tests: ${ fileArrToString ( files ) } in thread ${ i } ` ) ;
39-
40- pool . run ( "./test_thread" )
41- . send ( { files : files } )
42- . on ( "done" ,
43- ( results , input ) => {
44- jobCounter ++ ;
45- console . log ( `Tests ${ fileArrToString ( files ) } ${ jobCounter } /${
46- cpuCount } done.`) ;
47- } )
48- . on ( "error" , error => {
49- console . log ( "Exception in test:" , files , error ) ;
50- } ) ;
51- }
44+ testFiles . forEach ( file => {
45+ pool . run ( "./test_thread" )
46+ . send ( { files : [ file ] } )
47+ . on ( "done" ,
48+ ( testCount , failedTestCount ) => {
49+ if ( failedTestCount !== 0 ) {
50+ exitWithError = true ;
51+ }
52+ totalTestCount += testCount ;
53+ totalFailedTestCount += failedTestCount ;
54+ jobCounter ++ ;
55+ printTestStats (
56+ testCount ,
57+ failedTestCount ,
58+ `Tests ${ file } results:` ,
59+ `Thread: ${ jobCounter } /${ fileCount } done.` ) ;
60+ } )
61+ . on ( "error" , error => {
62+ console . log ( "Fatal non test related Exception in test file:" , file , error ) ;
63+ } ) ;
64+ } ) ;
5265
5366pool . on ( "finished" , ( ) => {
54- console . log ( "Everything done, shutting down the thread pool." ) ;
55- pool . killAll ( ) ;
67+ let footer = "All tests passed!" ;
68+ if ( exitWithError ) {
69+ footer = "Exiting with Error: One or more tests failed!" ;
70+ }
71+ printTestStats ( totalTestCount , totalFailedTestCount , "Final Results:" , footer ) ;
72+
73+ console . log ( "Everything done, shutting down the thread pool." ) ;
74+ const timeInMs = ( new Date ( ) . valueOf ( ) - testStartTime . valueOf ( ) ) ;
75+ console . log ( `Tests took: ${ Math . floor ( timeInMs / 1000 / 60 ) } :${ Math . floor ( timeInMs / 1000 ) % 60 } ` ) ;
76+
77+ pool . killAll ( ) ;
78+
79+ if ( exitWithError ) {
80+ process . exit ( 1 ) ;
81+ }
5682} ) ;
83+
0 commit comments