Fix flaky TTL cache test timing
Use a 20ms TTL with a 40ms wait so the expiry window is long enough for the immediate second read to hit the in-memory cache reliably.
This commit is contained in:
1 parent
903d175f2d
commit
c17ef0e7ab
1 file changed
+5
-4
@@ -36,10 +36,11 @@ describe("cached (memory-only, no Redis)", () => {
|
|||||||
let count = 0;
|
let count = 0;
|
||||||
const fn = vi.fn(async () => ++count);
|
const fn = vi.fn(async () => ++count);
|
||||||
const key = `ttl-${Math.random()}`;
|
const key = `ttl-${Math.random()}`;
|
||||||
expect(await cached(key, 1, fn)).toBe(1);
|
const ttl = 20;
|
||||||
expect(await cached(key, 1, fn)).toBe(1);
|
expect(await cached(key, ttl, fn)).toBe(1);
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
expect(await cached(key, ttl, fn)).toBe(1);
|
||||||
expect(await cached(key, 1, fn)).toBe(2);
|
await new Promise((r) => setTimeout(r, 40));
|
||||||
|
expect(await cached(key, ttl, fn)).toBe(2);
|
||||||
expect(fn).toHaveBeenCalledTimes(2);
|
expect(fn).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user