Skip to content

Commit 5a53900

Browse files
author
Vadim Demedes
committed
Merge pull request #120 from jamestalmage/jt-kill-forked-processes
Kill every forked process after tests are finished
2 parents 10dccb9 + 4d907aa commit 5a53900

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

cli.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ function sum(arr, key) {
133133
return result;
134134
}
135135

136-
function exit(results) {
136+
function exit(testRun) {
137+
var results = testRun.results;
137138
// assemble stats from all tests
138139
var stats = results.map(function (result) {
139140
return result.stats;
@@ -158,7 +159,10 @@ function exit(results) {
158159
// correctly flush the output when multiple test files
159160
process.stdout.write('');
160161

161-
process.exit(failed > 0 ? 1 : 0);
162+
// wait for the child processes to exit
163+
testRun.childProcesses.finally(function () {
164+
process.exit(failed > 0 ? 1 : 0);
165+
});
162166
}
163167

164168
function init(files) {
@@ -178,7 +182,14 @@ function init(files) {
178182

179183
var tests = files.map(run);
180184

181-
return Promise.all(tests);
185+
return Promise.all(tests).then(function (results) {
186+
return {
187+
results: results,
188+
childProcesses: Promise.map(tests, function (test) {
189+
return test.kill();
190+
})
191+
};
192+
});
182193
});
183194
}
184195

lib/fork.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ module.exports = function (args) {
1818

1919
var ps = childProcess.fork(babel, args, options);
2020

21+
var killed = false;
22+
var killedPromise = new Promise(function (resolve) {
23+
ps.on('exit', function (code) {
24+
killed = true;
25+
resolve(code);
26+
});
27+
});
28+
29+
function kill() {
30+
if (!killed) {
31+
ps.kill();
32+
}
33+
return killedPromise;
34+
}
35+
2136
var promise = new Promise(function (resolve, reject) {
2237
ps.on('results', function (results) {
2338
resolve(results);
@@ -31,6 +46,8 @@ module.exports = function (args) {
3146
});
3247
});
3348

49+
promise.kill = kill;
50+
3451
// emit `test` and `stats` events
3552
ps.on('message', function (event) {
3653
event.data.file = file;

test/fixture/long-running.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const test = require('../../');
3+
4+
test('long running', function (t) {
5+
t.plan(1);
6+
7+
setTimeout(function () {
8+
console.log('I\'m gonna live forever!!');
9+
}, 15000);
10+
11+
t.ok(true);
12+
});

test/fork.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ test('rejects on error and streams output', function (t) {
3939
t.end();
4040
});
4141
});
42+
43+
test('result.kill forcibly kills process', function (t) {
44+
t.plan(1);
45+
var start = Date.now();
46+
var promise = fork(fixture('long-running.js'))
47+
.on('exit', function () {
48+
t.ok(Date.now() - start < 10000);
49+
});
50+
51+
promise
52+
.then(function () {
53+
promise.kill();
54+
});
55+
});

0 commit comments

Comments
 (0)