Skip to content

Commit 4fcd6a5

Browse files
authored
Threaded tests (#145)
* Added experiemental test-threaded command * Improved workload distrubution * Travis test * Fixed Travis script * Fixed thread count for CI * Fixed work distrubution * Fixed codecov beeing invoked twice * Removed threaded tests from ci config. Since CI doesnt really benifit from multiple threads. * Fixed Typo and some formatting * Added some build and test info to readme
1 parent 452aab9 commit 4fcd6a5

File tree

7 files changed

+158
-4
lines changed

7 files changed

+158
-4
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ install:
66
script:
77
- npm run build
88
# Testing & Coverage
9-
- npm test
10-
after_success:
119
- npm run coverage
10+
after_success:
11+
- codecov
1212
deploy:
1313
provider: npm
1414
email: 'lorenz.junglas@student.kit.edu'

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ The real power of this transpiler is usage together with good declarations for t
4545
- [Dota 2 Modding](https://github.com/ModDota/API/tree/master/declarations/server)
4646
- [Defold Game Engine Scripting](https://github.com/dasannikov/DefoldTypeScript/blob/master/defold.d.ts)
4747

48+
## Building & Tests
49+
50+
`npm run build` to build the project.
51+
52+
`npm run test` to run tests.
53+
54+
`npm run test-threaded` runs test in parallel, faster but less detailed output.
55+
56+
`npm run coverage` or `npm run coverage-html` to generate a coverage report.
57+
4858
## Sublime Text integration
4959
This compiler works great in combination with the [Sublime Text Typescript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin) (available through the package manager as `TypeScript`).
5060

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"scripts": {
1313
"build": "tsc -p tsconfig.json",
1414
"test": "tslint -p . && ts-node ./test/runner.ts",
15-
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov",
15+
"test-threaded": "tslint -p . && ts-node ./test/threaded_runner.ts",
16+
"coverage-threaded": "nyc npm run test-threaded && nyc report --reporter=text-lcov > coverage.lcov",
17+
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov",
1618
"coverage-html": "nyc npm test && nyc report --reporter=html",
1719
"release-patch": "npm version patch",
1820
"release-minor": "npm version minor",
@@ -40,13 +42,17 @@
4042
"yargs": "^11.1.0"
4143
},
4244
"devDependencies": {
45+
"@types/glob": "^5.0.35",
4346
"@types/node": "^9.6.23",
4447
"@types/yargs": "^11.0.0",
4548
"alsatian": "^2.2.1",
49+
"circular-json": "^0.5.5",
4650
"codecov": "3.0.2",
4751
"deep-equal": "^1.0.1",
4852
"fengari": "^0.1.2",
53+
"glob": "^7.1.2",
4954
"nyc": "^11.9.0",
55+
"threads": "^0.12.0",
5056
"ts-node": "^7.0.0",
5157
"tslint": "^5.10.0",
5258
"tslint-override": "^0.1.2"

test/test_thread.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { MatchError, TestRunner, TestSet, TestOutcome } from "alsatian";
2+
import * as JSON from "circular-json";
3+
4+
module.exports = function(input, done) {
5+
const testSet = TestSet.create();
6+
testSet.addTestsFromFiles(input.files);
7+
const testRunner = new TestRunner();
8+
9+
testRunner.onTestComplete((result) => {
10+
if (result.outcome === TestOutcome.Fail) {
11+
if (result.error instanceof MatchError) {
12+
console.log(`Test ${result.testFixture.description}, ${result.test.key}(${JSON.stringify(result.testCase.caseArguments)}) Failed!`)
13+
console.log(" ---\n" +
14+
' message: "' +
15+
result.error.message +
16+
'"\n' +
17+
" severity: fail\n" +
18+
" data:\n" +
19+
" got: " +
20+
result.error.actual +
21+
"\n" +
22+
" expect: " +
23+
result.error.expected +
24+
"\n");
25+
}
26+
}
27+
})
28+
29+
testRunner.run(testSet)
30+
.then((results) => done(results, input))
31+
};

test/threaded_runner.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
});

test/unit/string.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class StringTests {
1818
@TestCase([65, 66])
1919
@TestCase([65, 66, 67])
2020
@Test("String.fromCharCode")
21-
public stringFromCharcode(inp: number[], expected: string) {
21+
public stringFromCharcode(inp: number[]) {
2222
// Transpile
2323
const lua = util.transpileString(
2424
`return String.fromCharCode(${inp.toString()})`

0 commit comments

Comments
 (0)