Skip to content

Commit 13f2353

Browse files
Fix flaky runWithRateLimit test by using fake timers
The 'throttles the task when the cache is populated recently' test was flaky because it used real time with a 1-second timeout window. Under CI load, the time between filling the rate limit cache and checking it could exceed 1 second, causing the old occurrences to be swept out and the rate limit to no longer be exceeded. Fix by using vi.useFakeTimers() to freeze time during the test, matching the pattern already used by the 'cache is populated but outdated' test in the same describe block. The existing afterEach hook already calls vi.useRealTimers() for cleanup. Also applied the same fix to the 'rate limit isn't used up' test to prevent the same class of flake.
1 parent 330e89c commit 13f2353

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

packages/cli-kit/src/private/node/conf-store.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ describe('runWithRateLimit', () => {
474474
test('throttles the task when the cache is populated recently', async () => {
475475
await inTemporaryDirectory(async (cwd) => {
476476
// Given
477+
vi.useFakeTimers()
477478
const config = new LocalStorage<any>({cwd})
478479
for (let i = 0; i < limit; i++) {
479480
// eslint-disable-next-line no-await-in-loop
@@ -511,6 +512,7 @@ describe('runWithRateLimit', () => {
511512
test("runs the task as usual when the cache is populated recently but the rate limit isn't used up", async () => {
512513
await inTemporaryDirectory(async (cwd) => {
513514
// Given
515+
vi.useFakeTimers()
514516
const config = new LocalStorage<any>({cwd})
515517
// Run the task once, but the rate limit is 2
516518
await runWithRateLimit(

0 commit comments

Comments
 (0)