From e43961bc9d4da18704adea1c309809e4a9ee2c61 Mon Sep 17 00:00:00 2001 From: DavidCai Date: Sat, 11 Feb 2017 16:04:25 +0800 Subject: [PATCH] test: increase coverage of timers --- test/parallel/test-timers-args.js | 9 +-- test/parallel/test-timers-clearImmediate.js | 24 ++++---- test/parallel/test-timers-immediate.js | 20 +++---- .../test-timers-throw-when-cb-not-function.js | 58 +++++++------------ 4 files changed, 43 insertions(+), 68 deletions(-) diff --git a/test/parallel/test-timers-args.js b/test/parallel/test-timers-args.js index 1ba44d8bcf3664..bbe55c828e032e 100644 --- a/test/parallel/test-timers-args.js +++ b/test/parallel/test-timers-args.js @@ -8,21 +8,22 @@ function range(n) { function timeout(nargs) { const args = range(nargs); - setTimeout.apply(null, [callback, 1].concat(args)); + const timer = setTimeout.apply(null, [callback, 1].concat(args)); function callback() { - assert.deepStrictEqual([].slice.call(arguments), args); + clearTimeout(timer); + assert.deepStrictEqual(Array.from(arguments), args); if (nargs < 128) timeout(nargs + 1); } } function interval(nargs) { const args = range(nargs); - const timer = setTimeout.apply(null, [callback, 1].concat(args)); + const timer = setInterval.apply(null, [callback, 1].concat(args)); function callback() { clearInterval(timer); - assert.deepStrictEqual([].slice.call(arguments), args); + assert.deepStrictEqual(Array.from(arguments), args); if (nargs < 128) interval(nargs + 1); } } diff --git a/test/parallel/test-timers-clearImmediate.js b/test/parallel/test-timers-clearImmediate.js index c49fd7622f4ab6..bcdcc0750e9e81 100644 --- a/test/parallel/test-timers-clearImmediate.js +++ b/test/parallel/test-timers-clearImmediate.js @@ -1,18 +1,16 @@ 'use strict'; -require('../common'); -const assert = require('assert'); +const common = require('../common'); -const N = 3; -let count = 0; -function next() { - const immediate = setImmediate(function() { +// run clearImmediate in the callback. +for (let i = 0; i < 3; ++i) { + const immediate = setImmediate(common.mustCall(function() { clearImmediate(immediate); - ++count; - }); + })); } -for (let i = 0; i < N; ++i) - next(); -process.on('exit', () => { - assert.strictEqual(count, N, `Expected ${N} immediate callback executions`); -}); +// run clearImmediate before the callback. +for (let i = 0; i < 3; ++i) { + const immediate = setImmediate(common.mustNotCall(function() {})); + + clearImmediate(immediate); +} diff --git a/test/parallel/test-timers-immediate.js b/test/parallel/test-timers-immediate.js index 11702cdef9da76..755c92c8a7e77d 100644 --- a/test/parallel/test-timers-immediate.js +++ b/test/parallel/test-timers-immediate.js @@ -2,9 +2,6 @@ const common = require('../common'); const assert = require('assert'); -let immediateC; -let immediateD; - let mainFinished = false; setImmediate(common.mustCall(function() { @@ -14,17 +11,14 @@ setImmediate(common.mustCall(function() { const immediateB = setImmediate(common.mustNotCall()); -setImmediate(function(x, y, z) { - immediateC = [x, y, z]; -}, 1, 2, 3); +for (let n = 1; n <= 5; n++) { + const args = new Array(n).fill(0).map((_, i) => i); -setImmediate(function(x, y, z, a, b) { - immediateD = [x, y, z, a, b]; -}, 1, 2, 3, 4, 5); + const callback = common.mustCall(function() { + assert.deepStrictEqual(Array.from(arguments), args); + }); -process.on('exit', function() { - assert.deepStrictEqual(immediateC, [1, 2, 3], 'immediateC args should match'); - assert.deepStrictEqual(immediateD, [1, 2, 3, 4, 5], '5 args should match'); -}); + setImmediate.apply(null, [callback].concat(args)); +} mainFinished = true; diff --git a/test/parallel/test-timers-throw-when-cb-not-function.js b/test/parallel/test-timers-throw-when-cb-not-function.js index 2aff904f06a500..a991ccd2fdc99b 100644 --- a/test/parallel/test-timers-throw-when-cb-not-function.js +++ b/test/parallel/test-timers-throw-when-cb-not-function.js @@ -2,25 +2,20 @@ require('../common'); const assert = require('assert'); +const error = /^TypeError: "callback" argument must be a function$/; + function doSetTimeout(callback, after) { return function() { setTimeout(callback, after); }; } -assert.throws(doSetTimeout('foo'), - /"callback" argument must be a function/); -assert.throws(doSetTimeout({foo: 'bar'}), - /"callback" argument must be a function/); -assert.throws(doSetTimeout(), - /"callback" argument must be a function/); -assert.throws(doSetTimeout(undefined, 0), - /"callback" argument must be a function/); -assert.throws(doSetTimeout(null, 0), - /"callback" argument must be a function/); -assert.throws(doSetTimeout(false, 0), - /"callback" argument must be a function/); - +assert.throws(doSetTimeout('foo'), error); +assert.throws(doSetTimeout({foo: 'bar'}), error); +assert.throws(doSetTimeout(), error); +assert.throws(doSetTimeout(undefined, 0), error); +assert.throws(doSetTimeout(null, 0), error); +assert.throws(doSetTimeout(false, 0), error); function doSetInterval(callback, after) { return function() { @@ -28,19 +23,12 @@ function doSetInterval(callback, after) { }; } -assert.throws(doSetInterval('foo'), - /"callback" argument must be a function/); -assert.throws(doSetInterval({foo: 'bar'}), - /"callback" argument must be a function/); -assert.throws(doSetInterval(), - /"callback" argument must be a function/); -assert.throws(doSetInterval(undefined, 0), - /"callback" argument must be a function/); -assert.throws(doSetInterval(null, 0), - /"callback" argument must be a function/); -assert.throws(doSetInterval(false, 0), - /"callback" argument must be a function/); - +assert.throws(doSetInterval('foo'), error); +assert.throws(doSetInterval({foo: 'bar'}), error); +assert.throws(doSetInterval(), error); +assert.throws(doSetInterval(undefined, 0), error); +assert.throws(doSetInterval(null, 0), error); +assert.throws(doSetInterval(false, 0), error); function doSetImmediate(callback, after) { return function() { @@ -48,15 +36,9 @@ function doSetImmediate(callback, after) { }; } -assert.throws(doSetImmediate('foo'), - /"callback" argument must be a function/); -assert.throws(doSetImmediate({foo: 'bar'}), - /"callback" argument must be a function/); -assert.throws(doSetImmediate(), - /"callback" argument must be a function/); -assert.throws(doSetImmediate(undefined, 0), - /"callback" argument must be a function/); -assert.throws(doSetImmediate(null, 0), - /"callback" argument must be a function/); -assert.throws(doSetImmediate(false, 0), - /"callback" argument must be a function/); +assert.throws(doSetImmediate('foo'), error); +assert.throws(doSetImmediate({foo: 'bar'}), error); +assert.throws(doSetImmediate(), error); +assert.throws(doSetImmediate(undefined, 0), error); +assert.throws(doSetImmediate(null, 0), error); +assert.throws(doSetImmediate(false, 0), error);