Permalink
Browse files

test: improve multiple timers tests

PR-URL: #14616
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information...
jasnell authored and MylesBorins committed Aug 4, 2017
1 parent c88f99f commit fc49cf41ea4f375d5d8f313a16000ec411d06113
@@ -2,32 +2,23 @@
const common = require('../common');
const assert = require('assert');
let immediateB;
let immediateC;
let immediateD;
let mainFinished = false;
setImmediate(common.mustCall(function() {
assert.strictEqual(mainFinished, true);
clearImmediate(immediateB);
}));
immediateB = setImmediate(function() {
let immediateB = setImmediate(function() {
common.fail('this immediate should not run');
});
setImmediate(function(x, y, z) {
immediateC = [x, y, z];
}, 1, 2, 3);
setImmediate(function(x, y, z, a, b) {
immediateD = [x, y, z, a, b];
}, 1, 2, 3, 4, 5);
setImmediate(common.mustCall((...args) => {
assert.deepStrictEqual(args, [1, 2, 3]);
}), 1, 2, 3);
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(common.mustCall((...args) => {
assert.deepStrictEqual(args, [1, 2, 3, 4, 5]);
}), 1, 2, 3, 4, 5);
mainFinished = true;
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
/*
* This test makes sure that non-integer timer delays do not make the process
@@ -18,13 +18,11 @@ require('../common');
*/
const TIMEOUT_DELAY = 1.1;
const NB_TIMEOUTS_FIRED = 50;
let N = 50;
let nbTimeoutFired = 0;
const interval = setInterval(function() {
++nbTimeoutFired;
if (nbTimeoutFired === NB_TIMEOUTS_FIRED) {
const interval = setInterval(common.mustCall(() => {
if (--N === 0) {
clearInterval(interval);
process.exit(0);
}
}, TIMEOUT_DELAY);
}, N), TIMEOUT_DELAY);
@@ -6,6 +6,7 @@
const common = require('../common');
const net = require('net');
const Countdown = require('../common/countdown');
const clients = [];
@@ -19,7 +20,7 @@ const server = net.createServer(function onClient(client) {
* the list of unref timers when traversing it, and exposes the
* original issue in joyent/node#8897.
*/
clients[0].setTimeout(1, function onTimeout() {
clients[0].setTimeout(1, () => {
clients[1].setTimeout(0);
clients[0].end();
clients[1].end();
@@ -31,19 +32,16 @@ const server = net.createServer(function onClient(client) {
}
});
server.listen(0, common.localhostIPv4, function() {
let nbClientsEnded = 0;
server.listen(0, common.localhostIPv4, common.mustCall(() => {
const countdown = new Countdown(2, common.mustCall(() => server.close()));
function addEndedClient(client) {
++nbClientsEnded;
if (nbClientsEnded === 2) {
server.close();
}
{
const client = net.connect({ port: server.address().port });
client.on('end', () => countdown.dec());
}
const client1 = net.connect({ port: this.address().port });
client1.on('end', addEndedClient);
const client2 = net.connect({ port: this.address().port });
client2.on('end', addEndedClient);
});
{
const client = net.connect({ port: server.address().port });
client.on('end', () => countdown.dec());
}
}));
@@ -1,27 +1,14 @@
'use strict';
require('../common');
const assert = require('assert');
const common = require('../common');
let called = 0;
let closed = 0;
const timeout = setTimeout(function() {
called++;
}, 10);
const timeout = setTimeout(common.mustCall(), 10);
timeout.unref();
// Wrap `close` method to check if the handle was closed
const close = timeout._handle.close;
timeout._handle.close = function() {
closed++;
timeout._handle.close = common.mustCall(function() {
return close.apply(this, arguments);
};
});
// Just to keep process alive and let previous timer's handle die
setTimeout(function() {
}, 50);
process.on('exit', function() {
assert.strictEqual(called, 1);
assert.strictEqual(closed, 1);
});
setTimeout(() => {}, 50);
@@ -1,76 +1,58 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
let interval_fired = false;
let timeout_fired = false;
let unref_interval = false;
let unref_timer = false;
let unref_callbacks = 0;
let checks = 0;
const LONG_TIME = 10 * 1000;
const SHORT_TIME = 100;
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
setTimeout(() => {}, 10).unref().ref().unref();
}, 'ref and unref are chainable');
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
setInterval(() => {}, 10).unref().ref().unref();
}, 'ref and unref are chainable');
setInterval(function() {
interval_fired = true;
}, LONG_TIME).unref();
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref();
setTimeout(function() {
timeout_fired = true;
}, LONG_TIME).unref();
const interval = setInterval(function() {
const interval = setInterval(common.mustCall(() => {
unref_interval = true;
clearInterval(interval);
}, SHORT_TIME);
}), SHORT_TIME);
interval.unref();
setTimeout(function() {
setTimeout(common.mustCall(() => {
unref_timer = true;
}, SHORT_TIME).unref();
}), SHORT_TIME).unref();
const check_unref = setInterval(function() {
const check_unref = setInterval(() => {
if (checks > 5 || (unref_interval && unref_timer))
clearInterval(check_unref);
checks += 1;
}, 100);
setTimeout(function() {
unref_callbacks++;
this.unref();
}, SHORT_TIME);
{
const timeout =
setTimeout(common.mustCall(() => {
timeout.unref();
}), SHORT_TIME);
}
// Should not timeout the test
setInterval(function() {
this.unref();
}, SHORT_TIME);
{
// Should not timeout the test
const timeout =
setInterval(() => timeout.unref(), SHORT_TIME);
}
// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
{
const t = setInterval(() => {}, 1);
process.nextTick(t.unref.bind({}));
process.nextTick(t.unref.bind(t));
}
process.on('exit', function() {
assert.strictEqual(interval_fired, false,
'Interval should not fire');
assert.strictEqual(timeout_fired, false,
'Timeout should not fire');
assert.strictEqual(unref_timer, true,
'An unrefd timeout should still fire');
assert.strictEqual(unref_interval, true,
'An unrefd interval should still fire');
assert.strictEqual(unref_callbacks, 1,
'Callback should only run once');
});
@@ -5,23 +5,20 @@
const common = require('../common');
const TEST_DURATION = common.platformTimeout(1000);
const N = 3;
let nbIntervalFired = 0;
let N = 3;
const keepOpen = setTimeout(() => {
console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N);
throw new Error('Test timed out. keepOpen was not canceled.');
}, TEST_DURATION);
const keepOpen =
setTimeout(
common.mustNotCall('Test timed out. keepOpen was not canceled.'),
TEST_DURATION);
const timer = setInterval(() => {
++nbIntervalFired;
if (nbIntervalFired === N) {
const timer = setInterval(common.mustCall(() => {
if (--N === 0) {
clearInterval(timer);
timer._onTimeout = () => {
throw new Error('Unrefd interval fired after being cleared.');
};
timer._onTimeout =
common.mustNotCall('Unrefd interal fired after being cleared');
clearTimeout(keepOpen);
}
}, 1);
}, N), 1);
timer.unref();
@@ -1,20 +1,7 @@
'use strict';
require('../common');
const assert = require('assert');
const common = require('../common');
let once = 0;
process.on('beforeExit', () => {
if (once > 1)
throw new RangeError('beforeExit should only have been called once!');
setTimeout(() => {}, 1).unref();
once++;
});
process.on('exit', (code) => {
if (code !== 0) return;
assert.strictEqual(once, 1);
});
process.on('beforeExit', common.mustCall(() => {
setTimeout(common.mustNotCall(), 1).unref();
}));
@@ -15,18 +15,14 @@ const assert = require('assert');
}
{
let ncalled = 0;
let ncalled = 3;
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
function f(a, b, c) {
const f = common.mustCall((a, b, c) => {
assert.strictEqual(a, 'foo');
assert.strictEqual(b, 'bar');
assert.strictEqual(c, 'baz');
if (++ncalled === 3) clearTimeout(iv);
}
if (--ncalled === 0) clearTimeout(iv);
}, ncalled);
process.on('exit', function() {
assert.strictEqual(ncalled, 3);
});
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
}

0 comments on commit fc49cf4

Please sign in to comment.