Skip to content

fix(tests): run the collector's recovery tests on the test's clock - #1900

Merged
devswha merged 1 commit into
mainfrom
fix/collector-test-timers
Oct 24, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/collector-test-timers

Conversation

@devswha

@devswha devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner

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 fix(tests): stop two unit tests from measuring timer timing #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

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between f34d16d and b5ca085.

Files selected for processing: 2
  • scripts/fixed-sleeps.json
  • server/collector.test.ts
Files with no reviewable changes: 1
  • scripts/fixed-sleeps.json

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Tests
    • Collector recovery and lifecycle checks now use controlled timing rather than real-time waits, making test results more consistent.
    • Removed the collector test’s fixed-delay configuration. These changes affect automated test execution; no user-facing behavior changes are included.

Collector recovery tests replace most real-time waits with fake-timer advancement and promise settling. The fixed-sleep configuration entry for server/collector.test.ts is removed.

Changes: Collector test timing

Layer / File(s) Summary
Fake-timer helpers and setup
server/collector.test.ts
Adds helpers to settle pending promises and advance fake timers in controlled increments. Recovery tests enable fake timers and restore real timers. The lifecycle readiness test uses promise settling; the debounce test uses Bun.sleep(30).
Recovery timing scenarios
server/collector.test.ts, scripts/fixed-sleeps.json
Recovery scenarios replace real-time waits with fake-clock advancement while retaining their existing assertions. Removes the fixed-sleep entry for server/collector.test.ts.

Priority: Low

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Other · Severity of issue fixed: Low

Merge Risk: Minimal · up to b5ca0

The reviewed test-only change has no established merge-blocking risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: collector recovery tests now use the test clock.
Description check Passed The description explains the problem, resulting behavior, scope, rationale, validation results, regression coverage, and linked issue. It uses headings different from the template, but it provides the…
Linked Issues check Passed Issue #1867 targets fixed waits in server/collector.test.ts. The recovery tests now use jest.useFakeTimers() with elapse() and settle promise work after each clock step. The polling recovery cas…
Out of Scope Changes check Passed The changes stay within issue #1867. They modify collector test timing helpers, recovery test setup and waits, and the fixed-sleep registry. No production code or unrelated behavior changes are presen…
Full details: Description check

Explanation

The description explains the problem, resulting behavior, scope, rationale, validation results, regression coverage, and linked issue. It uses headings different from the template, but it provides the required Change and Validation information.

Full details: Linked Issues check

Explanation

Issue #1867 targets fixed waits in server/collector.test.ts. The recovery tests now use jest.useFakeTimers() with elapse() and settle promise work after each clock step. The polling recovery case uses deterministic clock advancement and exact assertions. The fixed-sleep registry no longer lists this test file. The remaining marked Bun.sleep(30) checks that no refresh occurs and is not used to wait for a collector result.

Full details: Out of Scope Changes check

Explanation

The changes stay within issue #1867. They modify collector test timing helpers, recovery test setup and waits, and the fixed-sleep registry. No production code or unrelated behavior changes are present.

Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • 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.

server/collector.test.ts waited on fixed sleeps (tick(20), tick(70), ...) for
the collector's debounce, snapshot retry, replay settle and reconnect timers
(5 ms in fakeHerdr), so on a loaded PC a test could read the log before those
timers had run.

The recovery tests now run on bun's fake timers: elapse(ms) moves the clock a
millisecond at a time and lets the promise chains each step started settle
(setImmediate is not faked), so every timer fires at its own moment of the
test's clock however busy the PC is. The one recovery test that polled with
until() steps the clock as well. The file's last fixed wait is the marked one
that proves no refresh comes, and scripts/fixed-sleeps.json drops its
collector entry (61 to 0).

Fixes #1867
@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Holding this as a draft for a moment: do not merge this head. Since it was opened, main gained #1865, whose new collector test calls tick(). This PR removes tick, so although git merges the two cleanly, the result fails typecheck (server/collector.test.ts(79,13): error TS2304: Cannot find name 'tick').

The rebased commit (that test yields with this file's settled() instead; collector 58/58, typecheck clean) is in bun run check fast now. I'll push it once and mark the PR ready again.

@devswha
devswha marked this pull request as draft October 24, 2026 16:03
@devswha
devswha force-pushed the fix/collector-test-timers branch from 01e1aed to b5ca085 Compare October 24, 2026 18:06
@devswha
devswha marked this pull request as ready for review October 24, 2026 18:06
@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto main and marked ready for review. It was a draft only because its old head (01e1aed) broke the typecheck after #1865: main's lifecycle-subscription test calls tick(), which this PR removes. The rebased head b5ca085 runs the collector's recovery tests on the test's clock against the current main. bun run check fast passed on it before the push.

@devswha

devswha commented Oct 24, 2026

Copy link
Copy Markdown
Owner Author

Merge pass review-work report. Overall: PASSED. This PR changes tests only. 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 server/collector.test.ts 5 runs, 57/57 each; conventions and typecheck pass; git merge-tree clean against main, #1865 (shares collector.test.ts) and #1856 (shares fixed-sleeps.json)

Mutants: each of these fails at least one test.

  • Snapshot retry removed.
  • Debounce raised to 1000 ms.
  • Debounce never sends the structure change.
  • Replay settle wait removed.
  • elapse never advances the clock: 11 tests fail, so the tests do not pass because nothing ran.

Fake timers

  • The fake herdr is in memory, and collector.ts has no Date.now.
  • settled() uses the unfaked setImmediate, so promise chains finish before each step.
  • Timers are restored even when a test fails.

Ratchet: server/collector.test.ts goes from 61 to 0, and bun scripts/fixed-sleeps.ts agrees.

Follow-ups (LOW)

  • The backoff test still switches fake timers itself.
  • collector.stop() is called outside try/finally. This predates the PR.

b5ca085 (out of draft, rebased onto main with #1865): the PR also turns the one tick() that #1865's new collector test added into settled(). The merge pass ran server/collector.test.ts 3 times (58/58 each), conventions and the typecheck.

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

@devswha
devswha merged commit 9b2ad0f into main Oct 24, 2026
19 of 27 checks passed
@devswha
devswha deleted the fix/collector-test-timers branch October 24, 2026 20:33
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: server/collector.test.ts waits on fixed ticks for its timers

1 participant