1- import redis from 'redis-mock' ;
2- import { RedisClientType } from 'redis' ;
1+ import * as ioredis from 'ioredis' ;
32import TokenBucket from '../../src/rateLimiters/tokenBucket' ;
43
4+ // eslint-disable-next-line @typescript-eslint/no-var-requires
5+ const RedisMock = require ( 'ioredis-mock' ) ;
6+
57const CAPACITY = 10 ;
68// FIXME: Changing the refill rate effects test outcomes.
79const REFILL_RATE = 1 ; // 1 token per second
810
911let limiter : TokenBucket ;
10- let client : RedisClientType ;
12+ let client : ioredis . Redis ;
1113let timestamp : number ;
1214const user1 = '1' ;
1315const user2 = '2' ;
1416const user3 = '3' ;
1517const user4 = '4' ;
1618
17- async function getBucketFromClient (
18- redisClient : RedisClientType ,
19- uuid : string
20- ) : Promise < RedisBucket > {
19+ async function getBucketFromClient ( redisClient : ioredis . Redis , uuid : string ) : Promise < RedisBucket > {
2120 return redisClient . get ( uuid ) . then ( ( res ) => JSON . parse ( res || '{}' ) ) ;
2221}
2322
2423async function setTokenCountInClient (
25- redisClient : RedisClientType ,
24+ redisClient : ioredis . Redis ,
2625 uuid : string ,
2726 tokens : number ,
2827 time : number
@@ -36,7 +35,7 @@ xdescribe('Test TokenBucket Rate Limiter', () => {
3635 // Initialize a new token bucket before each test
3736 // create a mock user
3837 // intialze the token bucket algorithm
39- client = redis . createClient ( ) ;
38+ client = new RedisMock ( ) ;
4039 limiter = new TokenBucket ( CAPACITY , REFILL_RATE , client ) ;
4140 timestamp = new Date ( ) . valueOf ( ) ;
4241 } ) ;
@@ -197,7 +196,7 @@ xdescribe('Test TokenBucket Rate Limiter', () => {
197196 } ) ;
198197
199198 test ( 'bucket allows custom refill rates' , async ( ) => {
200- const doubleRefillClient : RedisClientType = redis . createClient ( ) ;
199+ const doubleRefillClient : ioredis . Redis = new RedisMock ( ) ;
201200 limiter = new TokenBucket ( CAPACITY , 2 , doubleRefillClient ) ;
202201
203202 await setTokenCountInClient ( doubleRefillClient , user1 , 0 , timestamp ) ;
0 commit comments