Skip to content

fix(tests): stop two unit tests from measuring timer timing - #1870

Merged
devswha merged 2 commits into
mainfrom
fix/rpc-diagnostics-test-stall-count
Oct 24, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/rpc-diagnostics-test-stall-count

Conversation

@devswha

@devswha devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Two unit tests that measured timer timing, both seen failing in bun run check fast on a loaded PC. Test-only.

1. server/rpc-diagnostics.test.ts: "logs a timeout once, then one recovery"

Failed with Expected: 4, Received: 3 requests. Its two stalled calls had a 30 ms timeout; on a busy event loop that timeout can fire before the request is written, or the daemon reads the bytes only after the test moved on, so how many stalled requests arrive is not the test's to fix.

  • Stalled calls carry stall: true; the fixture counts them apart and never answers them. The test asserts exactly the two answered requests (one per call, none sent again) and one or two stalled ones.
  • The first stalled call gets 500 ms, so the timeout it logs is the response's (phase: "response").
  • Reproduced on demand: blocking the event loop 80 ms after starting the second stalled call fails main with Expected: 4, Received: 3; this version passes. 200 runs, 8 at a time: 0 failures.

2. server/collector.test.ts: "backs off while a subscription keeps closing before it starts, and resets once one starts"

Asserted wall-clock time (a reconnect within 100 ms of Date.now()), which late timers overran: 2 of 10 runs under contract-test load. Reproduced on demand: a 120 ms event-loop stall after the drop gives 121 ms against the 100 ms limit.

  • Now on jest's fake timers, asserting each delay exactly: no reopen one ms before 20, 40, 80, 160 and the 160 ms cap, one at it, and 20 again once a connection started. Tripling the backoff, removing the reset or the cap fails it.
  • scripts/fixed-sleeps.json: the file's baseline drops 62 → 61, as the ratchet asks.

The rest of collector.test.ts still waits on fixed tick() sleeps; one other case failed once in 200 runs under load. That is filed as #1867 rather than converted here.

Verification

  • bun run check fast: passed (6393 pass, 0 fail) on current main.
  • Reviewed by omo-native-code-reviewer (approve, no blockers; its two optional notes — start the collector inside the try, assert at least one stalled request arrived — are applied). Test-only, so no Codex review.

Part of the flaky-tests lane (check fast reliability).

mp added 2 commits October 24, 2026 22:22
…swered ones

server/rpc-diagnostics.test.ts "logs a timeout once, then one recovery" failed in check fast:
expected 4 requests, received 3. Its two stalled calls had a 30 ms timeout, and on a busy
event loop that timeout can fire before the request is written, or the daemon reads the bytes
only after the test moved on; how many stalled requests arrive is not the test's to fix.

Stalled calls now carry stall: true, which the fixture counts apart and never answers. The
test asserts exactly the two answered requests (one per call, none sent again) and at most two
stalled ones. The first stalled call gets 500 ms, so the timeout it logs is the response's.
Reproduced on demand by blocking the event loop 80 ms after starting the second stalled call:
main fails with "Expected: 4, Received: 3", this passes. 200 runs, 8 at a time: 0 failures.
server/collector.test.ts "backs off while a subscription keeps closing before it starts, and
resets once one starts" asserted wall-clock time (a reconnect within 100 ms of Date.now()),
which a busy PC's late timers overran: 2 of 10 runs under contract-test load. A 120 ms event
loop stall after the drop reproduces it on demand (121 < 100 fails).

It now runs on jest's fake timers and asserts each delay exactly: no reopen one ms before 20,
40, 80, 160 and the 160 ms cap, one at it, and 20 again once a connection started. Tripling
the backoff or not resetting it fails the test.
@coderabbitai

coderabbitai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 10 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 427f5d3 and adf1481.

Files selected for processing: 3
  • scripts/fixed-sleeps.json
  • server/collector.test.ts
  • server/rpc-diagnostics.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

CodeRabbit's review was rate-limited ("Review limit reached"), so no CodeRabbit review ran on this PR. The reviews below stand in for it.

omo-native-code-reviewer: APPROVE, no blockers. Its two optional notes are applied: the collector is started inside the try (fake timers can never leak), and the test asserts at least one stalled request reached the daemon. Low, not changed: the fixture's default mode is still "stall" though no test relies on it.
Test-only: no Codex review, per the lane's review policy.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review-work report. Overall: PASSED. This PR changes tests only, so under the maintainer's token rule it merges on the code review, the merge pass's own check and CI, without the gate reviewer.

Lane Verdict
Code review (omo-native-code-reviewer, programming + remove-ai-slops) PASS (CLEAR)
Merge pass's own check both test files ran 5 times in a row, 0 failures; conventions and typecheck pass; git merge-tree is clean with #1865 (shares server/collector.test.ts)

What was checked

  • Mutants: each one fails the tests.
    • Removing the backoff reset.
    • Removing the doubling.
    • Removing the cap.
    • Sending every request twice.
    • Resending on timeout (fails 3 runs out of 3).
  • Fake timers: collector.stop() and then jest.useRealTimers() run in a finally. The repo already uses jest.useFakeTimers this way in two other test files.
  • Fixed-sleep ratchet: only goes down, with server/collector.test.ts at 62 → 61.

Follow-ups (MINOR)

All required checks are green, and no review thread is open.

CodeRabbit did not review this PR (rate-limited or skipped). The reviews above stand in for it.

@devswha
devswha merged commit 88267d2 into main Oct 24, 2026
17 checks passed
@devswha
devswha deleted the fix/rpc-diagnostics-test-stall-count branch October 24, 2026 14:00
devswha added a commit that referenced this pull request Oct 24, 2026
…1900)

## What it does

`server/collector.test.ts` waited on fixed sleeps (`await tick(20)`,
`tick(70)`, ... 61 of them by `scripts/fixed-sleeps.ts`'s count) for the
collector's debounce, snapshot retry, replay settle and reconnect timers
(5 ms in `fakeHerdr`). On a loaded PC a test could read the handler log
before those timers had run (1 failure in 200 runs seen in the
flaky-tests lane; it failed again on CI today, in #1895's Fast checks:
"tells what the snapshot that closes the subscription already shows"
read `[]` where it expected `w1:p1:unknown (was working)`). #1870 moved
the backoff test to fake timers and left the rest to this issue.

Now the whole `startStatusCollector recovery` describe runs on bun's
fake timers, so every timer fires at its own moment of the test's clock
however busy the PC is:

- `beforeEach`/`afterEach` turn fake timers on and off for the describe.
- `elapse(ms)` moves the clock a millisecond at a time and, after each
step, lets the promise chains that step started settle (`setImmediate`,
which bun does not fake). `elapse()` runs what is due now, as a 0 ms
timeout did. Each `tick()`/`tick(N)` became `elapse()`/`elapse(N)` with
the same amount, so every test reads the log at the same point of the
collector's timeline as before.
- The one recovery test that polled with `until()` (real sleeps and
`Date.now()`, which a frozen clock never moves) steps the clock and
asserts exact values.
- The first describe's only remaining wait is the marked `// fixed
wait:` that proves no refresh comes; `scripts/fixed-sleeps.json` drops
the collector entry (61 to 0).

No production code changes.

## Why it can no longer flake under load

What a test reads is decided by how far it moved the fake clock, not by
how long the PC took to run real timers: a debounce or replay settle
cannot be late, because it only runs when the test advances to it, and
the test waits for what it started to settle before it reads.

## How it was verified

- `bun test server/collector.test.ts`: 57 pass, 0 fail, in 3.5 s.
- 200 runs of the file, 8 at a time, at a load average of 150 to 162 on
32 cores (other lanes' checks running): 200 of 200 passed, 0 failures.
- The fake clock still catches timing regressions in
`server/collector.ts`: a reconnect delay 100 ms late fails 3 tests; a
replay settle 100 ms late fails 9.
- `scripts/conventions.test.ts` (fixed-sleep ratchet) passes.
- Review: `omo-native-code-reviewer` APPROVE. It ran the same collector
mutations against the old and the new test file and found each fails the
same tests with both (replay settle +100 ms: 9; reconnect +100 ms: 3;
snapshot retry +1000 ms: 1). Of its three LOW notes, the
reopened-subscription check now also asserts the subscription exists;
the marked fixed wait that proves no refresh comes and #1870's own (now
redundant) fake-timer calls are left as they are.
- `bun run check fast` (through `dori heavy`) on this commit: typecheck,
build and unit tests ok (6396 pass, 0 fail, 13 skip).

Fixes #1867

Co-authored-by: mp <mp@local>
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.

1 participant