Skip to content

Commit 41284fb

Browse files
cjihrigMylesBorins
authored andcommitted
test: cover thrown errors from exec() kill
This commit adds code coverage for the scenario where exec() kills a child process, but the call to ChildProcess#kill() throws an exception. PR-URL: #11038 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 7347860 commit 41284fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
// Flags: --expose_internals
3+
constcommon=require('../common');
4+
constassert=require('assert');
5+
constcp=require('child_process');
6+
constinternalCp=require('internal/child_process');
7+
8+
if(process.argv[2]==='child'){
9+
// Keep the process alive and printing to stdout.
10+
setInterval(()=>{console.log('foo');},1);
11+
}else{
12+
// Monkey patch ChildProcess#kill() to kill the process and then throw.
13+
constkill=internalCp.ChildProcess.prototype.kill;
14+
15+
internalCp.ChildProcess.prototype.kill=function(){
16+
kill.apply(this,arguments);
17+
thrownewError('mock error');
18+
};
19+
20+
constcmd=`${process.execPath}${__filename} child`;
21+
constoptions={maxBuffer: 0};
22+
constchild=cp.exec(cmd,options,common.mustCall((err,stdout,stderr)=>{
23+
// Verify that if ChildProcess#kill() throws, the error is reported.
24+
assert(/^Error:mockerror$/.test(err));
25+
assert.strictEqual(stdout,'');
26+
assert.strictEqual(stderr,'');
27+
assert.strictEqual(child.killed,true);
28+
}));
29+
}

0 commit comments

Comments
(0)