@@ -25,99 +25,104 @@ const assert = require('assert');
2525
2626const WINDOW = 200 ; // Why does this need to be so big?
2727
28- let interval_count = 0 ;
2928
3029const starttime = new Date ( ) ;
31- setTimeout ( common . mustCall ( function ( ) {
32- const endtime = new Date ( ) ;
30+ {
31+ setTimeout ( common . mustCall ( function ( ) {
32+ const endtime = new Date ( ) ;
3333
34- const diff = endtime - starttime ;
35- assert . ok ( diff > 0 ) ;
36- console . error ( `diff: ${ diff } ` ) ;
34+ const diff = endtime - starttime ;
35+ assert . ok ( diff > 0 ) ;
36+ console . error ( `diff: ${ diff } ` ) ;
3737
38- assert . strictEqual ( 1000 - WINDOW < diff && diff < 1000 + WINDOW , true ) ;
39- } ) , 1000 ) ;
38+ assert . strictEqual ( 1000 - WINDOW < diff && diff < 1000 + WINDOW , true ) ;
39+ } ) , 1000 ) ;
40+ }
4041
4142// This timer shouldn't execute
42- const id = setTimeout ( function ( ) { assert . strictEqual ( true , false ) ; } , 500 ) ;
43- clearTimeout ( id ) ;
43+ {
44+ const id = setTimeout ( common . mustNotCall ( ) , 500 ) ;
45+ clearTimeout ( id ) ;
46+ }
4447
45- setInterval ( function ( ) {
46- interval_count += 1 ;
47- const endtime = new Date ( ) ;
48+ {
49+ let interval_count = 0 ;
4850
49- const diff = endtime - starttime ;
50- assert . ok ( diff > 0 ) ;
51- console . error ( `diff: ${ diff } ` ) ;
51+ setInterval ( common . mustCall ( function ( ) {
52+ interval_count += 1 ;
53+ const endtime = new Date ( ) ;
5254
53- const t = interval_count * 1000 ;
55+ const diff = endtime - starttime ;
56+ assert . ok ( diff > 0 ) ;
57+ console . error ( `diff: ${ diff } ` ) ;
5458
55- assert . strictEqual ( t - WINDOW < diff && diff < t + WINDOW , true ) ;
59+ const t = interval_count * 1000 ;
5660
57- assert . strictEqual ( interval_count <= 3 , true ) ;
58- if ( interval_count === 3 )
59- clearInterval ( this ) ;
60- } , 1000 ) ;
61+ assert . strictEqual ( t - WINDOW < diff && diff < t + WINDOW , true ) ;
62+
63+ assert . strictEqual ( interval_count <= 3 , true ) ;
64+ if ( interval_count === 3 )
65+ clearInterval ( this ) ;
66+ } , 3 ) , 1000 ) ;
67+ }
6168
6269
6370// Single param:
64- setTimeout ( function ( param ) {
65- assert . strictEqual ( param , 'test param' ) ;
66- } , 1000 , 'test param' ) ;
71+ {
72+ setTimeout ( function ( param ) {
73+ assert . strictEqual ( param , 'test param' ) ;
74+ } , 1000 , 'test param' ) ;
75+ }
6776
68- let interval_count2 = 0 ;
69- setInterval ( function ( param ) {
70- ++ interval_count2 ;
71- assert . strictEqual ( param , 'test param' ) ;
77+ {
78+ let interval_count = 0 ;
79+ setInterval ( function ( param ) {
80+ ++ interval_count ;
81+ assert . strictEqual ( param , 'test param' ) ;
7282
73- if ( interval_count2 === 3 )
74- clearInterval ( this ) ;
75- } , 1000 , 'test param' ) ;
83+ if ( interval_count === 3 )
84+ clearInterval ( this ) ;
85+ } , 1000 , 'test param' ) ;
86+ }
7687
7788
7889// Multiple param
79- setTimeout ( function ( param1 , param2 ) {
80- assert . strictEqual ( param1 , 'param1' ) ;
81- assert . strictEqual ( param2 , 'param2' ) ;
82- } , 1000 , 'param1' , 'param2' ) ;
90+ {
91+ setTimeout ( function ( param1 , param2 ) {
92+ assert . strictEqual ( param1 , 'param1' ) ;
93+ assert . strictEqual ( param2 , 'param2' ) ;
94+ } , 1000 , 'param1' , 'param2' ) ;
95+ }
8396
84- let interval_count3 = 0 ;
85- setInterval ( function ( param1 , param2 ) {
86- ++ interval_count3 ;
87- assert . strictEqual ( param1 , 'param1' ) ;
88- assert . strictEqual ( param2 , 'param2' ) ;
97+ {
98+ let interval_count = 0 ;
99+ setInterval ( function ( param1 , param2 ) {
100+ ++ interval_count ;
101+ assert . strictEqual ( param1 , 'param1' ) ;
102+ assert . strictEqual ( param2 , 'param2' ) ;
89103
90- if ( interval_count3 === 3 )
91- clearInterval ( this ) ;
92- } , 1000 , 'param1' , 'param2' ) ;
104+ if ( interval_count === 3 )
105+ clearInterval ( this ) ;
106+ } , 1000 , 'param1' , 'param2' ) ;
107+ }
93108
94109// setInterval(cb, 0) should be called multiple times.
95- let count4 = 0 ;
96- const interval4 = setInterval ( function ( ) {
97- if ( ++ count4 > 10 ) clearInterval ( interval4 ) ;
98- } , 0 ) ;
99-
100-
101- // We should be able to clearTimeout multiple times without breakage.
102- let expectedTimeouts = 3 ;
103-
104- function t ( ) {
105- expectedTimeouts -- ;
110+ {
111+ let count = 0 ;
112+ const interval = setInterval ( common . mustCall ( function ( ) {
113+ if ( ++ count > 10 ) clearInterval ( interval ) ;
114+ } , 11 ) , 0 ) ;
106115}
107116
108- setTimeout ( t , 200 ) ;
109- setTimeout ( t , 200 ) ;
110- const y = setTimeout ( t , 200 ) ;
111-
112- clearTimeout ( y ) ;
113- setTimeout ( t , 200 ) ;
114- clearTimeout ( y ) ;
115-
117+ // We should be able to clearTimeout multiple times without breakage.
118+ {
119+ const t = common . mustCall ( 3 ) ;
116120
117- process . on ( 'exit' , function ( ) {
118- assert . strictEqual ( interval_count , 3 ) ;
119- assert . strictEqual ( count4 , 11 ) ;
121+ setTimeout ( t , 200 ) ;
122+ setTimeout ( t , 200 ) ;
123+ const y = setTimeout ( t , 200 ) ;
120124
121- // Check that the correct number of timers ran.
122- assert . strictEqual ( expectedTimeouts , 0 ) ;
123- } ) ;
125+ clearTimeout ( y ) ;
126+ setTimeout ( t , 200 ) ;
127+ clearTimeout ( y ) ;
128+ }
0 commit comments