Skip to content

Commit

Permalink
fix #155: reuse subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
1999 committed May 31, 2018
1 parent 810d602 commit fdb0f26
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/main/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class MochaWrapper extends Mocha {

setMaxParallel(maxParallel: number) {
this.maxParallel = maxParallel;
return this;
}

enableExitMode() {
Expand Down
3 changes: 3 additions & 0 deletions test/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ echo $?
echo 'TESTCASE: subprocess exits before sending an end message'
test/no-subprocess-end/index.js
echo $?
echo 'TESTCASE: subprocess reusage'
test/subprocess-reuse/index.js
echo $?

if [ $SAUCE_USERNAME ] && [ $SAUCE_ACCESS_KEY ]; then
echo 'TESTCASE: selenium-webdriver'
Expand Down
41 changes: 0 additions & 41 deletions test/no-subprocess-end/easy.js

This file was deleted.

1 change: 1 addition & 0 deletions test/subprocess-reuse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
counter.json
1 change: 1 addition & 0 deletions test/subprocess-reuse/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check that subprocesses are reused
32 changes: 32 additions & 0 deletions test/subprocess-reuse/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

const assert = require('assert');
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const MochaParallelTests = require('../../dist/main/mocha').default;

const counterFilePath = resolve(__dirname, 'counter.json');
const reporterPath = resolve(__dirname, '../util/silent-reporter.js');

process.on('unhandledRejection', (reason) => {
console.error(reason.stack);
process.exit(1);
});

// initialize counter file
writeFileSync(counterFilePath, '[]');

const mocha = new MochaParallelTests();
mocha
.reporter(reporterPath)
.addFile(`${__dirname}/spec/1.spec.js`)
.addFile(`${__dirname}/spec/2.spec.js`)
.addFile(`${__dirname}/spec/3.spec.js`)
.addFile(`${__dirname}/spec/4.spec.js`)
.slow(5000)
.setMaxParallel(2)
.run()
.on('end', function () {
const pidsUsed = require(counterFilePath);
assert.strictEqual(pidsUsed.length, 2, `Number of processes is wrong: ${pidsUsed.length}`);
});
17 changes: 17 additions & 0 deletions test/subprocess-reuse/spec/1.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');

const updateCounterWithOwnPid = () => {
const counterFilePath = resolve(__dirname, '../counter.json');
const pids = new Set(require(counterFilePath));

pids.add(process.pid);
writeFileSync(counterFilePath, JSON.stringify([...pids]));
};

describe('suite', () => {
it('case', (done) => {
updateCounterWithOwnPid();
setTimeout(done, 1000);
});
});
17 changes: 17 additions & 0 deletions test/subprocess-reuse/spec/2.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');

const updateCounterWithOwnPid = () => {
const counterFilePath = resolve(__dirname, '../counter.json');
const pids = new Set(require(counterFilePath));

pids.add(process.pid);
writeFileSync(counterFilePath, JSON.stringify([...pids]));
};

describe('suite', () => {
it('case', (done) => {
updateCounterWithOwnPid();
setTimeout(done, 1000);
});
});
17 changes: 17 additions & 0 deletions test/subprocess-reuse/spec/3.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');

const updateCounterWithOwnPid = () => {
const counterFilePath = resolve(__dirname, '../counter.json');
const pids = new Set(require(counterFilePath));

pids.add(process.pid);
writeFileSync(counterFilePath, JSON.stringify([...pids]));
};

describe('suite', () => {
it('case', (done) => {
updateCounterWithOwnPid();
setTimeout(done, 1000);
});
});
17 changes: 17 additions & 0 deletions test/subprocess-reuse/spec/4.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');

const updateCounterWithOwnPid = () => {
const counterFilePath = resolve(__dirname, '../counter.json');
const pids = new Set(require(counterFilePath));

pids.add(process.pid);
writeFileSync(counterFilePath, JSON.stringify([...pids]));
};

describe('suite', () => {
it('case', (done) => {
updateCounterWithOwnPid();
setTimeout(done, 1000);
});
});

0 comments on commit fdb0f26

Please sign in to comment.