Skip to content

test(modes): wait on the condition, not on time - #1422

Merged
amiddavid merged 5 commits into
mainfrom
test/fix-modes
Nov 25, 2026
Merged

amiddavid merged 5 commits into
mainfrom
test/fix-modes

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Owner, 2026-11-24: "tests that are based on time will go into the pool that runs on integrator, and tests that can be fixed since time is a by product of poor design are fixed."

This PR fixes the 5 modes tests on the pending list and removes their 5 lines.

Test-only. No behavior change. Data dependence: none. Only modes/modes_test.go, the pending list and the timingguard baseline change.

Overlap: #35 also changes modes/modes_test.go. It is a held draft since 2026-08-10, so its next rebase gets a small conflict here.

What changed

Test Cause Fix
TestPoolRunsJobs 2 s hang detector, local 3 s waitFor 30 s detector, testutil.WaitFor
TestEnqueueDedupIsAtomic local 3 s waitFor testutil.WaitFor
TestFullQueueDropsAndNeverBlocks local 3 s waitFor, 5 s detector testutil.WaitFor, 30 s detector
TestPanickingJobIsContained local 3 s waitFor, 2 s detector testutil.WaitFor, 30 s detector
TestStopLeaksNoGoroutines settle() ran 20 sleeps of 5 ms Bounded poll (WaitForState) until runtime.NumGoroutine() <= before

Also:

  • The local waitFor and settle helpers are gone.
  • TestStopDoesNotWaitForASlowJob (the 2 s Stop grace is the behavior) keeps its tight stopGrace+3s bound only when testutil.TimingEnabled(). Otherwise the bound is stopGrace+30s.
  • The timingguard baseline for modes_test.go is lower.

How it was tested

macOS, Go 1.27.4, GOMAXPROCS=4.

  • CGO_ENABLED=1 go test -race -count=20 -run '^(the 5 tests)$' ./modes/: pass.
  • CGO_ENABLED=1 go test -race ./modes/: pass.
  • gofmt, go vet ./modes/, go test ./internal/timingguard/: pass.
  • Vacuity:
    • A leaked goroutine added after Stop: TestStopLeaksNoGoroutines fails at the 30 s bound with "goroutines before 2, now 3".
    • close(done) removed from the job that follows the panic: TestPanickingJobIsContained fails at the 30 s detector.

Local tests: waived (owner rule)

Replace the local 3 s waitFor with testutil.WaitFor. Replace the settle
sleeps with a bounded poll of runtime.NumGoroutine. Raise the 2 s and
5 s hang detectors to 30 s. Gate the tight Stop grace bound with
testutil.TimingEnabled. Remove 5 tests from the pending timing list.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 35910817ef49 approve

Verdict: approve

Findings

None. The change is test-only and correct.

Non-blocking

  • modes/modes_test.go:582 — before can count goroutines that are still exiting. The old settle() ran before the capture; the new code captures runtime.NumGoroutine() at once. A worker of the test before (TestFullQueueDropsAndNeverBlocks) may not be off the goroutine list yet. before then reads 1-2 too high, and the check <= before can mask a leak of that many goroutines. A real leak loses all 4 workers, so the check still catches it. Fix if you want it exact: wait first for the count to stop falling, then capture before.
  • modes/modes_test.go:618 — the default Stop bound is now loose. A plain run accepts a Stop that blocks up to stopGrace + 30 s. The tight bound (stopGrace + 3 s) runs only with CG_TIMING_TESTS=1. This follows the timing-test rules, and the tight bound does run: tier 2 flags this PR (timing-tests-needed.sh sees the new TimingEnabled line), heavy CI and Part A set the variable. No change needed. I list it so the merge record names the weaker default.

What I checked

  • The full diff: 3 files, 1 commit (35910817). The commit message matches the change. DCO sign-off and Assisted-By: per the repo convention.
  • timingguard baseline. I applied the scanner rules of internal/timingguard/timingguard_test.go by hand to the new modes/modes_test.go: 0 sleeps, 0 poll loops, 3 select cases on <-time.After (lines 503, 629, 645). The deadline := time.After(30 s) at line 555 counts as none of the three kinds (no receive of time.After, and case <-deadline is not a deadline-name check). The edited line modes/modes_test.go after=3 is what -update would write. No other file's counts moved.
  • testutil.WaitFor / WaitForState / TimingEnabled. Read internal/testutil/testutil.go. The 30 s bound is cut to the test deadline minus 5 s, so no run hangs past -timeout. The TimingEnabled guard matches the documented rule "a test that checks more than time can guard only its time check", and has precedent (proxyapp/shutdown_test.go:75, proxy/tooldefer_866_test.go:237).
  • Each rewritten test keeps its assertion. Only the wait strategy changed:
    • TestPoolRunsJobs, TestEnqueueDedupIsAtomic, TestPanickingJobIsContained: same conditions, longer safety bound.
    • TestFullQueueDropsAndNeverBlocks: the 30 s deadline is a hang detector on Enqueue, not a timing assertion; the counter checks are unchanged.
    • TestStopDoesNotWaitForASlowJob: the functional check (Stop returns false while a job runs) still runs without the variable; the wall-clock bound moves behind TimingEnabled.
    • TestStopLeaksNoGoroutines: the wait-for-condition replaces the fixed settle(); a real leak still fails, after up to 30 s instead of immediately.
  • Test order. TestStopDoesNotWaitForASlowJob leaves its blocked worker until its own defer close(release). It runs after TestStopLeaksNoGoroutines in file order, so it cannot pollute the goroutine check.
  • pending-timing-fixes.txt. The 5 removed modes tests are the 5 this PR makes load-robust. TestStopDoesNotWaitForASlowJob was never in the list. The only consumer of the file is the hand rule in CLAUDE.md; no script parses it.
  • Coverage of the gated bound. scripts/dev/timing-tests-needed.sh prints "run" for this PR (the diff adds a TimingEnabled line), .github/workflows/ci.yaml sets CG_TIMING_TESTS: "1" (lines 95, 237), and scripts/release/part-a.sh defaults it to 1. So the tight Stop bound runs in tier 2 for this PR, in heavy CI, and in Part A.
  • No production code changed. modes/pool.go and modes/tracker.go are untouched. No data-dependence row applies (no feature change). No behavior change to name at the top of the PR.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 35910817ef49 blocking

Verdict: blocking

Findings

  1. modes/modes_test.go:582 — the leak check can pass while the pool leaks one goroutine, because the baseline is captured before the prior test's goroutines exit
    • What fails: a regression where Pool.Stop leaves 1 of the 4 workers alive still passes TestStopLeaksNoGoroutines.
    • Proof: Pool.Stop returns as soon as done closes, but the waiter goroutine that closed done may still exist (modes/pool.go:190-199). The test before this one in source order ends with defer p.Stop() (modes/modes_test.go:546), so that waiter can still be alive when line 582 captures before. before is then S+1 while the steady count is S. A pool that leaks 1 worker ends at S+1, and the condition runtime.NumGoroutine() <= before (modes/modes_test.go:592) holds, so the test passes. The removed settle() call ran before the capture, so the old code compared against the steady count S and failed on the same leak. The repo's own equivalent test states this rule: proxy/modes_test.go:301-305 settles first, with the comment "Everything unrelated (the mock upstream's own goroutines) is created BEFORE the baseline, so the only difference this measures is the pool's."
    • Fix: make the baseline tight before you capture it. For example, wait (bounded, testutil.WaitForState) until runtime.NumGoroutine() is unchanged across two polls, then capture before; or keep a small settle before the capture, marked //timing:ok the leak baseline must be quiet.

Non-blocking

  • modes/modes_test.go:592-595 — the state func reads runtime.NumGoroutine() again, so the number it reports can differ from the one the condition just read. Cosmetic; the message stays useful.
  • modes/modes_test.go:617-621 — in a normal run, TestStopDoesNotWaitForASlowJob now proves only that Stop returns within ~32 s, not within its grace. This is deliberate, commented, and the tight bound still runs when CG_TIMING_TESTS=1. Accepted.

What I checked

  • The full diff of the one commit (3591081): modes/modes_test.go, internal/timingguard/baseline.txt, scripts/release/pending-timing-fixes.txt. The commit message follows the DCO and Assisted-By rules.
  • modes/modes_test.go in full, and modes/pool.go (NewPool, Enqueue, Stop, stopGrace = 2 s) as the behavior under test.
  • testutil.WaitFor, WaitForState and TimingEnabled (internal/testutil/testutil.go): 30 s bound, cut to the test deadline minus 5 s, condition checked once more after the last sleep. Correct replacements for the deleted local waitFor.
  • The timingguard scanner (internal/timingguard/timingguard_test.go), traced by hand over the new file: 3 select cases on <-time.After (lines 503, 629, 645), no time.Sleep, no poll loop. deadline := time.After(30 * time.Second) (line 555) is an assign, not a receive, so it counts as nothing. The new baseline entry modes/modes_test.go after=3 matches.
  • The deleted waitFor and settle helpers have no other user in the modes package.
  • The 5 lines removed from scripts/release/pending-timing-fixes.txt: each named test is fixed by this diff (local waitFor, settle, or a tight bound replaced). The file header says a PR that fixes a listed test removes its line in the same PR.
  • No behavior change, no data-dependence change, no binaries: test-only PR.
  • I did not run builds or tests. Live tests are not approved for this review.

A previous test's Stop waiter could still be alive when the baseline was
read, so a leak of 1 worker passed. Wait until the count holds still for
5 polls, then read the baseline. The failure message now shows the same
count the condition read.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Fixed in the new head. Finding 1 (modes_test.go:582): the test now waits until runtime.NumGoroutine() holds still for 5 polls in a row (30 s bound) before it reads before. Non-blocking note on state: it now reports the same count the condition read. Stop bound unchanged.

Vacuity: one leaked goroutine after Stop fails the new test at the 30 s bound ("goroutines before 2, now 3"). On the old head the same leak also failed 5 of 5 runs here, because the previous test's waiter exits fast and the window is narrow. The window is real, so the quiet baseline closes it.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 c390c109dd78 approve

Verdict: approve

Findings

None. The fix commit is correct and closes the round-1 window.

Earlier findings

  • Baseline read too early in TestStopLeaksNoGoroutines (my non-blocking note; reviewer 2's finding 1, blocking): fixed by 16aa8147. The test now waits until runtime.NumGoroutine() holds the same value over 5 polls in a row (2 ms apart, 30 s bound) before it reads before. I checked the fix myself:
    • A foreign goroutine that is still alive makes the count drop when it exits, which resets steady, so the quiet wait does not end while the count is still falling.
    • The closure state (last, steady) is used by WaitFor on the test goroutine only, so there is no race.
    • The wait runs before NewPool, so it cannot mask a leak of the pool under test.
    • Reviewer 2 asked for 2 steady polls; the fix uses 5. Stronger than asked.
    • I agree with reviewer 2's blocking framing. I had the same point as non-blocking in round 1; a partial leak (1 of 4 workers) is realistic, so blocking was the right call.
  • The state func read NumGoroutine again (reviewer 2's non-blocking): fixed. The condition now stores now, and the message prints that same value.
  • Loose default Stop bound (my non-blocking; reviewer 2's non-blocking): unchanged, accepted. The author says so in the PR comment. The tight bound runs under CG_TIMING_TESTS=1, which tier 2, heavy CI and Part A set. No action needed.

What I checked

  • Last review: round 1, 35910817ef4937d577c2838321b5e9334ac05016, 2026-11-25T17:10:27Z.
  • New commits read: 4 in 35910817..c390c109 — 2 PR-owned: 16aa8147 (test(modes): make the goroutine baseline quiet before the leak check), c390c109 (merge of origin/main); 2 from main via the merge: 9c64a6a3 (proxy test price callback), ecd3c648 (RELEASING.md docs). I read both main commits' diffs; they do not touch modes/, internal/timingguard/ or scripts/release/pending-timing-fixes.txt, so they cannot interact with this PR's change.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt).
  • New items since my last review, and what I did about each:
    • Reviewer 2's round-1 review (17:10:57Z): re-derived its finding against my own round-1 analysis (same defect, tighter proof: the Stop waiter at modes/pool.go:190-199). Confirmed.
    • Author comment (17:15:49Z): describes the fix and a 5-of-5 local vacuity check. Verified each claim against the diff of 16aa8147.
    • Commit 16aa8147: read the full diff and the changed test in the file around it.
    • Merge c390c109: verified it is a clean merge — git diff 16aa8147..c390c109 changes only RELEASING.md and proxy/predictive_agent_test.go, exactly main's two commits.
  • The PR diff against the new base (9c64a6a3..c390c109) is still the same 3 files as round 1: modes/modes_test.go, internal/timingguard/baseline.txt, scripts/release/pending-timing-fixes.txt.
  • timingguard baseline still matches. The fix adds no time.Sleep, time.After or poll loop; testutil.WaitFor is in the exempt internal/testutil/ prefix. The file still counts 3 select time.After cases (the hang detectors), so modes/modes_test.go after=3 stays correct.
  • Flake risk of the quiet wait. A GC cycle or a slow-to-schedule dying goroutine can reset steady; the wait then simply continues inside its 30 s bound (cut to the test deadline minus 5 s). Nothing in this package keeps changing the count at that point. I see no realistic false timeout.
  • No production code changed, no data-dependence row applies, no client rule, no host-boundary question. DCO sign-off and Assisted-By: are on both new commits.
  • I did not run builds or tests. Live tests are not approved for this review.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.332 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 c390c109dd78 approve

Verdict: approve

Findings

None. My round-1 finding is fixed.

Earlier findings

  • My round-1 finding 1 (modes_test.go:582, an inflated goroutine baseline can mask a 1-worker leak): fixed in 16aa8147. The test now waits until runtime.NumGoroutine() holds the same value over 5 polls in a row, then reads before (modes/modes_test.go:582-594). A straggler from the prior test's Stop resets the stability counter when it exits, so it cannot inflate the baseline. A leak of 1 worker now ends at S+1 against a baseline of S and fails. The comment on the test states the rule, in the words of the proxy/modes_test.go precedent. The 5-poll quiet window is a heuristic, not a proof — a straggler that needs more than ~10 ms to exit can still slip past it — but the old settle() was the same kind of heuristic at 100 ms, and this closes the window I named.
  • My round-1 non-blocking note (the state func read a different count than the condition): fixed. The condition now stores the count in now and the state func reports that same value (modes/modes_test.go:605-610).
  • My round-1 non-blocking note (the loose Stop bound in a normal run): accepted, unchanged. The author's comment says so, and the tight bound still runs under CG_TIMING_TESTS=1.
  • Reviewer 1's two non-blocking notes: the first is the same baseline issue as my finding — fixed by the same commit. The second is the same loose Stop bound — accepted, unchanged. I checked both myself; I have nothing to add.

What I checked

  • Last review: round 1, 3591081, 2026-11-25T17:10:57Z.
  • New commits read: 4 — ecd3c648 docs(release): 3 merge rules from v0.7.9; 9c64a6a3 test(proxy): pass the prompt-size price callback; 16aa8147 test(modes): make the goroutine baseline quiet; c390c109 merge of origin/main.
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt).
  • New items since my last review, and what I did about each:
    • Reviewer 1's round-1 review (posted 30 s before mine): read in full. Its two non-blocking notes match my checks; no new finding to verify beyond them.
    • The author's comment (2026-11-25T17:15:49Z): describes the fix and a vacuity check (a forced 1-goroutine leak failed the new test). I verified the claim by reading the code, not by running it.
    • Commit 16aa8147: the fix. Verified as above.
    • Commits ecd3c648 and 9c64a6a3 (arrived through the merge of main): read both diffs in full. ecd3c648 is docs-only (RELEASING.md). 9c64a6a3 changes one test line; I checked the new callback against campaign.Rows (campaign/replay.go:161), which takes func(model string, promptTokens int64) — the fix matches. Both came from their own merged PRs (#1405, #1426).
  • The fix commit against the timingguard rules: the quiet-wait loop lives in internal/testutil (exempt); modes/modes_test.go itself gains no sleep, poll, or time.After. The merged internal/timingguard/baseline.txt still says modes/modes_test.go after=3, the PR's value — the merge did not keep main's stale sleep=2 poll=1 after=3 line, which would have failed the ratchet.
  • The fix's variables (last, steady, now) are written and read only inside the condition and state closures, which WaitFor/WaitForState call from the test goroutine (internal/testutil/testutil.go:46-71). No race under -race.
  • The full PR diff against the new base (9c64a6a3..HEAD): 3 files, the same set as round 1 plus the fix. No production code, no behavior change, no data dependence, no binaries.
  • I did not run builds or tests. Live tests are not approved for this review.

Signed-off-by: DAVID AMID <[email protected]>

# Conflicts:
#	internal/timingguard/baseline.txt
#	scripts/release/pending-timing-fixes.txt
Signed-off-by: DAVID AMID <[email protected]>

# Conflicts:
#	internal/timingguard/baseline.txt
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Head moved by a merge of main, with a conflict resolved only in the timing lists.

approval-still-valid.sh 1422 c390c109, exit 0:

approved: c390c109dd78 (base 9c64a6a336bd) patch-id c3dc7a2d19f4
head:     4070aecd0f59 (base 967cc7729002) patch-id 06ec0bc9fb31
--- git range-diff
LISTS ONLY: the code is the same, and the 2 timing list files remove and add the same lines as at the approved head. The approval of c390c109dd78 still counts. Say so on the PR.

CI is green at 4070aec.

Local tests: waived (owner rule)

@amiddavid
amiddavid merged commit 3606f6e into main Nov 25, 2026
10 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant