-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <santiago.gimeno@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
|
||
if (process.argv[2] === 'child') { | ||
setTimeout(() => { | ||
// The following console statements are part of the test. | ||
console.log('child stdout'); | ||
console.error('child stderr'); | ||
}, common.platformTimeout(1000)); | ||
return; | ||
} | ||
|
||
const cmd = `${process.execPath} ${__filename} child`; | ||
|
||
// Test the case where a timeout is set, and it expires. | ||
cp.exec(cmd, { timeout: 1 }, common.mustCall((err, stdout, stderr) => { | ||
assert.strictEqual(err.killed, true); | ||
assert.strictEqual(err.code, null); | ||
assert.strictEqual(err.signal, 'SIGTERM'); | ||
assert.strictEqual(err.cmd, cmd); | ||
assert.strictEqual(stdout.trim(), ''); | ||
assert.strictEqual(stderr.trim(), ''); | ||
})); | ||
|
||
// Test with a different kill signal. | ||
cp.exec(cmd, { | ||
timeout: 1, | ||
killSignal: 'SIGKILL' | ||
}, common.mustCall((err, stdout, stderr) => { | ||
assert.strictEqual(err.killed, true); | ||
assert.strictEqual(err.code, null); | ||
assert.strictEqual(err.signal, 'SIGKILL'); | ||
assert.strictEqual(err.cmd, cmd); | ||
assert.strictEqual(stdout.trim(), ''); | ||
assert.strictEqual(stderr.trim(), ''); | ||
})); | ||
|
||
// Test the case where a timeout is set, but not expired. | ||
cp.exec(cmd, { timeout: Math.pow(2, 30) }, | ||
common.mustCall((err, stdout, stderr) => { | ||
assert.ifError(err); | ||
assert.strictEqual(stdout.trim(), 'child stdout'); | ||
assert.strictEqual(stderr.trim(), 'child stderr'); | ||
}) | ||
); |