@@ -53,17 +53,35 @@ describe('Test FixedWindow Rate Limiter', () => {
5353 } ) ;
5454 test ( 'reached 40% capacity in current time window and still can pass request' , async ( ) => {
5555 const initial = 5 ;
56+ await setTokenCountInClient ( client , user2 , initial , timestamp ) ;
5657 const partialWithdraw = 2 ;
5758 expect (
58- ( await limiter . processRequest ( user2 , timestamp , initial + partialWithdraw ) )
59- . tokens
59+ (
60+ await limiter . processRequest (
61+ user2 ,
62+ timestamp + WINDOW_SIZE * 0.4 ,
63+ partialWithdraw
64+ )
65+ ) . tokens
6066 ) . toBe ( CAPACITY - initial - partialWithdraw ) ;
6167
6268 const tokenCountPartial = await getWindowFromClient ( client , user2 ) ;
6369 expect ( tokenCountPartial . currentTokens ) . toBe ( initial + partialWithdraw ) ;
6470 } ) ;
6571
6672 test ( 'window is partially full and request has no leftover tokens' , async ( ) => {
73+ const initial = 6 ;
74+ const partialWithdraw = 4 ;
75+ await setTokenCountInClient ( client , user2 , initial , timestamp ) ;
76+ expect (
77+ ( await limiter . processRequest ( user2 , timestamp , partialWithdraw ) ) . success
78+ ) . toBe ( true ) ;
79+ expect (
80+ ( await limiter . processRequest ( user2 , timestamp , partialWithdraw ) ) . tokens
81+ ) . toBe ( 0 ) ;
82+ } ) ;
83+
84+ test ( 'window is partially full and request exceeds tokens in availability' , async ( ) => {
6785 const initial = 6 ;
6886 const partialWithdraw = 5 ;
6987 await setTokenCountInClient ( client , user2 , initial , timestamp ) ;
0 commit comments