Skip to content

Commit 05f0b02

Browse files
committed
fix: cache-with-ttl test flakiness on Windows
Two fixes for flaky TTL cache tests: 1. Add invalidateCaches() in beforeEach to ensure SOCKET_CACACHE_DIR override takes effect. Without this, getSocketCacacheDir() could return a cached value from a previous test. 2. Increase TTL from 50ms to 200ms for expiration tests. On slow CI runners (especially Windows), I/O latency during cacache.put() can exceed 50ms, causing the entry to expire before the immediate get() returns.
1 parent 583199f commit 05f0b02

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

test/unit/cache-with-ttl.test.mts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ import * as path from 'node:path'
1717

1818
import { createTtlCache } from '@socketsecurity/lib/cache-with-ttl'
1919
import { resetEnv, setEnv } from '@socketsecurity/lib/env/rewire'
20+
import { invalidateCaches } from '@socketsecurity/lib/paths/rewire'
2021
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2122

2223
describe.sequential('cache-with-ttl', () => {
2324
let cache: ReturnType<typeof createTtlCache>
2425
let testCacheDir: string
2526

2627
beforeEach(() => {
27-
// Create a unique cache directory for each test to ensure isolation
28+
// Invalidate path caches to ensure SOCKET_CACACHE_DIR override takes effect.
29+
// This is necessary because getSocketCacacheDir() caches its result.
30+
invalidateCaches()
31+
32+
// Create a unique cache directory for each test to ensure isolation.
2833
testCacheDir = path.join(
2934
tmpdir(),
3035
`socket-test-cache-${Date.now()}-${Math.random().toString(36).slice(2)}`,
@@ -180,16 +185,17 @@ describe.sequential('cache-with-ttl', () => {
180185
})
181186

182187
it('should fetch again after cache expires', async () => {
188+
// Use longer TTL (200ms) to avoid flaky failures on slow CI runners.
183189
const shortCache = createTtlCache({
184-
ttl: 50,
190+
ttl: 200,
185191
prefix: 'short-cache',
186192
})
187193
const fetcher = vi.fn(async () => 'value')
188194
await shortCache.getOrFetch('key', fetcher)
189195
expect(fetcher).toHaveBeenCalledTimes(1)
190196

191-
// Wait for TTL to expire
192-
await new Promise(resolve => setTimeout(resolve, 100))
197+
// Wait for TTL to expire (300ms > 200ms TTL).
198+
await new Promise(resolve => setTimeout(resolve, 300))
193199

194200
await shortCache.getOrFetch('key', fetcher)
195201
expect(fetcher).toHaveBeenCalledTimes(2)
@@ -321,16 +327,17 @@ describe.sequential('cache-with-ttl', () => {
321327
})
322328

323329
it('should skip expired entries in getAll', async () => {
330+
// Use longer TTL (200ms) to avoid flaky failures on slow CI runners.
324331
const shortCache = createTtlCache({
325-
ttl: 50,
332+
ttl: 200,
326333
prefix: 'expiry-getall-test',
327334
})
328335

329336
await shortCache.set('key1', 'value1')
330337
await shortCache.set('key2', 'value2')
331338

332-
// Wait for TTL to expire
333-
await new Promise(resolve => setTimeout(resolve, 100))
339+
// Wait for TTL to expire (300ms > 200ms TTL).
340+
await new Promise(resolve => setTimeout(resolve, 300))
334341

335342
const all = await shortCache.getAll<string>('*')
336343
expect(all.size).toBe(0)
@@ -409,16 +416,18 @@ describe.sequential('cache-with-ttl', () => {
409416

410417
describe('TTL expiration', () => {
411418
it('should expire entries after TTL', async () => {
419+
// Use longer TTL (200ms) to avoid flaky failures on slow CI runners.
420+
// Windows in particular can have significant I/O latency during cacache.put().
412421
const shortCache = createTtlCache({
413-
ttl: 50,
422+
ttl: 200,
414423
prefix: 'expiry-test',
415424
})
416425

417426
await shortCache.set('key', 'value')
418427
expect(await shortCache.get<string>('key')).toBe('value')
419428

420-
// Wait for TTL to expire
421-
await new Promise(resolve => setTimeout(resolve, 100))
429+
// Wait for TTL to expire (300ms > 200ms TTL).
430+
await new Promise(resolve => setTimeout(resolve, 300))
422431

423432
expect(await shortCache.get('key')).toBeUndefined()
424433

0 commit comments

Comments
 (0)