Skip to content

Commit 7192e91

Browse files
committed
test: improve multiple timers tests
PR-URL: #14616 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent af70c3b commit 7192e91

8 files changed

+68
-131
lines changed

test/parallel/test-timers-immediate.js

+6-14
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
const common = require('../common');
2424
const assert = require('assert');
2525

26-
let immediateC;
27-
let immediateD;
28-
2926
let mainFinished = false;
3027

3128
setImmediate(common.mustCall(function() {
@@ -35,17 +32,12 @@ setImmediate(common.mustCall(function() {
3532

3633
const immediateB = setImmediate(common.mustNotCall());
3734

38-
setImmediate(function(x, y, z) {
39-
immediateC = [x, y, z];
40-
}, 1, 2, 3);
41-
42-
setImmediate(function(x, y, z, a, b) {
43-
immediateD = [x, y, z, a, b];
44-
}, 1, 2, 3, 4, 5);
35+
setImmediate(common.mustCall((...args) => {
36+
assert.deepStrictEqual(args, [1, 2, 3]);
37+
}), 1, 2, 3);
4538

46-
process.on('exit', function() {
47-
assert.deepStrictEqual(immediateC, [1, 2, 3], 'immediateC args should match');
48-
assert.deepStrictEqual(immediateD, [1, 2, 3, 4, 5], '5 args should match');
49-
});
39+
setImmediate(common.mustCall((...args) => {
40+
assert.deepStrictEqual(args, [1, 2, 3, 4, 5]);
41+
}), 1, 2, 3, 4, 5);
5042

5143
mainFinished = true;

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424

2525
/*
2626
* This test makes sure that non-integer timer delays do not make the process
@@ -39,13 +39,11 @@ require('../common');
3939
*/
4040

4141
const TIMEOUT_DELAY = 1.1;
42-
const NB_TIMEOUTS_FIRED = 50;
42+
let N = 50;
4343

44-
let nbTimeoutFired = 0;
45-
const interval = setInterval(function() {
46-
++nbTimeoutFired;
47-
if (nbTimeoutFired === NB_TIMEOUTS_FIRED) {
44+
const interval = setInterval(common.mustCall(() => {
45+
if (--N === 0) {
4846
clearInterval(interval);
4947
process.exit(0);
5048
}
51-
}, TIMEOUT_DELAY);
49+
}, 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
@@ -21,77 +21,59 @@
2121

2222
'use strict';
2323

24-
require('../common');
24+
const common = require('../common');
2525
const assert = require('assert');
2626

27-
let interval_fired = false;
28-
let timeout_fired = false;
2927
let unref_interval = false;
3028
let unref_timer = false;
31-
let unref_callbacks = 0;
3229
let checks = 0;
3330

3431
const LONG_TIME = 10 * 1000;
3532
const SHORT_TIME = 100;
3633

37-
assert.doesNotThrow(function() {
34+
assert.doesNotThrow(() => {
3835
setTimeout(() => {}, 10).unref().ref().unref();
3936
}, 'ref and unref are chainable');
4037

41-
assert.doesNotThrow(function() {
38+
assert.doesNotThrow(() => {
4239
setInterval(() => {}, 10).unref().ref().unref();
4340
}, 'ref and unref are chainable');
4441

45-
setInterval(function() {
46-
interval_fired = true;
47-
}, LONG_TIME).unref();
42+
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
43+
setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref();
4844

49-
setTimeout(function() {
50-
timeout_fired = true;
51-
}, LONG_TIME).unref();
52-
53-
const interval = setInterval(function() {
45+
const interval = setInterval(common.mustCall(() => {
5446
unref_interval = true;
5547
clearInterval(interval);
56-
}, SHORT_TIME);
48+
}), SHORT_TIME);
5749
interval.unref();
5850

59-
setTimeout(function() {
51+
setTimeout(common.mustCall(() => {
6052
unref_timer = true;
61-
}, SHORT_TIME).unref();
53+
}), SHORT_TIME).unref();
6254

63-
const check_unref = setInterval(function() {
55+
const check_unref = setInterval(() => {
6456
if (checks > 5 || (unref_interval && unref_timer))
6557
clearInterval(check_unref);
6658
checks += 1;
6759
}, 100);
6860

69-
setTimeout(function() {
70-
unref_callbacks++;
71-
this.unref();
72-
}, SHORT_TIME);
61+
{
62+
const timeout =
63+
setTimeout(common.mustCall(() => {
64+
timeout.unref();
65+
}), SHORT_TIME);
66+
}
7367

74-
// Should not timeout the test
75-
setInterval(function() {
76-
this.unref();
77-
}, SHORT_TIME);
68+
{
69+
// Should not timeout the test
70+
const timeout =
71+
setInterval(() => timeout.unref(), SHORT_TIME);
72+
}
7873

7974
// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
8075
{
8176
const t = setInterval(() => {}, 1);
8277
process.nextTick(t.unref.bind({}));
8378
process.nextTick(t.unref.bind(t));
8479
}
85-
86-
process.on('exit', function() {
87-
assert.strictEqual(interval_fired, false,
88-
'Interval should not fire');
89-
assert.strictEqual(timeout_fired, false,
90-
'Timeout should not fire');
91-
assert.strictEqual(unref_timer, true,
92-
'An unrefd timeout should still fire');
93-
assert.strictEqual(unref_interval, true,
94-
'An unrefd interval should still fire');
95-
assert.strictEqual(unref_callbacks, 1,
96-
'Callback should only run once');
97-
});

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
@@ -36,18 +36,14 @@ const assert = require('assert');
3636
}
3737

3838
{
39-
let ncalled = 0;
39+
let ncalled = 3;
4040

41-
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
42-
43-
function f(a, b, c) {
41+
const f = common.mustCall((a, b, c) => {
4442
assert.strictEqual(a, 'foo');
4543
assert.strictEqual(b, 'bar');
4644
assert.strictEqual(c, 'baz');
47-
if (++ncalled === 3) clearTimeout(iv);
48-
}
45+
if (--ncalled === 0) clearTimeout(iv);
46+
}, ncalled);
4947

50-
process.on('exit', function() {
51-
assert.strictEqual(ncalled, 3);
52-
});
48+
const iv = setInterval(f, 0, 'foo', 'bar', 'baz');
5349
}

0 commit comments

Comments
 (0)