Browse Source

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>
v6
James M Snell 8 years ago
parent
commit
7192e913f7
  1. 20
      test/parallel/test-timers-immediate.js
  2. 12
      test/parallel/test-timers-non-integer-delay.js
  3. 26
      test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js
  4. 23
      test/parallel/test-timers-unref-leak.js
  5. 60
      test/parallel/test-timers-unref.js
  6. 23
      test/parallel/test-timers-unrefd-interval-still-fires.js
  7. 21
      test/parallel/test-timers-unrefed-in-beforeexit.js
  8. 14
      test/parallel/test-timers-zero-timeout.js

20
test/parallel/test-timers-immediate.js

@ -23,9 +23,6 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
let immediateC;
let immediateD;
let mainFinished = false; let mainFinished = false;
setImmediate(common.mustCall(function() { setImmediate(common.mustCall(function() {
@ -35,17 +32,12 @@ setImmediate(common.mustCall(function() {
const immediateB = setImmediate(common.mustNotCall()); const immediateB = setImmediate(common.mustNotCall());
setImmediate(function(x, y, z) { setImmediate(common.mustCall((...args) => {
immediateC = [x, y, z]; assert.deepStrictEqual(args, [1, 2, 3]);
}, 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() { setImmediate(common.mustCall((...args) => {
assert.deepStrictEqual(immediateC, [1, 2, 3], 'immediateC args should match'); assert.deepStrictEqual(args, [1, 2, 3, 4, 5]);
assert.deepStrictEqual(immediateD, [1, 2, 3, 4, 5], '5 args should match'); }), 1, 2, 3, 4, 5);
});
mainFinished = true; mainFinished = true;

12
test/parallel/test-timers-non-integer-delay.js

@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; 'use strict';
require('../common'); const common = require('../common');
/* /*
* This test makes sure that non-integer timer delays do not make the process * 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 TIMEOUT_DELAY = 1.1;
const NB_TIMEOUTS_FIRED = 50; let N = 50;
let nbTimeoutFired = 0; const interval = setInterval(common.mustCall(() => {
const interval = setInterval(function() { if (--N === 0) {
++nbTimeoutFired;
if (nbTimeoutFired === NB_TIMEOUTS_FIRED) {
clearInterval(interval); clearInterval(interval);
process.exit(0); process.exit(0);
} }
}, TIMEOUT_DELAY); }, N), TIMEOUT_DELAY);

26
test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js

@ -6,6 +6,7 @@
const common = require('../common'); const common = require('../common');
const net = require('net'); const net = require('net');
const Countdown = require('../common/countdown');
const clients = []; const clients = [];
@ -19,7 +20,7 @@ const server = net.createServer(function onClient(client) {
* the list of unref timers when traversing it, and exposes the * the list of unref timers when traversing it, and exposes the
* original issue in joyent/node#8897. * original issue in joyent/node#8897.
*/ */
clients[0].setTimeout(1, function onTimeout() { clients[0].setTimeout(1, () => {
clients[1].setTimeout(0); clients[1].setTimeout(0);
clients[0].end(); clients[0].end();
clients[1].end(); clients[1].end();
@ -31,19 +32,16 @@ const server = net.createServer(function onClient(client) {
} }
}); });
server.listen(0, common.localhostIPv4, function() { server.listen(0, common.localhostIPv4, common.mustCall(() => {
let nbClientsEnded = 0; const countdown = new Countdown(2, common.mustCall(() => server.close()));
function addEndedClient(client) { {
++nbClientsEnded; const client = net.connect({ port: server.address().port });
if (nbClientsEnded === 2) { client.on('end', () => countdown.dec());
server.close();
}
} }
const client1 = net.connect({ port: this.address().port }); {
client1.on('end', addEndedClient); const client = net.connect({ port: server.address().port });
client.on('end', () => countdown.dec());
const client2 = net.connect({ port: this.address().port }); }
client2.on('end', addEndedClient); }));
});

23
test/parallel/test-timers-unref-leak.js

@ -1,27 +1,14 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert');
let called = 0; const timeout = setTimeout(common.mustCall(), 10);
let closed = 0;
const timeout = setTimeout(function() {
called++;
}, 10);
timeout.unref(); timeout.unref();
// Wrap `close` method to check if the handle was closed // Wrap `close` method to check if the handle was closed
const close = timeout._handle.close; const close = timeout._handle.close;
timeout._handle.close = function() { timeout._handle.close = common.mustCall(function() {
closed++;
return close.apply(this, arguments); return close.apply(this, arguments);
}; });
// Just to keep process alive and let previous timer's handle die // Just to keep process alive and let previous timer's handle die
setTimeout(function() { setTimeout(() => {}, 50);
}, 50);
process.on('exit', function() {
assert.strictEqual(called, 1);
assert.strictEqual(closed, 1);
});

60
test/parallel/test-timers-unref.js

@ -21,60 +21,55 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
let interval_fired = false;
let timeout_fired = false;
let unref_interval = false; let unref_interval = false;
let unref_timer = false; let unref_timer = false;
let unref_callbacks = 0;
let checks = 0; let checks = 0;
const LONG_TIME = 10 * 1000; const LONG_TIME = 10 * 1000;
const SHORT_TIME = 100; const SHORT_TIME = 100;
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
setTimeout(() => {}, 10).unref().ref().unref(); setTimeout(() => {}, 10).unref().ref().unref();
}, 'ref and unref are chainable'); }, 'ref and unref are chainable');
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
setInterval(() => {}, 10).unref().ref().unref(); setInterval(() => {}, 10).unref().ref().unref();
}, 'ref and unref are chainable'); }, 'ref and unref are chainable');
setInterval(function() { setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
interval_fired = true; setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref();
}, LONG_TIME).unref();
setTimeout(function() { const interval = setInterval(common.mustCall(() => {
timeout_fired = true;
}, LONG_TIME).unref();
const interval = setInterval(function() {
unref_interval = true; unref_interval = true;
clearInterval(interval); clearInterval(interval);
}, SHORT_TIME); }), SHORT_TIME);
interval.unref(); interval.unref();
setTimeout(function() { setTimeout(common.mustCall(() => {
unref_timer = true; 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)) if (checks > 5 || (unref_interval && unref_timer))
clearInterval(check_unref); clearInterval(check_unref);
checks += 1; checks += 1;
}, 100); }, 100);
setTimeout(function() { {
unref_callbacks++; const timeout =
this.unref(); setTimeout(common.mustCall(() => {
}, SHORT_TIME); timeout.unref();
}), SHORT_TIME);
}
// Should not timeout the test {
setInterval(function() { // Should not timeout the test
this.unref(); const timeout =
}, SHORT_TIME); setInterval(() => timeout.unref(), SHORT_TIME);
}
// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261. // 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({}));
process.nextTick(t.unref.bind(t)); 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');
});

23
test/parallel/test-timers-unrefd-interval-still-fires.js

@ -5,23 +5,20 @@
const common = require('../common'); const common = require('../common');
const TEST_DURATION = common.platformTimeout(1000); const TEST_DURATION = common.platformTimeout(1000);
const N = 3; let N = 3;
let nbIntervalFired = 0;
const keepOpen = setTimeout(() => { const keepOpen =
console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N); setTimeout(
throw new Error('Test timed out. keepOpen was not canceled.'); common.mustNotCall('Test timed out. keepOpen was not canceled.'),
}, TEST_DURATION); TEST_DURATION);
const timer = setInterval(() => { const timer = setInterval(common.mustCall(() => {
++nbIntervalFired; if (--N === 0) {
if (nbIntervalFired === N) {
clearInterval(timer); clearInterval(timer);
timer._onTimeout = () => { timer._onTimeout =
throw new Error('Unrefd interval fired after being cleared.'); common.mustNotCall('Unrefd interal fired after being cleared');
};
clearTimeout(keepOpen); clearTimeout(keepOpen);
} }
}, 1); }, N), 1);
timer.unref(); timer.unref();

21
test/parallel/test-timers-unrefed-in-beforeexit.js

@ -1,20 +1,7 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert');
let once = 0; process.on('beforeExit', common.mustCall(() => {
setTimeout(common.mustNotCall(), 1).unref();
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);
});

14
test/parallel/test-timers-zero-timeout.js

@ -36,18 +36,14 @@ const assert = require('assert');
} }
{ {
let ncalled = 0; let ncalled = 3;
const iv = setInterval(f, 0, 'foo', 'bar', 'baz'); const f = common.mustCall((a, b, c) => {
function f(a, b, c) {
assert.strictEqual(a, 'foo'); assert.strictEqual(a, 'foo');
assert.strictEqual(b, 'bar'); assert.strictEqual(b, 'bar');
assert.strictEqual(c, 'baz'); assert.strictEqual(c, 'baz');
if (++ncalled === 3) clearTimeout(iv); if (--ncalled === 0) clearTimeout(iv);
} }, ncalled);
process.on('exit', function() { const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
assert.strictEqual(ncalled, 3);
});
} }

Loading…
Cancel
Save