File tree Expand file tree Collapse file tree 4 files changed +57
-3
lines changed Expand file tree Collapse file tree 4 files changed +57
-3
lines changed Original file line number Diff line number Diff 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
164168function 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
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff 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+ } ) ;
You can’t perform that action at this time.
0 commit comments