Skip to content

Commit b83b517

Browse files
cjihrigMyles Borins
authored and
Myles Borins
committed
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 0fdfba8 commit b83b517

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cp = 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+
const cmd = `${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: Math.pow(2, 30) },
42+
common.mustCall((err, stdout, stderr) => {
43+
assert.ifError(err);
44+
assert.strictEqual(stdout.trim(), 'child stdout');
45+
assert.strictEqual(stderr.trim(), 'child stderr');
46+
})
47+
);

0 commit comments

Comments
 (0)