@@ -18,8 +18,8 @@ describe("Batcher", function () {
1818 var t0 = Date . now ( ) ;
1919 var batches = [ ] ;
2020
21- batcher . on ( "batch" , function ( batcher ) {
22- batches . push ( batcher ) ;
21+ batcher . on ( "batch" , function ( groups ) {
22+ batches . push ( groups ) ;
2323 } ) ;
2424
2525 return Promise . all ( [
@@ -60,8 +60,8 @@ describe("Batcher", function () {
6060 var t0 = Date . now ( ) ;
6161 var batches = [ ] ;
6262
63- batcher . on ( "batch" , function ( batcher ) {
64- batches . push ( batcher ) ;
63+ batcher . on ( "batch" , function ( groups ) {
64+ batches . push ( groups ) ;
6565 } ) ;
6666
6767 return Promise . all ( [
@@ -109,8 +109,8 @@ describe("Batcher", function () {
109109 } ) ;
110110 var batches = [ ] ;
111111
112- batcher . on ( "batch" , function ( batcher ) {
113- batches . push ( batcher ) ;
112+ batcher . on ( "batch" , function ( groups ) {
113+ batches . push ( groups ) ;
114114 } ) ;
115115
116116 return Promise . all ( [
@@ -133,63 +133,55 @@ describe("Batcher", function () {
133133 } ) ;
134134 } ) ;
135135
136- it ( "Should stagger flushes" , function ( ) {
136+ it ( "Should stagger flushes" , async function ( ) {
137137 c = makeTest ( ) ;
138- var batcher = new Bottleneck . Batcher ( {
138+ const batcher = new Bottleneck . Batcher ( {
139139 maxTime : 50 ,
140140 maxSize : 3 ,
141141 } ) ;
142- var t0 = Date . now ( ) ;
143- var batches = [ ] ;
142+ const t0 = Date . now ( ) ;
143+ const batches = [ ] ;
144144
145- batcher . on ( "batch" , function ( batcher ) {
146- batches . push ( batcher ) ;
145+ batcher . on ( "batch" , function ( groups ) {
146+ batches . push ( groups ) ;
147147 } ) ;
148148
149- return Promise . all ( [
150- batcher . add ( 1 ) . then ( ( _x ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 1 ) ) ,
151- batcher . add ( 2 ) . then ( ( _x ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 2 ) ) ,
152- ] )
153- . then ( function ( data ) {
154- c . mustEqual (
155- data . map ( ( [ t , x ] ) => [ Math . floor ( t / 50 ) , x ] ) ,
156- [
157- [ 1 , 1 ] ,
158- [ 1 , 2 ] ,
159- ] ,
160- ) ;
149+ const [ first , second ] = await Promise . all ( [
150+ batcher . add ( 1 ) . then ( ( ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 1 ) ) ,
151+ batcher . add ( 2 ) . then ( ( ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 2 ) ) ,
152+ ] ) ;
161153
162- var promises = [ ] ;
163- promises . push (
164- batcher . add ( 3 ) . then ( ( _x ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 3 ) ) ,
165- ) ;
154+ c . mustGte ( first [ 0 ] , 50 ) ;
155+ c . mustEqual ( first [ 1 ] , 1 ) ;
166156
167- return c . wait ( 10 ) . then ( function ( ) {
168- promises . push (
169- batcher . add ( 4 ) . then ( ( _x ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 4 ) ) ,
170- ) ;
157+ c . mustGte ( second [ 0 ] , 50 ) ;
158+ c . mustEqual ( second [ 1 ] , 2 ) ;
171159
172- return Promise . all ( promises ) ;
173- } ) ;
174- } )
175- . then ( function ( data ) {
176- c . mustEqual (
177- data . map ( ( [ t , x ] ) => [ Math . floor ( t / 50 ) , x ] ) ,
178- [
179- [ 2 , 3 ] ,
180- [ 2 , 4 ] ,
181- ] ,
182- ) ;
160+ const promises = [
161+ batcher . add ( 3 ) . then ( ( ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 3 ) ) ,
162+ ] ;
183163
184- return c . last ( ) ;
185- } )
186- . then ( function ( _results ) {
187- c . checkDuration ( 120 , 20 ) ;
188- c . mustEqual ( batches , [
189- [ 1 , 2 ] ,
190- [ 3 , 4 ] ,
191- ] ) ;
192- } ) ;
164+ await c . wait ( 10 ) ;
165+
166+ promises . push (
167+ batcher . add ( 4 ) . then ( ( ) => c . limiter . schedule ( c . promise , null , Date . now ( ) - t0 , 4 ) ) ,
168+ ) ;
169+
170+ const [ third , fourth ] = await Promise . all ( promises ) ;
171+
172+ c . mustGte ( third [ 0 ] , 100 ) ;
173+ c . mustEqual ( third [ 1 ] , 3 ) ;
174+
175+ c . mustGte ( fourth [ 0 ] , 100 ) ;
176+ c . mustEqual ( fourth [ 1 ] , 4 ) ;
177+
178+ await c . last ( ) ;
179+
180+ c . checkDuration ( 120 , 20 ) ;
181+ c . mustEqual ( batches , [
182+ [ 1 , 2 ] ,
183+ [ 3 , 4 ] ,
184+ ] ) ;
193185 } ) ;
194186
195187 it ( "Should force then stagger flushes" , function ( ) {
@@ -201,8 +193,8 @@ describe("Batcher", function () {
201193 var t0 = Date . now ( ) ;
202194 var batches = [ ] ;
203195
204- batcher . on ( "batch" , function ( batcher ) {
205- batches . push ( batcher ) ;
196+ batcher . on ( "batch" , function ( groups ) {
197+ batches . push ( groups ) ;
206198 } ) ;
207199
208200 var promises = [ ] ;
0 commit comments