Add Riot API client
This commit is contained in:
1 parent
1f2805be23
commit
256d776131
6 files changed
+908
No files matched your search
@@ -0,0 +1,59 @@
|
||||
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]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user