60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { RiotRateLimiter } from './riot-rate-limiter';
|
|
|
|
class ManualClock {
|
|
public currentTimeMs = 0;
|
|
public sleeps: number[] = [];
|
|
|
|
now = () => this.currentTimeMs;
|
|
|
|
sleep = (durationMs: number): Promise<void> => {
|
|
this.sleeps.push(durationMs);
|
|
this.currentTimeMs += durationMs;
|
|
return Promise.resolve();
|
|
};
|
|
}
|
|
|
|
describe('RiotRateLimiter', () => {
|
|
it('waits when the one-second development key window is exhausted', async () => {
|
|
const clock = new ManualClock();
|
|
const limiter = new RiotRateLimiter({ clock });
|
|
|
|
for (let index = 0; index < 20; index += 1) {
|
|
await limiter.waitForSlot('asia');
|
|
}
|
|
await limiter.waitForSlot('asia');
|
|
|
|
expect(clock.sleeps).toEqual([1000]);
|
|
});
|
|
|
|
it('tracks windows independently per Riot routing value', async () => {
|
|
const clock = new ManualClock();
|
|
const limiter = new RiotRateLimiter({ clock });
|
|
|
|
for (let index = 0; index < 20; index += 1) {
|
|
await limiter.waitForSlot('asia');
|
|
}
|
|
await limiter.waitForSlot('kr');
|
|
|
|
expect(clock.sleeps).toEqual([]);
|
|
});
|
|
|
|
it('waits for the longest exhausted window before recording a new request', async () => {
|
|
const clock = new ManualClock();
|
|
const limiter = new RiotRateLimiter({
|
|
clock,
|
|
windows: [
|
|
{ limit: 2, durationMs: 1000 },
|
|
{ limit: 3, durationMs: 5000 },
|
|
],
|
|
});
|
|
|
|
await limiter.waitForSlot('asia');
|
|
await limiter.waitForSlot('asia');
|
|
clock.currentTimeMs = 1000;
|
|
await limiter.waitForSlot('asia');
|
|
await limiter.waitForSlot('asia');
|
|
|
|
expect(clock.sleeps).toEqual([4000]);
|
|
});
|
|
});
|