fix: unify riot refresh rate gating

This commit is contained in:
dahyeon committed 2026-06-07 11:11:12 -07:00
1 parent 25f3ccad57
commit de8bc87940
6 files changed
+359 -123

No files matched your search

@@ -41,7 +41,7 @@ function makeScheduler() {
raw: jest.fn().mockReturnValue(rawClient),
} as unknown as jest.Mocked<GarnetService> & { raw: jest.Mock };
const rateLimiter = {
acquireBackground: jest.fn().mockResolvedValue(undefined),
isRefreshWindowOpen: jest.fn().mockResolvedValue(true),
} as unknown as jest.Mocked<RiotRateLimiterService>;
const enrichmentService = {
markRefreshRequested: jest.fn().mockResolvedValue(undefined),
@@ -84,9 +84,29 @@ describe('BackgroundRefreshScheduler', () => {
scheduler.onModuleDestroy();
});
it('does not query stale accounts outside the KST window', async () => {
it('uses the dawn refresh gate ratio inside the KST background window', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const { scheduler, rateLimiter } = makeScheduler();
await scheduler.tick();
expect(rateLimiter.isRefreshWindowOpen).toHaveBeenCalledWith(0.8);
});
it('uses the opportunistic refresh gate ratio outside the KST background window', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T17:59:59.000Z'));
const { scheduler, staleAccountRepository } = makeScheduler();
const { scheduler, rateLimiter, staleAccountRepository } = makeScheduler();
await scheduler.tick();
expect(rateLimiter.isRefreshWindowOpen).toHaveBeenCalledWith(0.5);
expect(staleAccountRepository.findStaleAccounts).toHaveBeenCalledTimes(1);
});
it('does not query stale accounts when the refresh gate is closed', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const { scheduler, rateLimiter, staleAccountRepository } = makeScheduler();
rateLimiter.isRefreshWindowOpen.mockResolvedValue(false);
await scheduler.tick();
@@ -103,21 +123,15 @@ describe('BackgroundRefreshScheduler', () => {
expect(staleAccountRepository.findStaleAccounts).not.toHaveBeenCalled();
});
it('marks and enriches each stale candidate with background rate tokens', async () => {
it('marks and enriches each stale candidate without background precharge', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const {
scheduler,
rateLimiter,
enrichmentService,
staleAccountRepository,
} = makeScheduler();
const { scheduler, enrichmentService, staleAccountRepository } =
makeScheduler();
const candidates = [candidate('puuid-1'), candidate('puuid-2')];
staleAccountRepository.findStaleAccounts.mockResolvedValue(candidates);
await scheduler.tick();
expect(rateLimiter.acquireBackground).toHaveBeenCalledTimes(2);
expect(rateLimiter.acquireBackground).toHaveBeenCalledWith(8);
expect(enrichmentService.markRefreshRequested).toHaveBeenCalledTimes(2);
expect(enrichmentService.markRefreshRequested).toHaveBeenNthCalledWith(
1,
@@ -131,6 +145,28 @@ describe('BackgroundRefreshScheduler', () => {
});
});
it('stops the candidate loop when the refresh gate closes mid-tick', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const {
scheduler,
rateLimiter,
enrichmentService,
staleAccountRepository,
} = makeScheduler();
staleAccountRepository.findStaleAccounts.mockResolvedValue([
candidate('puuid-1'),
candidate('puuid-2'),
]);
rateLimiter.isRefreshWindowOpen
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(false);
await scheduler.tick();
expect(enrichmentService.enrichRiotAccount).toHaveBeenCalledTimes(1);
});
it('continues after a candidate throws and records a failure counter', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const { scheduler, rawClient, enrichmentService, staleAccountRepository } =
@@ -157,13 +193,8 @@ describe('BackgroundRefreshScheduler', () => {
it('skips candidates already in failure backoff', async () => {
jest.useFakeTimers().setSystemTime(new Date('2026-05-02T18:00:00.000Z'));
const {
scheduler,
garnet,
rateLimiter,
enrichmentService,
staleAccountRepository,
} = makeScheduler();
const { scheduler, garnet, enrichmentService, staleAccountRepository } =
makeScheduler();
staleAccountRepository.findStaleAccounts.mockResolvedValue([
candidate('skip-me'),
]);
@@ -174,7 +205,6 @@ describe('BackgroundRefreshScheduler', () => {
await scheduler.tick();
expect(rateLimiter.acquireBackground).not.toHaveBeenCalled();
expect(enrichmentService.markRefreshRequested).not.toHaveBeenCalled();
expect(enrichmentService.enrichRiotAccount).not.toHaveBeenCalled();
});