1- import { Redis as RedisType } from 'ioredis' ;
1+ import * as ioredis from 'ioredis' ;
22import TokenBucket from '../../src/rateLimiters/tokenBucket' ;
33
44const RedisMock = require ( 'ioredis-mock' ) ;
55
6+ /** trying to solve ts-eslint error with require */
7+
8+ // import * as ioredis from 'ioredis';
9+ // // import RedisMock from 'ioredis-mock';
10+ // // import { createRequire } from 'module'; // this just makes it so can use require for ioredis-mock
11+ // import TokenBucket from '../../src/rateLimiters/tokenBucket';
12+ // const RedisMock = require('ioredis-mock');
13+ // // const c = new RedisMock();
14+
615const CAPACITY = 10 ;
716// FIXME: Changing the refill rate effects test outcomes.
817const REFILL_RATE = 1 ; // 1 token per second
918
1019let limiter : TokenBucket ;
11- let client : RedisType ;
20+ let client : ioredis . Redis ;
1221let timestamp : number ;
1322const user1 = '1' ;
1423const user2 = '2' ;
1524const user3 = '3' ;
1625const user4 = '4' ;
1726
18- async function getBucketFromClient ( redisClient : RedisType , uuid : string ) : Promise < RedisBucket > {
27+ async function getBucketFromClient ( redisClient : ioredis . Redis , uuid : string ) : Promise < RedisBucket > {
1928 const res = await redisClient . get ( uuid ) ;
2029 // if no uuid is found, return -1 for tokens and timestamp, which are both impossible
2130 if ( res === null ) return { tokens : - 1 , timestamp : - 1 } ;
2231 return JSON . parse ( res ) ;
2332}
2433
2534async function setTokenCountInClient (
26- redisClient : RedisType ,
35+ redisClient : ioredis . Redis ,
2736 uuid : string ,
2837 tokens : number ,
2938 time : number
@@ -45,7 +54,7 @@ describe('Test TokenBucket Rate Limiter', () => {
4554 describe ( 'TokenBucket returns correct number of tokens and updates redis store as expected' , ( ) => {
4655 describe ( 'after an ALLOWED request...' , ( ) => {
4756 afterEach ( ( ) => {
48- limiter . reset ( ) ;
57+ client . flushall ( ) ;
4958 } ) ;
5059 test ( 'bucket is initially full' , async ( ) => {
5160 // Bucket intially full
@@ -98,7 +107,7 @@ describe('Test TokenBucket Rate Limiter', () => {
98107 let redisData : RedisBucket ;
99108
100109 afterAll ( ( ) => {
101- limiter . reset ( ) ;
110+ client . flushall ( ) ;
102111 } ) ;
103112
104113 test ( 'where intial request is greater than bucket capacity' , async ( ) => {
@@ -136,7 +145,7 @@ describe('Test TokenBucket Rate Limiter', () => {
136145
137146 describe ( 'Token Bucket functions as expected' , ( ) => {
138147 afterEach ( ( ) => {
139- limiter . reset ( ) ;
148+ client . flushall ( ) ;
140149 } ) ;
141150 test ( 'allows a user to consume up to their current allotment of tokens' , async ( ) => {
142151 // "free requests"
@@ -208,7 +217,7 @@ describe('Test TokenBucket Rate Limiter', () => {
208217 } ) ;
209218
210219 test ( 'bucket allows custom refill rates' , async ( ) => {
211- const doubleRefillClient : RedisType = new RedisMock ( ) ;
220+ const doubleRefillClient : ioredis . Redis = new RedisMock ( ) ;
212221 limiter = new TokenBucket ( CAPACITY , 2 , doubleRefillClient ) ;
213222
214223 await setTokenCountInClient ( doubleRefillClient , user1 , 0 , timestamp ) ;
0 commit comments