|
| 1 | +import * as ioredis from 'ioredis'; |
| 2 | +import { FixedWindow as Window } from '../../src/@types/rateLimit'; |
| 3 | +import FixedWindow from '../../src/rateLimiters/fixedWindow'; |
| 4 | + |
| 5 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 6 | +const RedisMock = require('ioredis-mock'); |
| 7 | + |
| 8 | +const CAPACITY = 10; |
| 9 | +const WINDOW_SIZE = 6000; |
| 10 | + |
| 11 | +let limiter: FixedWindow; |
| 12 | +let client: ioredis.Redis; |
| 13 | +let timestamp: number; |
| 14 | +const user1 = '1'; |
| 15 | +const user2 = '2'; |
| 16 | +const user3 = '3'; |
| 17 | + |
| 18 | +async function getWindowFromClient(redisClient: ioredis.Redis, uuid: string): Promise<Window> { |
| 19 | + const res = await redisClient.get(uuid); |
| 20 | + // if no uuid is found, return -1 for tokens and timestamp, which are both impossible |
| 21 | + if (res === null) return { currentTokens: -1, fixedWindowStart: -1 }; |
| 22 | + return JSON.parse(res); |
| 23 | +} |
| 24 | + |
| 25 | +async function setTokenCountInClient( |
| 26 | + redisClient: ioredis.Redis, |
| 27 | + uuid: string, |
| 28 | + currentTokens: number, |
| 29 | + fixedWindowStart: number |
| 30 | +) { |
| 31 | + const value: Window = { currentTokens, fixedWindowStart }; |
| 32 | + await redisClient.set(uuid, JSON.stringify(value)); |
| 33 | +} |
| 34 | +describe('Test FixedWindow Rate Limiter', () => { |
| 35 | + beforeEach(async () => { |
| 36 | + client = new RedisMock(); |
| 37 | + limiter = new FixedWindow(CAPACITY, WINDOW_SIZE, client); |
| 38 | + timestamp = new Date().valueOf(); |
| 39 | + }); |
| 40 | + describe('FixedWindow returns correct number of tokens and updates redis store as expected', () => { |
| 41 | + describe('after an ALLOWED request...', () => { |
| 42 | + afterEach(() => { |
| 43 | + client.flushall(); |
| 44 | + }); |
| 45 | + test('current time window has no token initially', async () => { |
| 46 | + // zero token used in this time window |
| 47 | + const withdraw5 = 5; |
| 48 | + expect((await limiter.processRequest(user1, timestamp, withdraw5)).tokens).toBe( |
| 49 | + CAPACITY - withdraw5 |
| 50 | + ); |
| 51 | + const tokenCountFull = await getWindowFromClient(client, user1); |
| 52 | + expect(tokenCountFull.currentTokens).toBe(5); |
| 53 | + }); |
| 54 | + test('reached 40% capacity in current time window and still can pass request', async () => { |
| 55 | + const initial = 5; |
| 56 | + await setTokenCountInClient(client, user2, initial, timestamp); |
| 57 | + const partialWithdraw = 2; |
| 58 | + expect( |
| 59 | + ( |
| 60 | + await limiter.processRequest( |
| 61 | + user2, |
| 62 | + timestamp + WINDOW_SIZE * 0.4, |
| 63 | + partialWithdraw |
| 64 | + ) |
| 65 | + ).tokens |
| 66 | + ).toBe(CAPACITY - initial - partialWithdraw); |
| 67 | + |
| 68 | + const tokenCountPartial = await getWindowFromClient(client, user2); |
| 69 | + expect(tokenCountPartial.currentTokens).toBe(initial + partialWithdraw); |
| 70 | + }); |
| 71 | + |
| 72 | + 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 () => { |
| 85 | + const initial = 6; |
| 86 | + const partialWithdraw = 5; |
| 87 | + await setTokenCountInClient(client, user2, initial, timestamp); |
| 88 | + expect( |
| 89 | + (await limiter.processRequest(user2, timestamp, partialWithdraw)).success |
| 90 | + ).toBe(false); |
| 91 | + expect( |
| 92 | + (await limiter.processRequest(user2, timestamp, partialWithdraw)).tokens |
| 93 | + ).toBe(4); |
| 94 | + }); |
| 95 | + }); |
| 96 | + describe('after a BLOCKED request...', () => { |
| 97 | + afterEach(() => { |
| 98 | + client.flushall(); |
| 99 | + }); |
| 100 | + test('initial request is greater than capacity', async () => { |
| 101 | + // expect remaining tokens to be 10, b/c the 11 token request should be blocked |
| 102 | + expect((await limiter.processRequest(user1, timestamp, 11)).success).toBe(false); |
| 103 | + // expect current tokens in the window to still be 0 |
| 104 | + expect((await getWindowFromClient(client, user1)).currentTokens).toBe(0); |
| 105 | + }); |
| 106 | + test('window is partially full but not enough time elapsed to reach new window', async () => { |
| 107 | + const requestedTokens = 9; |
| 108 | + |
| 109 | + await setTokenCountInClient(client, user2, requestedTokens, timestamp); |
| 110 | + // expect remaining tokens to be 1, b/c the 2-token-request should be blocked |
| 111 | + const result = await limiter.processRequest(user2, timestamp + WINDOW_SIZE - 1, 2); |
| 112 | + |
| 113 | + expect(result.success).toBe(false); |
| 114 | + expect(result.tokens).toBe(1); |
| 115 | + |
| 116 | + // expect current tokens in the window to still be 9 |
| 117 | + expect((await getWindowFromClient(client, user2)).currentTokens).toBe(9); |
| 118 | + }); |
| 119 | + }); |
| 120 | + describe('updateTimeWindow function works as expect', () => { |
| 121 | + afterEach(() => { |
| 122 | + client.flushall(); |
| 123 | + }); |
| 124 | + test('New window is initialized after reaching the window size', async () => { |
| 125 | + const fullRequest = 10; |
| 126 | + await setTokenCountInClient(client, user3, fullRequest, timestamp); |
| 127 | + const noAccess = await limiter.processRequest( |
| 128 | + user3, |
| 129 | + timestamp + WINDOW_SIZE - 1, |
| 130 | + 2 |
| 131 | + ); |
| 132 | + |
| 133 | + // expect not passing any request |
| 134 | + expect(noAccess.tokens).toBe(0); |
| 135 | + expect(noAccess.success).toBe(false); |
| 136 | + |
| 137 | + const newRequest = 1; |
| 138 | + expect( |
| 139 | + (await limiter.processRequest(user3, timestamp + WINDOW_SIZE, newRequest)) |
| 140 | + .success |
| 141 | + ).toBe(true); |
| 142 | + const count = await getWindowFromClient(client, user3); |
| 143 | + expect(count.currentTokens).toBe(1); |
| 144 | + }); |
| 145 | + test('Request will be passed after two window sizes', async () => { |
| 146 | + const fullRequest = 10; |
| 147 | + await setTokenCountInClient(client, user3, fullRequest, timestamp); |
| 148 | + const noAccess = await limiter.processRequest( |
| 149 | + user3, |
| 150 | + timestamp + WINDOW_SIZE - 1, |
| 151 | + 2 |
| 152 | + ); |
| 153 | + |
| 154 | + // expect not passing any request |
| 155 | + expect(noAccess.tokens).toBe(0); |
| 156 | + expect(noAccess.success).toBe(false); |
| 157 | + |
| 158 | + const newRequest = 6; |
| 159 | + // check if current time is over one window size |
| 160 | + const newAccess = await limiter.processRequest( |
| 161 | + user3, |
| 162 | + timestamp + WINDOW_SIZE * 2, |
| 163 | + newRequest |
| 164 | + ); |
| 165 | + |
| 166 | + expect(newAccess.tokens).toBe(4); |
| 167 | + expect(newAccess.success).toBe(true); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments