Skip to content

Commit 6abe54c

Browse files
committed
Improve test2
1 parent c2911da commit 6abe54c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/index.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,33 @@ describe('RateLimiter implementations', () => {
126126
const options = { interval: 10, maxInInterval: 100 };
127127
const limiter = await createLimiter(options);
128128

129-
// Should allow first action through.
130129
setTime(1000);
130+
131+
// All actions available for consuming
131132
expect(await limiter.wouldLimitWithInfo(id)).toEqual({
132-
actionsRemaining: 100,
133+
actionsRemaining: options.maxInInterval,
133134
blocked: false,
134135
blockedDueToCount: false,
135136
blockedDueToMinDifference: false,
136137
millisecondsUntilAllowed: 0,
137138
});
138-
expect(await limiter.limit(id, 2)).toBe(false);
139+
140+
// Lock all at once
141+
expect(await limiter.limit(id, options.maxInInterval)).toBe(false);
142+
// Nothing can be locked
143+
expect(await limiter.limit(id)).toBe(true);
144+
145+
// Wait for window to pass
146+
setTime(1000 + options.interval);
147+
// All available again
148+
expect(await limiter.limit(id, options.maxInInterval)).toBe(false);
149+
expect(await limiter.limit(id)).toBe(true);
150+
151+
// Wait for half a window more
152+
setTime(1000 + options.interval + options.interval / 2);
153+
// Umm, should it have half of maxInInterval?
154+
expect(await limiter.limit(id, options.maxInInterval / 2)).toBe(false);
155+
expect(await limiter.limit(id)).toBe(true);
139156
});
140157

141158
it('blocked actions count as actions', async () => {

0 commit comments

Comments
 (0)