@@ -14,7 +14,6 @@ let timestamp: number;
1414const user1 = '1' ;
1515const user2 = '2' ;
1616const user3 = '3' ;
17- const user4 = '4' ;
1817
1918async function getWindowFromClient ( redisClient : ioredis . Redis , uuid : string ) : Promise < RedisWindow > {
2019 const res = await redisClient . get ( uuid ) ;
@@ -59,24 +58,21 @@ describe('Test FixedWindow Rate Limiter', () => {
5958 ( await limiter . processRequest ( user2 , timestamp , initial + partialWithdraw ) )
6059 . tokens
6160 ) . toBe ( CAPACITY - initial - partialWithdraw ) ;
62- // TODO: using setWindowClient first and update the capacity (not working)
63- // TODO: might need to look into why is not taking second request...
6461
6562 const tokenCountPartial = await getWindowFromClient ( client , user2 ) ;
6663 expect ( tokenCountPartial . currentTokens ) . toBe ( initial + partialWithdraw ) ;
6764 } ) ;
6865
69- // TODO: need to fix processRequest Fn()
70-
71- xtest ( 'window is partially full and request has no leftover tokens' , async ( ) => {
66+ test ( 'window is partially full and request has no leftover tokens' , async ( ) => {
7267 const initial = 6 ;
7368 const partialWithdraw = 5 ;
7469 await setTokenCountInClient ( client , user2 , initial , timestamp ) ;
7570 expect (
7671 ( await limiter . processRequest ( user2 , timestamp , partialWithdraw ) ) . success
7772 ) . toBe ( false ) ;
78- const tokenCountPartialToEmpty = await getWindowFromClient ( client , user2 ) ;
79- expect ( tokenCountPartialToEmpty . currentTokens ) . toBe ( 10 ) ;
73+ expect (
74+ ( await limiter . processRequest ( user2 , timestamp , partialWithdraw ) ) . tokens
75+ ) . toBe ( 0 ) ;
8076 } ) ;
8177 } ) ;
8278 describe ( 'after a BLOCKED request...' , ( ) => {
@@ -89,17 +85,15 @@ describe('Test FixedWindow Rate Limiter', () => {
8985 // expect current tokens in the window to still be 0
9086 expect ( ( await getWindowFromClient ( client , user1 ) ) . currentTokens ) . toBe ( 0 ) ;
9187 } ) ;
92- xtest ( 'window is partially full but not enough time elapsed to reach new window' , async ( ) => {
88+ test ( 'window is partially full but not enough time elapsed to reach new window' , async ( ) => {
9389 const requestedTokens = 9 ;
9490
9591 await setTokenCountInClient ( client , user2 , requestedTokens , timestamp ) ;
9692 // expect remaining tokens to be 1, b/c the 2-token-request should be blocked
97- // TODO: fix processRequest function
9893 const result = await limiter . processRequest ( user2 , timestamp + WINDOW_SIZE - 1 , 2 ) ;
9994
10095 expect ( result . success ) . toBe ( false ) ;
101- // TODO: fix this, (expect 1, but get 9 -- which is # of currentTokens)
102- expect ( result . tokens ) . toBe ( CAPACITY - requestedTokens ) ;
96+ expect ( result . tokens ) . toBe ( 0 ) ;
10397
10498 // expect current tokens in the window to still be 9
10599 expect ( ( await getWindowFromClient ( client , user2 ) ) . currentTokens ) . toBe ( 9 ) ;
@@ -109,7 +103,7 @@ describe('Test FixedWindow Rate Limiter', () => {
109103 afterEach ( ( ) => {
110104 client . flushall ( ) ;
111105 } ) ;
112- xtest ( 'New window is initialized after reaching the window size' , async ( ) => {
106+ test ( 'New window is initialized after reaching the window size' , async ( ) => {
113107 const fullRequest = 10 ;
114108 await setTokenCountInClient ( client , user3 , fullRequest , timestamp ) ;
115109 const noAccess = await limiter . processRequest (
@@ -118,16 +112,16 @@ describe('Test FixedWindow Rate Limiter', () => {
118112 1
119113 ) ;
120114
121- // TODO: fix processRequest function
122115 // expect cannot pass any request
116+ expect ( noAccess . tokens ) . toBe ( 0 ) ;
123117 expect ( noAccess . success ) . toBe ( false ) ;
124118
125119 const newRequest = 3 ;
126120 await setTokenCountInClient ( client , user3 , newRequest , timestamp + WINDOW_SIZE + 1 ) ;
127121 const newAccess = await limiter . processRequest ( user3 , timestamp , 1 ) ;
128122
129123 expect ( newAccess . success ) . toBe ( true ) ;
130- expect ( newAccess . tokens ) . toBe ( 4 ) ;
124+ expect ( newAccess . tokens ) . toBe ( 6 ) ;
131125 } ) ;
132126 } ) ;
133127 } ) ;
0 commit comments