test: improve multiple timers tests
PR-URL: https://github.com/nodejs/node/pull/14616 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
parent
af70c3b478
commit
7192e913f7
@ -23,9 +23,6 @@
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
let immediateC;
|
||||
let immediateD;
|
||||
|
||||
let mainFinished = false;
|
||||
|
||||
setImmediate(common.mustCall(function() {
|
||||
@ -35,17 +32,12 @@ setImmediate(common.mustCall(function() {
|
||||
|
||||
const immediateB = setImmediate(common.mustNotCall());
|
||||
|
||||
setImmediate(function(x, y, z) {
|
||||
immediateC = [x, y, z];
|
||||
}, 1, 2, 3);
|
||||
setImmediate(common.mustCall((...args) => {
|
||||
assert.deepStrictEqual(args, [1, 2, 3]);
|
||||
}), 1, 2, 3);
|
||||
|
||||
setImmediate(function(x, y, z, a, b) {
|
||||
immediateD = [x, y, z, a, b];
|
||||
}, 1, 2, 3, 4, 5);
|
||||
|
||||
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;
|
||||
|
@ -20,7 +20,7 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
|
||||
/*
|
||||
* This test makes sure that non-integer timer delays do not make the process
|
||||
@ -39,13 +39,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);
|
||||
|
@ -21,60 +21,55 @@
|
||||
|
||||
'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.
|
||||
{
|
||||
@ -82,16 +77,3 @@ setInterval(function() {
|
||||
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();
|
||||
}));
|
||||
|
@ -36,18 +36,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');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user