:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(tests): stop the keystore TTL test racing the wall clock (#95267)
Julian Bezf7dae081e8df
diff --git a/nodejs/src/ingestion/pipelines/sessionreplay/shared/keystore/cache/memory-cache.test.ts b/nodejs/src/ingestion/pipelines/sessionreplay/shared/keystore/cache/memory-cache.test.tsindex a152f2fe402b..ddd3b6210cd4 100644--- a/nodejs/src/ingestion/pipelines/sessionreplay/shared/keystore/cache/memory-cache.test.ts+++ b/nodejs/src/ingestion/pipelines/sessionreplay/shared/keystore/cache/memory-cache.test.ts@@ -223,23 +223,40 @@ describe('MemoryCachedKeyStore', () => {})it('should expire cached items after ttlMs', async () => {- const ttlMs = 50 // Short TTL for testing- const customCachedKeyStore = new MemoryCachedKeyStore(mockDelegate, { ttlMs })-- // First call populates cache- await customCachedKeyStore.getKey('session-123', 1)- mockDelegate.getKey.mockClear()-- // Immediate second call should use cache- await customCachedKeyStore.getKey('session-123', 1)- expect(mockDelegate.getKey).not.toHaveBeenCalled()-- // Wait for TTL to expire- await new Promise((resolve) => setTimeout(resolve, ttlMs + 10))-- // After TTL - should call delegate- await customCachedKeyStore.getKey('session-123', 1)- expect(mockDelegate.getKey).toHaveBeenCalledWith('session-123', 1)+ const ttlMs = 50+ // lru-cache reads the clock through the performance object it captured at import, so+ // Jest's fake timers cannot reach it: they replace that global object instead of+ // patching it. A spy on the method does reach it. Drive the clock by hand, because on+ // a loaded runner the wall clock can pass ttlMs between the two reads below.+ let now = performance.now()+ const nowSpy = jest.spyOn(performance, 'now').mockImplementation(() => now)++ try {+ const customCachedKeyStore = new MemoryCachedKeyStore(mockDelegate, { ttlMs })++ // First call populates cache+ await customCachedKeyStore.getKey('session-123', 1)+ mockDelegate.getKey.mockClear()++ // Baseline: still cached while the clock has not moved+ await customCachedKeyStore.getKey('session-123', 1)+ expect(mockDelegate.getKey).not.toHaveBeenCalled()++ // Move past the TTL+ now += ttlMs + 10++ // The read above made lru-cache keep its staleness reading for ttlResolution, which+ // is 1 ms by default, and only a timer clears it. Wait for a timer that expires+ // after that one, so the next staleness check reads the clock above. Elapsed time+ // is all this wait needs, so a slow runner cannot break it.+ await new Promise((resolve) => setTimeout(resolve, 5))++ // After TTL - should call delegate+ await customCachedKeyStore.getKey('session-123', 1)+ expect(mockDelegate.getKey).toHaveBeenCalledWith('session-123', 1)+ } finally {+ nowSpy.mockRestore()+ }})})