Skip to content

Commit

Permalink
test: increase coverage of timers
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Feb 11, 2017
1 parent f8cdaaa commit e43961b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 68 deletions.
9 changes: 5 additions & 4 deletions test/parallel/test-timers-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
24 changes: 11 additions & 13 deletions test/parallel/test-timers-clearImmediate.js
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 7 additions & 13 deletions test/parallel/test-timers-immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
const common = require('../common');
const assert = require('assert');

let immediateC;
let immediateD;

let mainFinished = false;

setImmediate(common.mustCall(function() {
Expand All @@ -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;
58 changes: 20 additions & 38 deletions test/parallel/test-timers-throw-when-cb-not-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,43 @@
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() {
setInterval(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() {
setImmediate(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);

0 comments on commit e43961b

Please sign in to comment.