: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(nodejs): control the clock in the CPU-call yield test (#126923)
Paul D'Ambrad05608ef0afc
diff --git a/nodejs/src/common/utils/event-loop-yield.test.ts b/nodejs/src/common/utils/event-loop-yield.test.tsindex e584b269be6a..37afdea3911e 100644--- a/nodejs/src/common/utils/event-loop-yield.test.ts+++ b/nodejs/src/common/utils/event-loop-yield.test.ts@@ -94,27 +94,44 @@ describe('event-loop-yield', () => {})it('does not block the loop across many CPU-bound calls', async () => {+ jest.useFakeTimers({ doNotFake: ['setImmediate'] })+ let elapsed = 0+ const now = jest.spyOn(performance, 'now').mockImplementation(() => elapsed)configureEventLoopYield(20)let longestDelay = 0+ let completed = 0let lastTick = performance.now()const interval = setInterval(() => {const now = performance.now()longestDelay = Math.max(longestDelay, now - lastTick)lastTick = now- }, 0)+ }, 1)try {const blockMs = 30for (let i = 0; i < 10; i++) {- await yieldEventLoopIfNeeded('test', () => busyWaitMs(blockMs))+ const call = yieldEventLoopIfNeeded('test', () => {+ elapsed += blockMs+ }).then(() => {+ completed += 1+ })++ await new Promise((resolve) => setImmediate(resolve))+ expect(completed).toBe(i)+ await jest.advanceTimersByTimeAsync(1)+ await call+ expect(completed).toBe(i + 1)}// 10 * 30 = 300ms total blocking. With yielding, longestDelay// should be close to one block + scheduling jitter. (If yielding// were broken, this would be ~300ms.)- expect(longestDelay).toBeLessThan(blockMs * 4)+ expect(longestDelay).toBe(blockMs)} finally {clearInterval(interval)+ await jest.runAllTimersAsync()+ now.mockRestore()+ jest.useRealTimers()}})})