Skip to content

Make the http timeout test deterministic with a fake clock (#324) - #325

Merged
LinuxDevil merged 2 commits into
mainfrom
lou-324-flaky-http-timeout
Oct 2, 2026
Merged

LinuxDevil merged 2 commits into
mainfrom
lou-324-flaky-http-timeout

Conversation

@LinuxDevil

Copy link
Copy Markdown
Owner

Closes #324

What made it flaky

src/tools/built-in/http.test.ts "rejects near the configured timeout" ran a real request against a real server that never answers and asserted 450 <= elapsed < 2000 against the real 500 ms timeout. Under load (the full coverage run, many vitest workers) the event loop can be delayed by more than 1.5 s, so the upper bound failed. Production code is fine: the timer is an ordinary setTimeout(() => controller.abort(), timeoutMs) in runHttpRequest.

Fix (test only, no production change)

The test now fakes only setTimeout/clearTimeout (vi.useFakeTimers({ toFake: [...] })); the sockets stay real. It waits until the server has the request (so the timer is armed), then asserts: still pending after 499 ms, rejects with "timed out after 500ms" at 500 ms, and the server sees the connection close (the abort tore it down). That is stricter than before (exact boundary and the exact message, plus the connection teardown) and has no wall-clock comparison. Real timers are restored in finally.

Other tests named in the ticket

credentialBroker.test.ts has no timing assertion. src/execution/guardrails.test.ts has elapsed < 2000 checks around real child processes (100 ms command timeout); a fake clock does not apply there (the child and the kill are real), so I left them alone as the ticket says "only if the fix is the same".

Repeat-run evidence (Windows 11, Node 26)

npx vitest run src/tools src/security src/execution/guardrails.test.ts run 20 times, 4 runs at a time in parallel (about 4x the normal load): http.test.ts (23 tests) passed 20/20. Two unrelated tests failed in some of those 20 runs, not touched here: src/security/SubprocessSandbox.test.ts integration (Docker daemon required) in 5 runs and src/tools/workspace/checkpoints.node.test.ts "rewinds three turns" in 1 run.

Verification

  • npx tsc --noEmit: clean
  • npm run lint: 0 warnings
  • npm run test:coverage (after merging origin/main at fe54100): 268 files passed, 1 skipped; 3878 tests passed, 6 skipped
  • npm run fallow: exit 0
  • npm run docs:llms:check: clean
  • Not run (tests-only change, nothing in build, exports, docs or Agent Forge touched): docs snippets, test:types, pack-smoke, Agent Forge checks.

🤖 Generated with Claude Code

LinuxDevil and others added 2 commits October 3, 2026 00:18
The test compared real elapsed time to a real 500 ms timeout (>= 450, < 2000),
which failed when load delayed the event loop. It now fakes setTimeout and
clearTimeout only, checks the request is pending at 499 ms, rejects with
'timed out after 500ms' at 500 ms, and checks the connection was closed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@LinuxDevil
LinuxDevil merged commit 3753ccc into main Oct 2, 2026
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: http.test.ts 'rejects near the configured timeout' under load

1 participant