Skip to content

Commit f24193d

Browse files
committed
fix #155: reuse subprocess
1 parent 810d602 commit f24193d

File tree

10 files changed

+108
-43
lines changed

10 files changed

+108
-43
lines changed

src/main/mocha.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default class MochaWrapper extends Mocha {
5252

5353
setMaxParallel(maxParallel: number) {
5454
this.maxParallel = maxParallel;
55+
return this;
5556
}
5657

5758
enableExitMode() {

test/index.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,13 @@ echo 'TESTCASE: --exit option support'
144144
test test/exit/index.js
145145
echo $?
146146
echo 'TESTCASE: --retries plus all tests fail'
147-
test/retries-all-fail/index.js
147+
test test/retries-all-fail/index.js
148148
echo $?
149149
echo 'TESTCASE: subprocess exits before sending an end message'
150-
test/no-subprocess-end/index.js
150+
test test/no-subprocess-end/index.js
151+
echo $?
152+
echo 'TESTCASE: subprocess reusage'
153+
test test/subprocess-reuse/index.js
151154
echo $?
152155

153156
if [ $SAUCE_USERNAME ] && [ $SAUCE_ACCESS_KEY ]; then

test/no-subprocess-end/easy.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/subprocess-reuse/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
counter.json

test/subprocess-reuse/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check that subprocesses are reused

test/subprocess-reuse/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
const assert = require('assert');
4+
const { writeFileSync } = require('fs');
5+
const { resolve } = require('path');
6+
const MochaParallelTests = require('../../dist/main/mocha').default;
7+
8+
const counterFilePath = resolve(__dirname, 'counter.json');
9+
const reporterPath = resolve(__dirname, '../util/silent-reporter.js');
10+
11+
process.on('unhandledRejection', (reason) => {
12+
console.error(reason.stack);
13+
process.exit(1);
14+
});
15+
16+
// initialize counter file
17+
writeFileSync(counterFilePath, '[]');
18+
19+
const mocha = new MochaParallelTests();
20+
mocha
21+
.reporter(reporterPath)
22+
.addFile(`${__dirname}/spec/1.spec.js`)
23+
.addFile(`${__dirname}/spec/2.spec.js`)
24+
.addFile(`${__dirname}/spec/3.spec.js`)
25+
.addFile(`${__dirname}/spec/4.spec.js`)
26+
.slow(5000)
27+
.setMaxParallel(2)
28+
.run()
29+
.on('end', function () {
30+
const pidsUsed = require(counterFilePath);
31+
assert.strictEqual(pidsUsed.length, 2, `Number of processes is wrong: ${pidsUsed.length}`);
32+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { writeFileSync } = require('fs');
2+
const { resolve } = require('path');
3+
4+
const updateCounterWithOwnPid = () => {
5+
const counterFilePath = resolve(__dirname, '../counter.json');
6+
const pids = new Set(require(counterFilePath));
7+
8+
pids.add(process.pid);
9+
writeFileSync(counterFilePath, JSON.stringify([...pids]));
10+
};
11+
12+
describe('suite', () => {
13+
it('case', (done) => {
14+
updateCounterWithOwnPid();
15+
setTimeout(done, 1000);
16+
});
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { writeFileSync } = require('fs');
2+
const { resolve } = require('path');
3+
4+
const updateCounterWithOwnPid = () => {
5+
const counterFilePath = resolve(__dirname, '../counter.json');
6+
const pids = new Set(require(counterFilePath));
7+
8+
pids.add(process.pid);
9+
writeFileSync(counterFilePath, JSON.stringify([...pids]));
10+
};
11+
12+
describe('suite', () => {
13+
it('case', (done) => {
14+
updateCounterWithOwnPid();
15+
setTimeout(done, 1000);
16+
});
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { writeFileSync } = require('fs');
2+
const { resolve } = require('path');
3+
4+
const updateCounterWithOwnPid = () => {
5+
const counterFilePath = resolve(__dirname, '../counter.json');
6+
const pids = new Set(require(counterFilePath));
7+
8+
pids.add(process.pid);
9+
writeFileSync(counterFilePath, JSON.stringify([...pids]));
10+
};
11+
12+
describe('suite', () => {
13+
it('case', (done) => {
14+
updateCounterWithOwnPid();
15+
setTimeout(done, 1000);
16+
});
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { writeFileSync } = require('fs');
2+
const { resolve } = require('path');
3+
4+
const updateCounterWithOwnPid = () => {
5+
const counterFilePath = resolve(__dirname, '../counter.json');
6+
const pids = new Set(require(counterFilePath));
7+
8+
pids.add(process.pid);
9+
writeFileSync(counterFilePath, JSON.stringify([...pids]));
10+
};
11+
12+
describe('suite', () => {
13+
it('case', (done) => {
14+
updateCounterWithOwnPid();
15+
setTimeout(done, 1000);
16+
});
17+
});

0 commit comments

Comments
 (0)