From c45e347d5ce54860534f74a94572e0e01f8cbe9f Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 6 Aug 2017 14:16:05 +0800 Subject: [PATCH 1/3] test: fix hijackStdout behavior in console `console.log` and some other function will swallow the exception in `stdout.write`. So an asynchronous exception is needed, or `common.hijackStdout` won't detect some exception. Refs: https://github.com/nodejs/node/blob/v8.2.1/lib/console.js#L87 --- test/common/index.js | 9 ++++++++- test/parallel/test-common.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/test/common/index.js b/test/common/index.js index a5ca4cec576e74..3cbd9d7ffcb0f1 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -801,7 +801,14 @@ function hijackStdWritable(name, listener) { stream.writeTimes = 0; stream.write = function(data, callback) { - listener(data); + try { + listener(data); + } catch(e) { + process.nextTick(function() { + throw e; + }); + } + _write.call(stream, data, callback); stream.writeTimes++; }; diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 7fee3e0b841437..84b491fddea5ca 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -108,3 +108,23 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ]; common[`restoreStd${txt}`](); assert.strictEqual(originalWrite, stream.write); }); + +// hijackStderr and hijackStdout again +// for console +[ 'err', 'out' ].forEach((txt) => { + common[`hijackStd${txt}`](common.mustCall(function(data) { + assert.strictEqual(data, 'test\n'); + + // throw an error + throw new Error(`console ${txt} error`); + })); + + console[txt === 'err' ? 'error' : 'log']('test'); + common[`restoreStd${txt}`](); +}); + +let uncaughtTimes = 0; +process.on('uncaughtException', common.mustCallAtLeast(function(e) { + assert.strictEqual(e instanceof Error, true); + assert.strictEqual(e.message, `console ${([ 'err', 'out' ])[uncaughtTimes++]} error`); +}, 2)); From 9dbe31bd7cad2f69010a1e0276ed46d630b6e9ce Mon Sep 17 00:00:00 2001 From: XadillaX Date: Thu, 10 Aug 2017 11:04:40 +0800 Subject: [PATCH 2/3] update after reviewing --- test/common/index.js | 6 ++---- test/parallel/test-common.js | 14 ++++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index 3cbd9d7ffcb0f1..acade1d858152c 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -803,10 +803,8 @@ function hijackStdWritable(name, listener) { stream.write = function(data, callback) { try { listener(data); - } catch(e) { - process.nextTick(function() { - throw e; - }); + } catch (e) { + process.nextTick(() => { throw e; }); } _write.call(stream, data, callback); diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 84b491fddea5ca..40d292fc742208 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -111,20 +111,22 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ]; // hijackStderr and hijackStdout again // for console -[ 'err', 'out' ].forEach((txt) => { - common[`hijackStd${txt}`](common.mustCall(function(data) { +[[ 'err', 'error' ], [ 'out', 'log' ]].forEach(([ type, method ]) => { + common[`hijackStd${type}`](common.mustCall(function(data) { assert.strictEqual(data, 'test\n'); // throw an error - throw new Error(`console ${txt} error`); + throw new Error(`console ${type} error`); })); - console[txt === 'err' ? 'error' : 'log']('test'); - common[`restoreStd${txt}`](); + console[method]('test'); + common[`restoreStd${type}`](); }); let uncaughtTimes = 0; process.on('uncaughtException', common.mustCallAtLeast(function(e) { assert.strictEqual(e instanceof Error, true); - assert.strictEqual(e.message, `console ${([ 'err', 'out' ])[uncaughtTimes++]} error`); + assert.strictEqual( + e.message, + `console ${([ 'err', 'out' ])[uncaughtTimes++]} error`); }, 2)); From 0ef550ce4a407eb97724ca1c0d81b36d17977ed4 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Fri, 11 Aug 2017 14:17:10 +0800 Subject: [PATCH 3/3] update after reviewing --- test/parallel/test-common.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 40d292fc742208..647c39d939f2aa 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -125,6 +125,7 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ]; let uncaughtTimes = 0; process.on('uncaughtException', common.mustCallAtLeast(function(e) { + assert.strictEqual(uncaughtTimes < 2, true); assert.strictEqual(e instanceof Error, true); assert.strictEqual( e.message,