Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(loop-lag clock): block off the CPU with Atomics.wait, not a spawned child
garrytan0dffdf666ec3
diff --git a/test/loop-lag-cpu-clock.test.ts b/test/loop-lag-cpu-clock.test.tsindex 541400a378..f3bf181a5c 100644--- a/test/loop-lag-cpu-clock.test.ts+++ b/test/loop-lag-cpu-clock.test.ts@@ -5,11 +5,15 @@* the shared-budget test read 156-257 ms; per-gap schedstat showed the excess as run-queue wait plus sleeps behind a* spawned Git that had not reached exec, with the thread's own CPU at 61-69 ms. `watchLoopLag` reads the thread's CPU.*- * Forced probe (deterministic): the thread blocks 150 ms in a synchronous child process with a timer due, as it does- * behind a spawned Git that has not reached exec. The wall clock reads it as 150 ms of lag; the thread's CPU clock, read- * on both sides of the call, does not move (nothing else can run on the thread during a synchronous call, so work left- * over from an earlier test file in the same process cannot land in the reading). The CPU clock still sees a stretch of- * work, and two stretches in one turn, the regression the bounds exist to catch.+ * Forced probe (deterministic): the thread blocks 150 ms with a timer due, as it does behind a spawned Git that has not+ * reached exec. The wall clock reads it as 150 ms of lag; the thread's CPU clock, read on both sides of the block, does+ * not move (nothing else can run on the thread during a synchronous block, so work left over from an earlier test file in+ * the same process cannot land in the reading). The CPU clock still sees a stretch of work, and two stretches in one turn,+ * the regression the bounds exist to catch.+ *+ * The block is an Atomics.wait, not a synchronous child: a spawn's own setup runs on the thread (1-4 ms, about 20 ms for a+ * process's first spawn), and on a VM that charges host steal to the running thread those few ms read 30 ms once against+ * the 25 ms bound (#7231, full-suite). That is the spawn's work, not the off-CPU time this test is about.*/import { expect, test } from 'bun:test';import { execFileSync } from 'node:child_process';@@ -19,8 +23,12 @@ import { threadCpuNow } from './helpers/cpu-time.ts';/** The bound the loop-lag tests hold. */const MAX_LAG_MS = 100;const turn = () => new Promise<void>(resolve => setTimeout(resolve, 20));-/** Blocks the thread off the CPU for `ms` in a synchronous child; returns the thread CPU the call itself cost. */-const offCpu = (ms: number) => { const start = threadCpuNow(); execFileSync('sleep', [String(ms / 1000)]); return threadCpuNow() - start; };+/** Blocks the thread off the CPU for `ms`; returns the thread CPU the block cost. */+const offCpu = (ms: number) => {+ const start = threadCpuNow();+ expect(Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)).toBe('timed-out');+ return threadCpuNow() - start;+};test('a thread kept off the CPU with a timer due is not lag; the wall clock reads it as lag', async () => {let last = performance.now(), wallLag = 0;@@ -30,7 +38,7 @@ test('a thread kept off the CPU with a timer due is not lag; the wall clock readawait turn();clearInterval(wall);expect(Math.round(wallLag)).toBeGreaterThanOrEqual(MAX_LAG_MS + 40);- // The spawn itself costs a few ms of the thread's CPU (fork, pipes); the 150 ms off the CPU cost none.+ // The 150 ms off the CPU cost none.expect(Math.round(cpu)).toBeLessThan(MAX_LAG_MS / 4);});