From 3368a98c74e0cf308865e1c3f36835ce71d3150b Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 20 Oct 2016 11:38:59 -0400 Subject: [PATCH] test: add child_process.exec() timeout coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds coverage for the timeout option used by child_process exec() and execFile(). PR-URL: https://github.com/nodejs/node/pull/9208 Reviewed-By: Santiago Gimeno Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso --- .../test-child-process-exec-timeout.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/parallel/test-child-process-exec-timeout.js diff --git a/test/parallel/test-child-process-exec-timeout.js b/test/parallel/test-child-process-exec-timeout.js new file mode 100644 index 00000000000000..25aba6de2780e8 --- /dev/null +++ b/test/parallel/test-child-process-exec-timeout.js @@ -0,0 +1,45 @@ +'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'); +}));