Skip to content

Commit fc49cf4

Browse files
jasnellMylesBorins
authored andcommitted
test: improve multiple timers tests
PR-URL: #14616 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent c88f99f commit fc49cf4

8 files changed

+69
-133
lines changed

test/parallel/test-timers-immediate.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22
const common = require('../common');
33
const assert = require('assert');
44

5-
let immediateB;
6-
let immediateC;
7-
let immediateD;
8-
95
let mainFinished = false;
106

117
setImmediate(common.mustCall(function() {
128
assert.strictEqual(mainFinished, true);
139
clearImmediate(immediateB);
1410
}));
1511

16-
immediateB = setImmediate(function() {
12+
let immediateB = setImmediate(function() {
1713
common.fail('this immediate should not run');
1814
});
1915

20-
setImmediate(function(x, y, z) {
21-
immediateC = [x, y, z];
22-
}, 1, 2, 3);
23-
24-
setImmediate(function(x, y, z, a, b) {
25-
immediateD = [x, y, z, a, b];
26-
}, 1, 2, 3, 4, 5);
16+
setImmediate(common.mustCall((...args) => {
17+
assert.deepStrictEqual(args, [1, 2, 3]);
18+
}), 1, 2, 3);
2719

28-
process.on('exit', function() {
29-
assert.deepStrictEqual(immediateC, [1, 2, 3], 'immediateC args should match');
30-
assert.deepStrictEqual(immediateD, [1, 2, 3, 4, 5], '5 args should match');
31-
});
20+
setImmediate(common.mustCall((...args) => {
21+
assert.deepStrictEqual(args, [1, 2, 3, 4, 5]);
22+
}), 1, 2, 3, 4, 5);
3223

3324
mainFinished = true;

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33

44
/*
55
* This test makes sure that non-integer timer delays do not make the process
@@ -18,13 +18,11 @@ require('../common');
1818
*/
1919

2020
const TIMEOUT_DELAY = 1.1;
21-
const NB_TIMEOUTS_FIRED = 50;
21+
let N = 50;
2222

23-
let nbTimeoutFired = 0;
24-
const interval = setInterval(function() {
25-
++nbTimeoutFired;
26-
if (nbTimeoutFired === NB_TIMEOUTS_FIRED) {
23+
const interval = setInterval(common.mustCall(() => {
24+
if (--N === 0) {
2725
clearInterval(interval);
2826
process.exit(0);
2927
}
30-
}, TIMEOUT_DELAY);
28+
}, N), TIMEOUT_DELAY);

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

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const common = require('../common');
88
const net = require('net');
9+
const Countdown = require('../common/countdown');
910

1011
const clients = [];
1112

@@ -19,7 +20,7 @@ const server = net.createServer(function onClient(client) {
1920
* the list of unref timers when traversing it, and exposes the
2021
* original issue in joyent/node#8897.
2122
*/
22-
clients[0].setTimeout(1, function onTimeout() {
23+
clients[0].setTimeout(1, () => {
2324
clients[1].setTimeout(0);
2425
clients[0].end();
2526
clients[1].end();
@@ -31,19 +32,16 @@ const server = net.createServer(function onClient(client) {
3132
}
3233
});
3334

34-
server.listen(0, common.localhostIPv4, function() {
35-
let nbClientsEnded = 0;
35+
server.listen(0, common.localhostIPv4, common.mustCall(() => {
36+
const countdown = new Countdown(2, common.mustCall(() => server.close()));
3637

37-
function addEndedClient(client) {
38-
++nbClientsEnded;
39-
if (nbClientsEnded === 2) {
40-
server.close();
41-
}
38+
{
39+
const client = net.connect({ port: server.address().port });
40+
client.on('end', () => countdown.dec());
4241
}
4342

44-
const client1 = net.connect({ port: this.address().port });
45-
client1.on('end', addEndedClient);
46-
47-
const client2 = net.connect({ port: this.address().port });
48-
client2.on('end', addEndedClient);
49-
});
43+
{
44+
const client = net.connect({ port: server.address().port });
45+
client.on('end', () => countdown.dec());
46+
}
47+
}));
+5-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
'use strict';
2-
require('../common');
3-
const assert = require('assert');
2+
const common = require('../common');
43

5-
let called = 0;
6-
let closed = 0;
7-
8-
const timeout = setTimeout(function() {
9-
called++;
10-
}, 10);
4+
const timeout = setTimeout(common.mustCall(), 10);
115
timeout.unref();
126

137
// Wrap `close` method to check if the handle was closed
148
const close = timeout._handle.close;
15-
timeout._handle.close = function() {
16-
closed++;
9+
timeout._handle.close = common.mustCall(function() {
1710
return close.apply(this, arguments);
18-
};
11+
});
1912

2013
// Just to keep process alive and let previous timer's handle die
21-
setTimeout(function() {
22-
}, 50);
23-
24-
process.on('exit', function() {
25-
assert.strictEqual(called, 1);
26-
assert.strictEqual(closed, 1);
27-
});
14+
setTimeout(() => {}, 50);

test/parallel/test-timers-unref.js

+21-39
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,58 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55

6-
let interval_fired = false;
7-
let timeout_fired = false;
86
let unref_interval = false;
97
let unref_timer = false;
10-
let unref_callbacks = 0;
118
let checks = 0;
129

1310
const LONG_TIME = 10 * 1000;
1411
const SHORT_TIME = 100;
1512

16-
assert.doesNotThrow(function() {
13+
assert.doesNotThrow(() => {
1714
setTimeout(() => {}, 10).unref().ref().unref();
1815
}, 'ref and unref are chainable');
1916

20-
assert.doesNotThrow(function() {
17+
assert.doesNotThrow(() => {
2118
setInterval(() => {}, 10).unref().ref().unref();
2219
}, 'ref and unref are chainable');
2320

24-
setInterval(function() {
25-
interval_fired = true;
26-
}, LONG_TIME).unref();
21+
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
22+
setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref();
2723

28-
setTimeout(function() {
29-
timeout_fired = true;
30-
}, LONG_TIME).unref();
31-
32-
const interval = setInterval(function() {
24+
const interval = setInterval(common.mustCall(() => {
3325
unref_interval = true;
3426
clearInterval(interval);
35-
}, SHORT_TIME);
27+
}), SHORT_TIME);
3628
interval.unref();
3729

38-
setTimeout(function() {
30+
setTimeout(common.mustCall(() => {
3931
unref_timer = true;
40-
}, SHORT_TIME).unref();
32+
}), SHORT_TIME).unref();
4133

42-
const check_unref = setInterval(function() {
34+
const check_unref = setInterval(() => {
4335
if (checks > 5 || (unref_interval && unref_timer))
4436
clearInterval(check_unref);
4537
checks += 1;
4638
}, 100);
4739

48-
setTimeout(function() {
49-
unref_callbacks++;
50-
this.unref();
51-
}, SHORT_TIME);
40+
{
41+
const timeout =
42+
setTimeout(common.mustCall(() => {
43+
timeout.unref();
44+
}), SHORT_TIME);
45+
}
5246

53-
// Should not timeout the test
54-
setInterval(function() {
55-
this.unref();
56-
}, SHORT_TIME);
47+
{
48+
// Should not timeout the test
49+
const timeout =
50+
setInterval(() => timeout.unref(), SHORT_TIME);
51+
}
5752

5853
// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
5954
{
6055
const t = setInterval(() => {}, 1);
6156
process.nextTick(t.unref.bind({}));
6257
process.nextTick(t.unref.bind(t));
6358
}
64-
65-
process.on('exit', function() {
66-
assert.strictEqual(interval_fired, false,
67-
'Interval should not fire');
68-
assert.strictEqual(timeout_fired, false,
69-
'Timeout should not fire');
70-
assert.strictEqual(unref_timer, true,
71-
'An unrefd timeout should still fire');
72-
assert.strictEqual(unref_interval, true,
73-
'An unrefd interval should still fire');
74-
assert.strictEqual(unref_callbacks, 1,
75-
'Callback should only run once');
76-
});

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

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
const common = require('../common');
66

77
const TEST_DURATION = common.platformTimeout(1000);
8-
const N = 3;
9-
let nbIntervalFired = 0;
8+
let N = 3;
109

11-
const keepOpen = setTimeout(() => {
12-
console.error('[FAIL] Interval fired %d/%d times.', nbIntervalFired, N);
13-
throw new Error('Test timed out. keepOpen was not canceled.');
14-
}, TEST_DURATION);
10+
const keepOpen =
11+
setTimeout(
12+
common.mustNotCall('Test timed out. keepOpen was not canceled.'),
13+
TEST_DURATION);
1514

16-
const timer = setInterval(() => {
17-
++nbIntervalFired;
18-
if (nbIntervalFired === N) {
15+
const timer = setInterval(common.mustCall(() => {
16+
if (--N === 0) {
1917
clearInterval(timer);
20-
timer._onTimeout = () => {
21-
throw new Error('Unrefd interval fired after being cleared.');
22-
};
18+
timer._onTimeout =
19+
common.mustNotCall('Unrefd interal fired after being cleared');
2320
clearTimeout(keepOpen);
2421
}
25-
}, 1);
22+
}, N), 1);
2623

2724
timer.unref();
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
'use strict';
22

3-
require('../common');
4-
const assert = require('assert');
3+
const common = require('../common');
54

6-
let once = 0;
7-
8-
process.on('beforeExit', () => {
9-
if (once > 1)
10-
throw new RangeError('beforeExit should only have been called once!');
11-
12-
setTimeout(() => {}, 1).unref();
13-
once++;
14-
});
15-
16-
process.on('exit', (code) => {
17-
if (code !== 0) return;
18-
19-
assert.strictEqual(once, 1);
20-
});
5+
process.on('beforeExit', common.mustCall(() => {
6+
setTimeout(common.mustNotCall(), 1).unref();
7+
}));

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ const assert = require('assert');
1515
}
1616

1717
{
18-
let ncalled = 0;
18+
let ncalled = 3;
1919

20-
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
21-
22-
function f(a, b, c) {
20+
const f = common.mustCall((a, b, c) => {
2321
assert.strictEqual(a, 'foo');
2422
assert.strictEqual(b, 'bar');
2523
assert.strictEqual(c, 'baz');
26-
if (++ncalled === 3) clearTimeout(iv);
27-
}
24+
if (--ncalled === 0) clearTimeout(iv);
25+
}, ncalled);
2826

29-
process.on('exit', function() {
30-
assert.strictEqual(ncalled, 3);
31-
});
27+
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
3228
}

0 commit comments

Comments
 (0)