Repository navigation
test: stabilize flaky tests (rate-limit, evidence, privval-tcp) - #1339
Conversation
…rate-limit test parallelSendWithLimit asserted elapsed == 3.0 s exactly via math.Floor, which flaked (~25%) under load because the wall-clock can drift by several hundred milliseconds. assertRateLimits asserted exact token counts, which flaked when the token bucket refill landed 1 token off due to scheduler jitter. Replace both with tolerance bands: - elapsed: GreaterOrEqual(3s) / LessOrEqual(3+2 s) instead of exact equality - per-peer counts: ±1 slack around the expected value No production code changed. No new dependencies added. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…imeout) The inner loop in waitForEvidence had no timeout: any propagation stall would spin until the 5-minute go test package deadline killed the process, producing an opaque timeout rather than a useful failure message. Replace the bare for-loop with require.Eventually (30 s / 100 ms), which gives a clear diagnostic message and respects test deadlines. The success condition is unchanged. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…odeSetPrivValTCP Two bugs in TestNodeSetPrivValTCP: 1. A spawned goroutine called require.NoError(t, signerServer.Start(ctx)), which calls runtime.Goexit on the wrong goroutine — undefined behaviour per the testify docs. Fixed by sending the error on a buffered channel and asserting on the test goroutine after the server is confirmed running. 2. The TCP dial/read-write timeout was 100 ms, too tight for cold -race CI. Raised to 2 s. Added require.Eventually to wait for IsRunning before the client dials, so there is no race between goroutine scheduling and the connection attempt. Co-Authored-By: Claude Opus 4.6 <[email protected]>
📝 WalkthroughWalkthroughThree test files are updated to fix flaky behavior. ChangesTest Flakiness Fixes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Pull request overview
This PR aims to stabilize three chronically flaky tests by reducing sensitivity to timing jitter and avoiding long-running package-level timeouts, without changing production behavior.
Changes:
internal/p2p/client: loosened rate-limit test assertions to tolerate scheduling jitter and slow CI.internal/evidence: replaced an unbounded busy-wait loop with a boundedrequire.Eventuallywait.node: fixedTestNodeSetPrivValTCPto avoid callingrequirefrom a non-test goroutine and increased tight TCP timeouts.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| node/node_test.go | Increases dial/read-write timeouts and routes server start errors back to the test goroutine. |
| internal/p2p/client/ratelimit_test.go | Replaces exact wall-clock/count assertions with tolerance bands to reduce flakiness. |
| internal/evidence/reactor_test.go | Converts evidence propagation waiting to a bounded Eventually-based wait to fail fast instead of timing out the package. |
| require.Eventually(t, func() bool { | ||
| var size int64 | ||
| localEvList, size = pool.PendingEvidence(int64(len(evList) * 1000)) | ||
| time.Sleep(time.Millisecond * 100) | ||
| t.Log("current wait status:", "|", | ||
| "local", len(localEvList), "|", |
Replace require.Eventually and require.Equalf inside fn (which was dispatched to spawned goroutines) with a plain time-bounded poll loop that returns an error. Goroutines send errors to a buffered channel; assertions happen on the test goroutine after wg.Wait(), satisfying Go's testing package contract that t.FailNow must not be called from non-test goroutines. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Replace the non-blocking channel drain (which ran after require.Eventually and could never be reached on Start failure) with a poll loop on the test goroutine that checks startErrCh on every tick. A Start() failure now surfaces with its actual error rather than a generic "did not start in time" timeout message. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
The lower-bound check (elapsed >= testTimeSeconds) was guaranteed to pass by the preceding time.Sleep, making it a no-op. Remove it and add a comment explaining why; keep only the meaningful upper-bound check (+2 s CI slack). Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Review-round update (Marvin + Copilot)Pushed fixes for this round's findings:
Note: a review flagged 🤖 Co-authored by Claudius the Magnificent AI Agent |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/evidence/reactor_test.go (1)
152-180:⚠️ Potential issue | 🟡 Minor | ⚡ Quick win
waitForEvidenceno longer fails when extra evidence appears (false-positive risk).From Line 152 onward, polling stops when
len(localEvList) >= len(evList), but the later map check only verifies expected hashes exist. That means extra evidence won’t fail the test, despite the comment saying it will. Add an explicit exact-length assertion before hash/content validation.Suggested fix
for len(localEvList) < len(evList) { ... } + + if len(localEvList) != len(evList) { + return fmt.Errorf("unexpected evidence count: have %d, want %d", + len(localEvList), len(evList)) + } // put the reaped evidence in a map so we can quickly check we got everything evMap := make(map[string]types.Evidence)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/evidence/reactor_test.go` around lines 152 - 180, The `waitForEvidence` function polls until `len(localEvList) >= len(evList)`, but the subsequent map-based validation only checks that expected evidence hashes exist—it does not fail if extra evidence is present, contradicting the comment that says it will catch extra evidence. Add an explicit length equality assertion after the map is populated (around where `evMap` is created from `localEvList`) to verify that `len(localEvList) == len(evList)` before or immediately after iterating through the expected evidence in the final loop, ensuring the test fails when unexpected extra evidence appears.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@internal/evidence/reactor_test.go`:
- Around line 152-180: The `waitForEvidence` function polls until
`len(localEvList) >= len(evList)`, but the subsequent map-based validation only
checks that expected evidence hashes exist—it does not fail if extra evidence is
present, contradicting the comment that says it will catch extra evidence. Add
an explicit length equality assertion after the map is populated (around where
`evMap` is created from `localEvList`) to verify that `len(localEvList) ==
len(evList)` before or immediately after iterating through the expected evidence
in the final loop, ensuring the test fails when unexpected extra evidence
appears.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 03258764-782f-4d1e-9e96-9316fabf5a6f
📒 Files selected for processing (3)
internal/evidence/reactor_test.gointernal/p2p/client/ratelimit_test.gonode/node_test.go
Issue being fixed or feature implemented
Three chronic flaky tests in the "Test" CI suite cause spurious red runs independent of code changes:
TestSendRateLimitflakes ~25%.internal/evidencecan hit the 5-min package timeout on any propagation stall.TestNodeSetPrivValTCPraces a tight 100ms timeout and (worse) callsrequirefrom a non-test goroutine (illegalFailNow/Goexit).What was done?
internal/p2p/clientrate-limit test: replaced exact wall-clock/count assertions with tolerance bands. Production limiter unchanged; no new dependency.internal/evidence: replaced the unboundedwaitForEvidencebusy-wait withrequire.Eventually(30s)so a stall fails fast and localized instead of timing out the whole package.nodeTestNodeSetPrivValTCP: report the server-goroutine error over a channel checked on the test goroutine (fixes the illegal off-goroutinerequire), wait for listener-ready, and raise the 100ms timeout to 2s.Test-only — no production behavior change. No new dependencies.
How Has This Been Tested?
-raceloops: T1 30/30, T2 10/10, T3 40/40.go build ./...,go vet, gofmt, golangci-lint all clean (no new issues).Breaking Changes
None — test-only.
Checklist:
For repository code-owners and collaborators only
🤖 Co-authored by Claudius the Magnificent AI Agent
Summary by CodeRabbit