Skip to content

Commit 2e3741d

Browse files
committed
updated tests to reflect changes
2 parents 44a52f8 + 3f9ad17 commit 2e3741d

File tree

5 files changed

+63
-71
lines changed

5 files changed

+63
-71
lines changed

src/@types/rateLimit.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface RedisBucket {
2626
export interface RedisWindow {
2727
currentTokens: number;
2828
previousTokens: number;
29-
fixedWindowStart: number;
29+
fixedWindowStart?: number;
3030
}
3131

3232
export type RedisLog = RedisBucket[];

src/rateLimiters/slidingWindowCounter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class SlidingWindowCounter implements RateLimiter {
112112
};
113113

114114
// if request time is in a new window
115-
if (timestamp >= window.fixedWindowStart + this.windowSize) {
115+
if (window.fixedWindowStart && timestamp >= window.fixedWindowStart + this.windowSize) {
116116
// if more than one window was skipped
117117
if (timestamp >= window.fixedWindowStart + this.windowSize * 2) {
118118
// if one or more windows was skipped, reset new window to be at current timestamp
@@ -131,7 +131,7 @@ class SlidingWindowCounter implements RateLimiter {
131131
let rollingWindowProportion = 0;
132132
let previousRollingTokens = 0;
133133

134-
if (updatedUserWindow.previousTokens) {
134+
if (updatedUserWindow.fixedWindowStart && updatedUserWindow.previousTokens) {
135135
// proportion of rolling window present in previous window
136136
rollingWindowProportion =
137137
(this.windowSize - (timestamp - updatedUserWindow.fixedWindowStart)) /

test/rateLimiters/slidingWindowCounter.test.ts

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function setTokenCountInClient(
3535
await redisClient.set(uuid, JSON.stringify(value));
3636
}
3737

38-
describe('Test TokenBucket Rate Limiter', () => {
38+
describe('Test SlidingWindowCounter Rate Limiter', () => {
3939
beforeEach(async () => {
4040
// init a mock redis cache
4141
client = new RedisMock();
@@ -341,88 +341,80 @@ describe('Test TokenBucket Rate Limiter', () => {
341341
expect(count1.currentTokens).toBe(0);
342342
expect(count1.previousTokens).toBe(initRequest);
343343
});
344+
});
344345

345-
test('rolling window at 50% blocks requests over allowed limit set by formula', async () => {
346-
// 50% of rolling window present in previous fixed window
347-
// 1.5*60000 = 90000 (time after initial fixedWindowStart
348-
// to set rolling window at 50% of previous fixed window)
346+
test('rolling window at 50% blocks requests over allowed limit set by formula', async () => {
347+
// 50% of rolling window present in previous fixed window
348+
// 1.5*60000 = 90000 (time after initial fixedWindowStart
349+
// to set rolling window at 50% of previous fixed window)
349350

350-
// to set initial fixedWindowStart
351-
await setTokenCountInClient(client, user4, 0, 0, timestamp);
351+
// to set initial fixedWindowStart
352+
await setTokenCountInClient(client, user4, 0, 0, timestamp);
352353

353-
const initRequest = 8;
354+
const initRequest = 8;
354355

355-
// large request at very end of first fixed window
356-
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
356+
// large request at very end of first fixed window
357+
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
357358

358-
// 7 + 8 * .5 = 11, over capacity (request should be blocked)
359-
const result = await limiter.processRequest(
360-
user4,
361-
timestamp + WINDOW_SIZE * 1.5,
362-
7
363-
);
364-
expect(result.tokens).toBe(6);
365-
expect(result.success).toBe(false);
359+
// 7 + 8 * .5 = 11, over capacity (request should be blocked)
360+
const result = await limiter.processRequest(user4, timestamp + WINDOW_SIZE * 1.5, 7);
361+
expect(result.tokens).toBe(6);
362+
expect(result.success).toBe(false);
366363

367-
// currentTokens (in current fixed window): 0
368-
// previousTokens (in previous fixed window): 8
369-
const count = await getWindowFromClient(client, user4);
370-
expect(count.currentTokens).toBe(0);
371-
expect(count.previousTokens).toBe(initRequest);
372-
});
364+
// currentTokens (in current fixed window): 0
365+
// previousTokens (in previous fixed window): 8
366+
const count = await getWindowFromClient(client, user4);
367+
expect(count.currentTokens).toBe(0);
368+
expect(count.previousTokens).toBe(initRequest);
369+
});
373370

374-
test('rolling window at 25% blocks requests over allowed limit set by formula', async () => {
375-
// 25% of rolling window present in previous fixed window
376-
// 1.75*60000 = 105000 (time after initial fixedWindowStart
377-
// to set rolling window at 25% of previous fixed window)
371+
test('rolling window at 25% blocks requests over allowed limit set by formula', async () => {
372+
// 25% of rolling window present in previous fixed window
373+
// 1.75*60000 = 105000 (time after initial fixedWindowStart
374+
// to set rolling window at 25% of previous fixed window)
378375

379-
// to set initial fixedWindowStart
380-
await setTokenCountInClient(client, user4, 0, 0, timestamp);
376+
// to set initial fixedWindowStart
377+
await setTokenCountInClient(client, user4, 0, 0, timestamp);
381378

382-
const initRequest = 8;
379+
const initRequest = 8;
383380

384-
// large request at very end of first fixed window
385-
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
381+
// large request at very end of first fixed window
382+
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
386383

387-
// 9 + 8 * .25 = 11, over capacity (request should be blocked)
388-
const result = await limiter.processRequest(
389-
user4,
390-
timestamp + WINDOW_SIZE * 1.75,
391-
9
392-
);
393-
expect(result.tokens).toBe(8);
394-
expect(result.success).toBe(false);
384+
// 9 + 8 * .25 = 11, over capacity (request should be blocked)
385+
const result = await limiter.processRequest(user4, timestamp + WINDOW_SIZE * 1.75, 9);
386+
expect(result.tokens).toBe(8);
387+
expect(result.success).toBe(false);
395388

396-
// currentTokens (in current fixed window): 0
397-
// previousTokens (in previous fixed window): 8
398-
const count = await getWindowFromClient(client, user4);
399-
expect(count.currentTokens).toBe(0);
400-
expect(count.previousTokens).toBe(initRequest);
401-
});
402-
test('rolling window at 100% blocks requests over allowed limit set by formula', async () => {
403-
// 1% of rolling window present in previous fixed window
404-
// .01*60000 = 600 (time after initial fixedWindowStart
405-
// to set rolling window at 100% of previous fixed window)
389+
// currentTokens (in current fixed window): 0
390+
// previousTokens (in previous fixed window): 8
391+
const count = await getWindowFromClient(client, user4);
392+
expect(count.currentTokens).toBe(0);
393+
expect(count.previousTokens).toBe(initRequest);
394+
});
395+
test('rolling window at 100% blocks requests over allowed limit set by formula', async () => {
396+
// 1% of rolling window present in previous fixed window
397+
// .01*60000 = 600 (time after initial fixedWindowStart
398+
// to set rolling window at 100% of previous fixed window)
406399

407-
// to set initial fixedWindowStart
408-
await setTokenCountInClient(client, user4, 0, 0, timestamp);
400+
// to set initial fixedWindowStart
401+
await setTokenCountInClient(client, user4, 0, 0, timestamp);
409402

410-
const initRequest = 8;
403+
const initRequest = 8;
411404

412-
// large request at very end of first fixed window
413-
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
405+
// large request at very end of first fixed window
406+
await limiter.processRequest(user4, timestamp + WINDOW_SIZE - 1, initRequest);
414407

415-
// 11 + 8 * .01 = 11, above capacity (request should be blocked)
416-
const result = await limiter.processRequest(user4, timestamp + WINDOW_SIZE, 11);
417-
expect(result.tokens).toBe(2);
418-
expect(result.success).toBe(false);
408+
// 11 + 8 * .01 = 11, above capacity (request should be blocked)
409+
const result = await limiter.processRequest(user4, timestamp + WINDOW_SIZE, 11);
410+
expect(result.tokens).toBe(2);
411+
expect(result.success).toBe(false);
419412

420-
// currentTokens (in current fixed window): 0
421-
// previousTokens (in previous fixed window): 8
422-
const count1 = await getWindowFromClient(client, user4);
423-
expect(count1.currentTokens).toBe(0);
424-
expect(count1.previousTokens).toBe(initRequest);
425-
});
413+
// currentTokens (in current fixed window): 0
414+
// previousTokens (in previous fixed window): 8
415+
const count1 = await getWindowFromClient(client, user4);
416+
expect(count1.currentTokens).toBe(0);
417+
expect(count1.previousTokens).toBe(initRequest);
426418
});
427419
});
428420

test/rateLimiters/tokenBucket.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Test TokenBucket Rate Limiter', () => {
154154
});
155155

156156
test("blocks requests exceeding the user's current allotment of tokens", async () => {
157-
// Test > capacity tokens reqeusted
157+
// Test > capacity tokens requested
158158
expect((await limiter.processRequest(user1, timestamp, CAPACITY + 1)).success).toBe(
159159
false
160160
);

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"resolveJsonModule": true,
1818
"isolatedModules": true,
1919
"noEmit": false,
20-
"typeRoots": ["src/@types", "node_modules/@types"],
20+
"typeRoots": ["@types", "node_modules/@types"],
2121
"types": ["node", "jest"],
2222
"outDir": "build/"
2323
},

0 commit comments

Comments
 (0)