Fix flaky TestServer_HealthMonitoring_Enabled and related tests#3990
Merged
Fix flaky TestServer_HealthMonitoring_Enabled and related tests#3990
Conversation
Replace time.Sleep-based synchronization with require.Eventually polling in three health monitoring server tests. The tests were flaky because: - DegradedThreshold (5ms) and Timeout (10ms) were too tight for CI/race detector, causing mock calls to exceed thresholds - time.Sleep waits are inherently racy and non-deterministic Changes: - Use require.Eventually to poll for expected health status instead of sleeping a fixed duration - Increase Timeout from 10ms to 5s and DegradedThreshold from 5ms to 2s (mock calls never take that long, but prevents false degraded status) - Replace trailing time.Sleep shutdown waits with select on errCh - Add proper server startup error handling in HandleBackendHealth test Verified with -race -count=20 on all three tests (60/60 pass, 0 races). Fixes #3985 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3990 +/- ##
==========================================
+ Coverage 68.56% 68.58% +0.02%
==========================================
Files 437 437
Lines 44662 44662
==========================================
+ Hits 30621 30633 +12
+ Misses 11657 11647 -10
+ Partials 2384 2382 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
jhrozek
approved these changes
Mar 4, 2026
reyortiz3
pushed a commit
that referenced
this pull request
Mar 4, 2026
Replace time.Sleep-based synchronization with require.Eventually polling in three health monitoring server tests. The tests were flaky because: - DegradedThreshold (5ms) and Timeout (10ms) were too tight for CI/race detector, causing mock calls to exceed thresholds - time.Sleep waits are inherently racy and non-deterministic Changes: - Use require.Eventually to poll for expected health status instead of sleeping a fixed duration - Increase Timeout from 10ms to 5s and DegradedThreshold from 5ms to 2s (mock calls never take that long, but prevents false degraded status) - Replace trailing time.Sleep shutdown waits with select on errCh - Add proper server startup error handling in HandleBackendHealth test Verified with -race -count=20 on all three tests (60/60 pass, 0 races). Fixes #3985 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
time.Sleep-based synchronization withrequire.Eventuallypolling in three flaky health monitoring server testsTimeout(10ms → 5s) andDegradedThreshold(5ms → 2s) so mock calls don't falsely trigger degraded/timeout status under CI load or race detectortime.Sleepshutdown waits with properselectonerrChRoot Cause
The tests used
time.Sleep(200ms)to wait for health checks and had aDegradedThresholdof 5ms. Under CI load or with-race, mock calls could exceed 5ms, causing backends to be marked "degraded" instead of "healthy". Additionally, callingWaitForInitialHealthChecks()from the test goroutine races withStart()(which runs in a separate goroutine) because the server'sReady()channel fires beforehealthMonitor.Start()callsWaitGroup.Add().Test plan
go test -race -run TestServer_HealthMonitoring_Enabled -count=20— 20/20 pass, 0 racesgo test -race -run TestServer_HandleBackendHealth_Enabled -count=20— 20/20 pass, 0 racesgo test -race -run TestServer_Stop_StopsHealthMonitor -count=20— 20/20 pass, 0 racesgo test -race -run TestServer_Health -count=5— full suite passesFixes #3985
🤖 Generated with Claude Code