Skip to content

Commit fc49cf4

Browse files
jasnellMylesBorins
authored andcommitted
test: improve multiple timers tests
PR-URL: #14616 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent c88f99f commit fc49cf4

8 files changed

Lines changed: 69 additions & 133 deletions

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22
constcommon=require('../common');
33
constassert=require('assert');
44

5-
letimmediateB;
6-
letimmediateC;
7-
letimmediateD;
8-
95
letmainFinished=false;
106

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

16-
immediateB=setImmediate(function(){
12+
letimmediateB=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‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
constcommon=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
constTIMEOUT_DELAY=1.1;
21-
constNB_TIMEOUTS_FIRED=50;
21+
letN=50;
2222

23-
letnbTimeoutFired=0;
24-
constinterval=setInterval(function(){
25-
++nbTimeoutFired;
26-
if(nbTimeoutFired===NB_TIMEOUTS_FIRED){
23+
constinterval=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‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
constcommon=require('../common');
88
constnet=require('net');
9+
constCountdown=require('../common/countdown');
910

1011
constclients=[];
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,functiononTimeout(){
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-
letnbClientsEnded=0;
35+
server.listen(0,common.localhostIPv4,common.mustCall(()=>{
36+
constcountdown=newCountdown(2,common.mustCall(()=>server.close()));
3637

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

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

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

137
// Wrap `close` method to check if the handle was closed
148
constclose=timeout._handle.close;
15-
timeout._handle.close=function(){
16-
closed++;
9+
timeout._handle.close=common.mustCall(function(){
1710
returnclose.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‎

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,58 @@
11
'use strict';
22

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

6-
letinterval_fired=false;
7-
lettimeout_fired=false;
86
letunref_interval=false;
97
letunref_timer=false;
10-
letunref_callbacks=0;
118
letchecks=0;
129

1310
constLONG_TIME=10*1000;
1411
constSHORT_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-
constinterval=setInterval(function(){
24+
constinterval=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-
constcheck_unref=setInterval(function(){
34+
constcheck_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+
consttimeout=
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+
consttimeout=
50+
setInterval(()=>timeout.unref(),SHORT_TIME);
51+
}
5752

5853
// Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261.
5954
{
6055
constt=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‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
constcommon=require('../common');
66

77
constTEST_DURATION=common.platformTimeout(1000);
8-
constN=3;
9-
letnbIntervalFired=0;
8+
letN=3;
109

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

16-
consttimer=setInterval(()=>{
17-
++nbIntervalFired;
18-
if(nbIntervalFired===N){
15+
consttimer=setInterval(common.mustCall(()=>{
16+
if(--N===0){
1917
clearInterval(timer);
20-
timer._onTimeout=()=>{
21-
thrownewError('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();
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
'use strict';
22

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

6-
letonce=0;
7-
8-
process.on('beforeExit',()=>{
9-
if(once>1)
10-
thrownewRangeError('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‎

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

1717
{
18-
letncalled=0;
18+
letncalled=3;
1919

20-
constiv=setInterval(f,0,'foo','bar','baz');
21-
22-
functionf(a,b,c){
20+
constf=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+
constiv=setInterval(f,0,'foo','bar','baz');
3228
}

0 commit comments

Comments
 (0)