Skip to content

Commit 9e4d7a2

Browse files
committed
added test for skipped fixed window
1 parent ad54fda commit 9e4d7a2

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

test/rateLimiters/slidingWindowCounter.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,21 @@ describe('Test TokenBucket Rate Limiter', () => {
552552
false
553553
);
554554
});
555+
556+
test('updates correctly when > WINDOW_SIZE * 2 has surpassed', async () => {
557+
await setTokenCountInClient(client, user1, 1, 0, timestamp);
558+
559+
// to make sure that previous tokens is not 1
560+
const result = await limiter.processRequest(user1, timestamp + WINDOW_SIZE * 2, 1);
561+
562+
expect(result.tokens).toBe(9);
563+
564+
const redisData: RedisWindow = await getWindowFromClient(client, user1);
565+
566+
expect(redisData.currentTokens).toBe(1);
567+
expect(redisData.previousTokens).toBe(0);
568+
expect(redisData.fixedWindowStart).toBe(timestamp + WINDOW_SIZE * 2);
569+
});
555570
});
556571

557572
describe('SlidingWindowCounter correctly updates Redis cache', () => {

0 commit comments

Comments
 (0)