File tree 3 files changed +44
-4
lines changed 3 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -186,7 +186,7 @@ added: v0.0.1
186
186
Schedules repeated execution of ` callback ` every ` delay ` milliseconds.
187
187
188
188
When ` delay ` is larger than ` 2147483647 ` or less than ` 1 ` , the ` delay ` will be
189
- set to ` 1 ` .
189
+ set to ` 1 ` . Non-integer delays are truncated to an integer.
190
190
191
191
If ` callback ` is not a function, a [ ` TypeError ` ] [ ] will be thrown.
192
192
@@ -209,7 +209,7 @@ nor of their ordering. The callback will be called as close as possible to the
209
209
time specified.
210
210
211
211
When ` delay ` is larger than ` 2147483647 ` or less than ` 1 ` , the ` delay `
212
- will be set to ` 1 ` .
212
+ will be set to ` 1 ` . Non-integer delays are truncated to an integer.
213
213
214
214
If ` callback ` is not a function, a [ ` TypeError ` ] [ ] will be thrown.
215
215
Original file line number Diff line number Diff line change @@ -193,10 +193,13 @@ exports._unrefActive = function(item) {
193
193
// Appends a timer onto the end of an existing timers list, or creates a new
194
194
// list if one does not already exist for the specified timeout duration.
195
195
function insert ( item , refed , start ) {
196
- const msecs = item . _idleTimeout ;
196
+ let msecs = item . _idleTimeout ;
197
197
if ( msecs < 0 || msecs === undefined )
198
198
return ;
199
199
200
+ // Truncate so that accuracy of sub-milisecond timers is not assumed.
201
+ msecs = Math . trunc ( msecs ) ;
202
+
200
203
item . _idleStart = start ;
201
204
202
205
// Use an existing list if there is one, otherwise we need to make a new one.
@@ -378,7 +381,9 @@ function unenroll(item) {
378
381
// clearTimeout that makes it clear that the list should not be deleted.
379
382
// That function could then be used by http and other similar modules.
380
383
if ( item [ kRefed ] ) {
381
- const list = lists [ item . _idleTimeout ] ;
384
+ // Compliment truncation during insert().
385
+ const msecs = Math . trunc ( item . _idleTimeout ) ;
386
+ const list = lists [ msecs ] ;
382
387
if ( list !== undefined && L . isEmpty ( list ) ) {
383
388
debug ( 'unenroll: list empty' ) ;
384
389
queue . removeAt ( list . priorityQueuePosition ) ;
Original file line number Diff line number Diff line change 21
21
22
22
'use strict' ;
23
23
const common = require ( '../common' ) ;
24
+ const assert = require ( 'assert' ) ;
24
25
25
26
/*
26
27
* This test makes sure that non-integer timer delays do not make the process
@@ -47,3 +48,37 @@ const interval = setInterval(common.mustCall(() => {
47
48
process . exit ( 0 ) ;
48
49
}
49
50
} , N ) , TIMEOUT_DELAY ) ;
51
+
52
+ // Test non-integer delay ordering
53
+ {
54
+ const ordering = [ ] ;
55
+
56
+ setTimeout ( common . mustCall ( ( ) => {
57
+ ordering . push ( 1 ) ;
58
+ } ) , 1 ) ;
59
+
60
+ setTimeout ( common . mustCall ( ( ) => {
61
+ ordering . push ( 2 ) ;
62
+ } ) , 1.8 ) ;
63
+
64
+ setTimeout ( common . mustCall ( ( ) => {
65
+ ordering . push ( 3 ) ;
66
+ } ) , 1.1 ) ;
67
+
68
+ setTimeout ( common . mustCall ( ( ) => {
69
+ ordering . push ( 4 ) ;
70
+ } ) , 1 ) ;
71
+
72
+ setTimeout ( common . mustCall ( ( ) => {
73
+ const expected = [ 1 , 2 , 3 , 4 ] ;
74
+
75
+ assert . deepStrictEqual (
76
+ ordering ,
77
+ expected ,
78
+ `Non-integer delay ordering should be ${ expected } , but got ${ ordering } `
79
+ ) ;
80
+
81
+ // 2 should always be last of these delays due to ordering guarentees by
82
+ // the implementation.
83
+ } ) , 2 ) ;
84
+ }
You can’t perform that action at this time.
0 commit comments