Skip to content

Commit deef2f6

Browse files
cjihrigevanlucas
authored andcommitted
test: add child_process.exec() timeout coverage
This commit adds coverage for the timeout option used by child_process exec() and execFile(). PR-URL: #9208 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 5e138fe commit deef2f6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constcp=require('child_process');
5+
6+
if(process.argv[2]==='child'){
7+
setTimeout(()=>{
8+
// The following console statements are part of the test.
9+
console.log('child stdout');
10+
console.error('child stderr');
11+
},common.platformTimeout(1000));
12+
return;
13+
}
14+
15+
constcmd=`${process.execPath}${__filename} child`;
16+
17+
// Test the case where a timeout is set, and it expires.
18+
cp.exec(cmd,{timeout: 1},common.mustCall((err,stdout,stderr)=>{
19+
assert.strictEqual(err.killed,true);
20+
assert.strictEqual(err.code,null);
21+
assert.strictEqual(err.signal,'SIGTERM');
22+
assert.strictEqual(err.cmd,cmd);
23+
assert.strictEqual(stdout.trim(),'');
24+
assert.strictEqual(stderr.trim(),'');
25+
}));
26+
27+
// Test with a different kill signal.
28+
cp.exec(cmd,{
29+
timeout: 1,
30+
killSignal: 'SIGKILL'
31+
},common.mustCall((err,stdout,stderr)=>{
32+
assert.strictEqual(err.killed,true);
33+
assert.strictEqual(err.code,null);
34+
assert.strictEqual(err.signal,'SIGKILL');
35+
assert.strictEqual(err.cmd,cmd);
36+
assert.strictEqual(stdout.trim(),'');
37+
assert.strictEqual(stderr.trim(),'');
38+
}));
39+
40+
// Test the case where a timeout is set, but not expired.
41+
cp.exec(cmd,{timeout: 2**30},common.mustCall((err,stdout,stderr)=>{
42+
assert.ifError(err);
43+
assert.strictEqual(stdout.trim(),'child stdout');
44+
assert.strictEqual(stderr.trim(),'child stderr');
45+
}));

0 commit comments

Comments
(0)