|
| 1 | +import {MatchError} from "alsatian"; |
| 2 | +import * as glob from "glob"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | +import {config, Pool} from "threads"; |
| 6 | + |
| 7 | +config.set({ |
| 8 | + basepath: { |
| 9 | + node: __dirname, |
| 10 | + } |
| 11 | +}); |
| 12 | + |
| 13 | +let cpuCount = os.cpus().length + 1; |
| 14 | +if ("TRAVIS" in process.env && "CI" in process.env) { |
| 15 | + // fixed thread count for CI |
| 16 | + cpuCount = 8; |
| 17 | +} |
| 18 | +const testFiles: string[] = glob.sync("./test/**/*.spec.ts"); |
| 19 | +const pool = new Pool(cpuCount); |
| 20 | +let jobCounter = 0; |
| 21 | + |
| 22 | +const fileArrToString = (fileArr: string[]) => |
| 23 | + fileArr.map(val => path.basename(val).replace(".spec.ts", "")).join(", "); |
| 24 | + |
| 25 | +console.log( |
| 26 | + `Running tests: ${fileArrToString(testFiles)} with ${cpuCount} threads`); |
| 27 | + |
| 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 | +} |
| 52 | + |
| 53 | +pool.on("finished", () => { |
| 54 | + console.log("Everything done, shutting down the thread pool."); |
| 55 | + pool.killAll(); |
| 56 | +}); |
0 commit comments