Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve multiple timers tests #14616

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions test/parallel/test-timers-immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
const common = require('../common');
const assert = require('assert');

let immediateC;
let immediateD;

let mainFinished = false;

setImmediate(common.mustCall(function() {
Expand All @@ -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(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;
12 changes: 5 additions & 7 deletions test/parallel/test-timers-non-integer-delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const common = require('../common');
const net = require('net');
const Countdown = require('../common/countdown');

const clients = [];

Expand All @@ -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();
Expand All @@ -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());
}
}));
23 changes: 5 additions & 18 deletions test/parallel/test-timers-unref-leak.js
Original file line number Diff line number Diff line change
@@ -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);
60 changes: 21 additions & 39 deletions test/parallel/test-timers-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,77 +21,59 @@

'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');
});
23 changes: 10 additions & 13 deletions test/parallel/test-timers-unrefd-interval-still-fires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is an uncommon wrapping, IMHO

const keepOpen = setTimeout(
  common.mustNotCall('Test timed out. keepOpen was not canceled.'),
  TEST_DURATION
);

is more "regular".

Alltought

const keepOpenMsg = 'Test timed out. keepOpen was not canceled.'
const keepOpen = setTimeout(common.mustNotCall(keepOpenMsg), TEST_DURATION);

is usually my preference.

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();
21 changes: 4 additions & 17 deletions test/parallel/test-timers-unrefed-in-beforeexit.js
Original file line number Diff line number Diff line change
@@ -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();
}));
14 changes: 5 additions & 9 deletions test/parallel/test-timers-zero-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ const assert = require('assert');
}

{
let ncalled = 0;
let ncalled = 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: camelCase nCalled


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');
}