Skip to content

Commit 7df525b

Browse files
Yufei WuYufei Wu
authored andcommitted
updated testing
1 parent 81f99e6 commit 7df525b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/rateLimiters/fixedWindow.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class FixedWindow implements RateLimiter {
9595
}
9696

9797
// update a new time window, check the current capacity situation
98-
if (tokens > this.capacity) {
99-
await this.client.setex(uuid, keyExpiry, JSON.stringify(updatedUserWindow));
100-
return { success: false, tokens: this.capacity };
101-
}
98+
// if (tokens > this.capacity) {
99+
// await this.client.setex(uuid, keyExpiry, JSON.stringify(updatedUserWindow));
100+
// return { success: false, tokens: this.capacity };
101+
// }
102102
await this.client.setex(uuid, keyExpiry, JSON.stringify(updatedUserWindow));
103103
return {
104104
success: true,

test/rateLimiters/fixedWindow.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,35 @@ describe('Test FixedWindow Rate Limiter', () => {
5353
});
5454
test('reached 40% capacity in current time window and still can pass request', async () => {
5555
const initial = 5;
56+
await setTokenCountInClient(client, user2, initial, timestamp);
5657
const partialWithdraw = 2;
5758
expect(
58-
(await limiter.processRequest(user2, timestamp, initial + partialWithdraw))
59-
.tokens
59+
(
60+
await limiter.processRequest(
61+
user2,
62+
timestamp + WINDOW_SIZE * 0.4,
63+
partialWithdraw
64+
)
65+
).tokens
6066
).toBe(CAPACITY - initial - partialWithdraw);
6167

6268
const tokenCountPartial = await getWindowFromClient(client, user2);
6369
expect(tokenCountPartial.currentTokens).toBe(initial + partialWithdraw);
6470
});
6571

6672
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 () => {
6785
const initial = 6;
6886
const partialWithdraw = 5;
6987
await setTokenCountInClient(client, user2, initial, timestamp);

0 commit comments

Comments
 (0)