@@ -249,15 +249,15 @@ describe('Test TokenBucket Rate Limiter', () => {
249249 const result = await limiter . processRequest (
250250 user4 ,
251251 timestamp + WINDOW_SIZE * 1.99 ,
252- 4
252+ 10
253253 ) ;
254254 expect ( result . tokens ) . toBe ( 0 ) ;
255255 expect ( result . success ) . toBe ( true ) ;
256256
257257 // currentTokens (in current fixed window): 4
258258 // previousTokens (in previous fixed window): 8
259259 const count1 = await getWindowFromClient ( client , user4 ) ;
260- expect ( count1 . currentTokens ) . toBe ( 4 ) ;
260+ expect ( count1 . currentTokens ) . toBe ( 10 ) ;
261261 expect ( count1 . previousTokens ) . toBe ( 8 ) ;
262262 } ) ;
263263 } ) ;
@@ -304,7 +304,7 @@ describe('Test TokenBucket Rate Limiter', () => {
304304
305305 // 3 + 8 * 1 = 11, above capacity (request should be blocked)
306306 const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE , 3 ) ;
307- expect ( result . tokens ) . toBe ( 10 ) ;
307+ expect ( result . tokens ) . toBe ( 2 ) ;
308308 expect ( result . success ) . toBe ( false ) ;
309309
310310 // currentTokens (in current fixed window): 0
@@ -332,7 +332,7 @@ describe('Test TokenBucket Rate Limiter', () => {
332332 timestamp + WINDOW_SIZE * 1.25 ,
333333 5
334334 ) ;
335- expect ( result . tokens ) . toBe ( 10 ) ;
335+ expect ( result . tokens ) . toBe ( 4 ) ;
336336 expect ( result . success ) . toBe ( false ) ;
337337
338338 // currentTokens (in current fixed window): 0
@@ -341,80 +341,88 @@ describe('Test TokenBucket Rate Limiter', () => {
341341 expect ( count1 . currentTokens ) . toBe ( 0 ) ;
342342 expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
343343 } ) ;
344- } ) ;
345344
346- test ( 'rolling window at 50% blocks requests over allowed limit set by formula' , async ( ) => {
347- // 50% of rolling window present in previous fixed window
348- // 1.5*60000 = 90000 (time after initial fixedWindowStart
349- // to set rolling window at 50% of previous fixed window)
345+ test ( 'rolling window at 50% blocks requests over allowed limit set by formula' , async ( ) => {
346+ // 50% of rolling window present in previous fixed window
347+ // 1.5*60000 = 90000 (time after initial fixedWindowStart
348+ // to set rolling window at 50% of previous fixed window)
350349
351- // to set initial fixedWindowStart
352- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
350+ // to set initial fixedWindowStart
351+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
353352
354- const initRequest = 8 ;
353+ const initRequest = 8 ;
355354
356- // large request at very end of first fixed window
357- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
355+ // large request at very end of first fixed window
356+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
358357
359- // 7 + 8 * .5 = 11, over capacity (request should be blocked)
360- const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE * 1.5 , 7 ) ;
361- expect ( result . tokens ) . toBe ( 10 ) ;
362- expect ( result . success ) . toBe ( false ) ;
358+ // 7 + 8 * .5 = 11, over capacity (request should be blocked)
359+ const result = await limiter . processRequest (
360+ user4 ,
361+ timestamp + WINDOW_SIZE * 1.5 ,
362+ 7
363+ ) ;
364+ expect ( result . tokens ) . toBe ( 6 ) ;
365+ expect ( result . success ) . toBe ( false ) ;
363366
364- // currentTokens (in current fixed window): 0
365- // previousTokens (in previous fixed window): 8
366- const count = await getWindowFromClient ( client , user4 ) ;
367- expect ( count . currentTokens ) . toBe ( 0 ) ;
368- expect ( count . previousTokens ) . toBe ( initRequest ) ;
369- } ) ;
367+ // currentTokens (in current fixed window): 0
368+ // previousTokens (in previous fixed window): 8
369+ const count = await getWindowFromClient ( client , user4 ) ;
370+ expect ( count . currentTokens ) . toBe ( 0 ) ;
371+ expect ( count . previousTokens ) . toBe ( initRequest ) ;
372+ } ) ;
370373
371- test ( 'rolling window at 25% blocks requests over allowed limit set by formula' , async ( ) => {
372- // 25% of rolling window present in previous fixed window
373- // 1.75*60000 = 105000 (time after initial fixedWindowStart
374- // to set rolling window at 25% of previous fixed window)
374+ test ( 'rolling window at 25% blocks requests over allowed limit set by formula' , async ( ) => {
375+ // 25% of rolling window present in previous fixed window
376+ // 1.75*60000 = 105000 (time after initial fixedWindowStart
377+ // to set rolling window at 25% of previous fixed window)
375378
376- // to set initial fixedWindowStart
377- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
379+ // to set initial fixedWindowStart
380+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
378381
379- const initRequest = 8 ;
382+ const initRequest = 8 ;
380383
381- // large request at very end of first fixed window
382- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
384+ // large request at very end of first fixed window
385+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
383386
384- // 9 + 8 * .25 = 11, over capacity (request should be blocked)
385- const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE * 1.75 , 9 ) ;
386- expect ( result . tokens ) . toBe ( 10 ) ;
387- expect ( result . success ) . toBe ( false ) ;
387+ // 9 + 8 * .25 = 11, over capacity (request should be blocked)
388+ const result = await limiter . processRequest (
389+ user4 ,
390+ timestamp + WINDOW_SIZE * 1.75 ,
391+ 9
392+ ) ;
393+ expect ( result . tokens ) . toBe ( 8 ) ;
394+ expect ( result . success ) . toBe ( false ) ;
388395
389- // currentTokens (in current fixed window): 0
390- // previousTokens (in previous fixed window): 8
391- const count = await getWindowFromClient ( client , user4 ) ;
392- expect ( count . currentTokens ) . toBe ( 0 ) ;
393- expect ( count . previousTokens ) . toBe ( initRequest ) ;
394- } ) ;
395- test ( 'rolling window at 100% blocks requests over allowed limit set by formula' , async ( ) => {
396- // 1% of rolling window present in previous fixed window
397- // .01*60000 = 600 (time after initial fixedWindowStart
398- // to set rolling window at 100% of previous fixed window)
396+ // currentTokens (in current fixed window): 0
397+ // previousTokens (in previous fixed window): 8
398+ const count = await getWindowFromClient ( client , user4 ) ;
399+ expect ( count . currentTokens ) . toBe ( 0 ) ;
400+ expect ( count . previousTokens ) . toBe ( initRequest ) ;
401+ } ) ;
402+ test ( 'rolling window at 100% blocks requests over allowed limit set by formula' , async ( ) => {
403+ // 1% of rolling window present in previous fixed window
404+ // .01*60000 = 600 (time after initial fixedWindowStart
405+ // to set rolling window at 100% of previous fixed window)
399406
400- // to set initial fixedWindowStart
401- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
407+ // to set initial fixedWindowStart
408+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
402409
403- const initRequest = 8 ;
410+ const initRequest = 8 ;
404411
405- // large request at very end of first fixed window
406- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
412+ // large request at very end of first fixed window
413+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
407414
408- // 11 + 8 * .01 = 11, above capacity (request should be blocked)
409- const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE , 11 ) ;
410- expect ( result . tokens ) . toBe ( 10 ) ;
411- expect ( result . success ) . toBe ( false ) ;
415+ // 11 + 8 * .01 = 11, above capacity (request should be blocked)
416+ const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE , 11 ) ;
417+ expect ( result . tokens ) . toBe ( 2 ) ;
418+ expect ( result . success ) . toBe ( false ) ;
412419
413- // currentTokens (in current fixed window): 0
414- // previousTokens (in previous fixed window): 8
415- const count1 = await getWindowFromClient ( client , user4 ) ;
416- expect ( count1 . currentTokens ) . toBe ( 0 ) ;
417- expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
420+ // currentTokens (in current fixed window): 0
421+ // previousTokens (in previous fixed window): 8
422+ const count1 = await getWindowFromClient ( client , user4 ) ;
423+ expect ( count1 . currentTokens ) . toBe ( 0 ) ;
424+ expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
425+ } ) ;
418426 } ) ;
419427 } ) ;
420428
@@ -465,7 +473,7 @@ describe('Test TokenBucket Rate Limiter', () => {
465473 await (
466474 await limiter . processRequest ( user1 , timestamp + WINDOW_SIZE , 4 )
467475 ) . tokens
468- ) . toBe ( 2 ) ;
476+ ) . toBe ( 1 ) ;
469477 // currentTokens (in current fixed window): 0
470478 // previousTokens (in previous fixed window): 8
471479 const count = await getWindowFromClient ( client , user1 ) ;
0 commit comments