From 7d2a7710c34f67e01e38ac32ba95bcb9e24388ac Mon Sep 17 00:00:00 2001 From: BethGriggs Date: Thu, 29 Dec 2016 16:55:51 +0000 Subject: [PATCH 1/2] test: refactor several parallel/test-timer tests Change var to const/let. Simplify test-timers-uncaught-exception. --- test/parallel/test-timers-args.js | 6 ++-- test/parallel/test-timers-immediate-queue.js | 12 +++---- .../parallel/test-timers-non-integer-delay.js | 12 +++---- test/parallel/test-timers-ordering.js | 14 ++++---- .../test-timers-uncaught-exception.js | 36 +++++-------------- 5 files changed, 30 insertions(+), 50 deletions(-) diff --git a/test/parallel/test-timers-args.js b/test/parallel/test-timers-args.js index 4638cb39ccf6b9..1ba44d8bcf3664 100644 --- a/test/parallel/test-timers-args.js +++ b/test/parallel/test-timers-args.js @@ -7,7 +7,7 @@ function range(n) { } function timeout(nargs) { - var args = range(nargs); + const args = range(nargs); setTimeout.apply(null, [callback, 1].concat(args)); function callback() { @@ -17,8 +17,8 @@ function timeout(nargs) { } function interval(nargs) { - var args = range(nargs); - var timer = setTimeout.apply(null, [callback, 1].concat(args)); + const args = range(nargs); + const timer = setTimeout.apply(null, [callback, 1].concat(args)); function callback() { clearInterval(timer); diff --git a/test/parallel/test-timers-immediate-queue.js b/test/parallel/test-timers-immediate-queue.js index fd952301500af9..f53f18140f46df 100644 --- a/test/parallel/test-timers-immediate-queue.js +++ b/test/parallel/test-timers-immediate-queue.js @@ -6,15 +6,13 @@ const assert = require('assert'); // but immediates queued while processing the current queue should happen // on the next turn of the event loop. -// in v0.10 hit should be 1, because we only process one cb per turn -// in v0.11 and beyond it should be the exact same size of QUEUE -// if we're letting things recursively add to the immediate QUEUE hit will be -// > QUEUE +// hit should be the exact same size of QUEUE, if we're letting things +// recursively add to the immediate QUEUE hit will be > QUEUE -var ticked = false; +let ticked = false; -var hit = 0; -var QUEUE = 1000; +let hit = 0; +const QUEUE = 1000; function run() { if (hit === 0) diff --git a/test/parallel/test-timers-non-integer-delay.js b/test/parallel/test-timers-non-integer-delay.js index b42053db3b1895..cd7fa5e661dc81 100644 --- a/test/parallel/test-timers-non-integer-delay.js +++ b/test/parallel/test-timers-non-integer-delay.js @@ -1,4 +1,6 @@ 'use strict'; +require('../common'); + /* * This test makes sure that non-integer timer delays do not make the process * hang. See https://github.com/joyent/node/issues/8065 and @@ -15,13 +17,11 @@ * it 100%. */ -require('../common'); - -var TIMEOUT_DELAY = 1.1; -var NB_TIMEOUTS_FIRED = 50; +const TIMEOUT_DELAY = 1.1; +const NB_TIMEOUTS_FIRED = 50; -var nbTimeoutFired = 0; -var interval = setInterval(function() { +let nbTimeoutFired = 0; +const interval = setInterval(function() { ++nbTimeoutFired; if (nbTimeoutFired === NB_TIMEOUTS_FIRED) { clearInterval(interval); diff --git a/test/parallel/test-timers-ordering.js b/test/parallel/test-timers-ordering.js index 79b16338e65d79..853f80a66a2384 100644 --- a/test/parallel/test-timers-ordering.js +++ b/test/parallel/test-timers-ordering.js @@ -1,22 +1,22 @@ 'use strict'; require('../common'); const assert = require('assert'); -var Timer = process.binding('timer_wrap').Timer; -var N = 30; +const Timer = process.binding('timer_wrap').Timer; +const N = 30; -var last_i = 0; -var last_ts = 0; +let last_i = 0; +let last_ts = 0; -var f = function(i) { +const f = function(i) { if (i <= N) { // check order - assert.equal(i, last_i + 1, 'order is broken: ' + i + ' != ' + + assert.strictEqual(i, last_i + 1, 'order is broken: ' + i + ' != ' + last_i + ' + 1'); last_i = i; // check that this iteration is fired at least 1ms later than the previous - var now = Timer.now(); + const now = Timer.now(); console.log(i, now); assert(now >= last_ts + 1, 'current ts ' + now + ' < prev ts ' + last_ts + ' + 1'); diff --git a/test/parallel/test-timers-uncaught-exception.js b/test/parallel/test-timers-uncaught-exception.js index 84dae65af45c13..49ce0bf4184a6c 100644 --- a/test/parallel/test-timers-uncaught-exception.js +++ b/test/parallel/test-timers-uncaught-exception.js @@ -1,40 +1,22 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); - -var exceptions = 0; -var timer1 = 0; -var timer2 = 0; +const errorMsg = 'BAM!'; // the first timer throws... -console.error('set first timer'); -setTimeout(function() { - console.error('first timer'); - timer1++; - throw new Error('BAM!'); -}, 100); +setTimeout(common.mustCall(function() { + throw new Error(errorMsg); +}), 100); // ...but the second one should still run -console.error('set second timer'); -setTimeout(function() { - console.error('second timer'); - assert.equal(timer1, 1); - timer2++; -}, 100); +setTimeout(common.mustCall(function() {}), 100); function uncaughtException(err) { - console.error('uncaught handler'); - assert.equal(err.message, 'BAM!'); - exceptions++; + assert.strictEqual(err.message, errorMsg); } -process.on('uncaughtException', uncaughtException); -var exited = false; +process.on('uncaughtException', common.mustCall(uncaughtException)); + process.on('exit', function() { - assert(!exited); - exited = true; process.removeListener('uncaughtException', uncaughtException); - assert.equal(exceptions, 1); - assert.equal(timer1, 1); - assert.equal(timer2, 1); }); From ff0f0c8e60ac7335181a1db536d68c2dea51c492 Mon Sep 17 00:00:00 2001 From: BethGriggs Date: Tue, 3 Jan 2017 14:31:18 +0000 Subject: [PATCH 2/2] fixup! test: refactor several parallel/test-timer tests --- test/parallel/test-timers-uncaught-exception.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-timers-uncaught-exception.js b/test/parallel/test-timers-uncaught-exception.js index 49ce0bf4184a6c..31a2d7eccd8105 100644 --- a/test/parallel/test-timers-uncaught-exception.js +++ b/test/parallel/test-timers-uncaught-exception.js @@ -6,17 +6,13 @@ const errorMsg = 'BAM!'; // the first timer throws... setTimeout(common.mustCall(function() { throw new Error(errorMsg); -}), 100); +}), 1); // ...but the second one should still run -setTimeout(common.mustCall(function() {}), 100); +setTimeout(common.mustCall(function() {}), 1); function uncaughtException(err) { assert.strictEqual(err.message, errorMsg); } process.on('uncaughtException', common.mustCall(uncaughtException)); - -process.on('exit', function() { - process.removeListener('uncaughtException', uncaughtException); -});