@@ -91,6 +91,7 @@ describe('Test TokenBucket Rate Limiter', () => {
9191 await setTokenCountInClient ( client , user4 , 10 , null , timestamp ) ;
9292 // tokens returned in processRequest is equal to the capacity
9393 // still available in the fixed window
94+
9495 expect (
9596 ( await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE + 1 , 1 ) ) . tokens
9697 ) . toBe ( 0 ) ; // here, we expect the rolling window to only allow 1 token, b/c
@@ -199,8 +200,8 @@ describe('Test TokenBucket Rate Limiter', () => {
199200 ( await limiter . processRequest ( user2 , timestamp + WINDOW_SIZE , 5 ) ) . tokens
200201 ) . toBe ( CAPACITY - initRequest ) ;
201202
202- // expect current tokens in the window to still be 0
203- expect ( ( await getWindowFromClient ( client , user2 ) ) . currentTokens ) . toBe ( 0 ) ;
203+ // expect current tokens in the window to still be 6
204+ expect ( ( await getWindowFromClient ( client , user2 ) ) . currentTokens ) . toBe ( 6 ) ;
204205 } ) ;
205206
206207 // 3 rolling window tests with different proportions (.25, .5, .75)
@@ -276,7 +277,7 @@ describe('Test TokenBucket Rate Limiter', () => {
276277 // currentTokens (in current fixed window): 0
277278 // previousTokens (in previous fixed window): 8
278279 const count = await getWindowFromClient ( client , user4 ) ;
279- expect ( count . currentTokens ) . toBe ( 4 ) ;
280+ expect ( count . currentTokens ) . toBe ( 0 ) ;
280281 expect ( count . previousTokens ) . toBe ( initRequest ) ;
281282 } ) ;
282283 } ) ;
@@ -321,20 +322,19 @@ describe('Test TokenBucket Rate Limiter', () => {
321322 } ) ;
322323
323324 test ( 'fixed window and current/previous tokens update as expected' , async ( ) => {
324- await setTokenCountInClient ( client , user1 , 0 , null , timestamp ) ;
325- // fills first window with 4 tokens
325+ // fills first window with 5 tokens
326326 await limiter . processRequest ( user1 , timestamp , 5 ) ;
327- // fills second window with 5 tokens
327+ // fills second window with 4 tokens
328328 expect (
329329 await (
330330 await limiter . processRequest ( user1 , timestamp + WINDOW_SIZE + 1 , 4 )
331331 ) . tokens
332- ) . toBe ( 6 ) ;
332+ ) . toBe ( 2 ) ;
333333 // currentTokens (in current fixed window): 0
334334 // previousTokens (in previous fixed window): 8
335- const count = await getWindowFromClient ( client , user4 ) ;
335+ const count = await getWindowFromClient ( client , user1 ) ;
336336 // ensures that fixed window is updated when a request goes over
337- expect ( count . fixedWindowStart ) . toBe ( timestamp + WINDOW_SIZE ) ;
337+ expect ( count . fixedWindowStart ) . toBe ( timestamp + WINDOW_SIZE + 1 ) ;
338338 // ensures that previous tokens property updates on fixed window change
339339 expect ( count . previousTokens ) . toBe ( 5 ) ;
340340 // ensures that current tokens only represents tokens from current window requests
@@ -348,10 +348,11 @@ describe('Test TokenBucket Rate Limiter', () => {
348348
349349 await newLimiter . processRequest ( user1 , timestamp , 8 ) ;
350350
351- // expect that a new window is entered, leaving 9 tokens available after a 1 token request
351+ // expect that a new window is entered, leaving 2 tokens available after both requests
352+ // 8 * .99 -> 7 (floored) + 1 = 8
352353 expect (
353354 ( await newLimiter . processRequest ( user1 , timestamp + newWindowSize + 1 , 1 ) ) . tokens
354- ) . toBe ( 9 ) ;
355+ ) . toBe ( 2 ) ;
355356 } ) ;
356357
357358 test ( 'sliding window allows custom capacities' , async ( ) => {
0 commit comments