Skip to content

Commit 35f7c74

Browse files
committed
test: replace time.Sleep with require.Eventually in health integration test
Replace fixed duration time.Sleep calls with require.Eventually polling to make the health endpoint integration test more robust and less flaky. Changes: - Use require.Eventually to poll for health state transitions - Poll every 100ms instead of fixed sleeps - Generous timeouts (5s and 10s) that terminate early on success - Better error handling during polling Benefits: - More resilient to timing variations in CI/CD environments - Faster test execution (completes as soon as conditions are met) - Eliminates magic numbers (1700ms compensation) - Expresses intent clearly (wait until condition is met) - Non-flaky (tested 3x consecutively)
1 parent f9abded commit 35f7c74

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

node/single_sequencer_integration_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,25 @@ func TestHealthEndpointWhenBlockProductionStops(t *testing.T) {
466466
// warnThreshold = blockTime * 3 = 1500ms = 1.5s
467467
// failThreshold = blockTime * 5 = 2500ms = 2.5s
468468

469-
// Wait for WARN threshold (3x block time = 1.5 seconds after last block)
470-
// We need to wait a bit longer to account for the time blocks take to stop
471-
time.Sleep(1700 * time.Millisecond)
472-
473-
health, err = rpcClient.GetHealth(ctx)
474-
require.NoError(err)
475-
// Health could be WARN or FAIL depending on exact timing, but should not be PASS
476-
require.NotEqual("PASS", health.String(), "Health should not be PASS after block production stops")
477-
478-
// Wait for FAIL threshold (5x block time = 2.5 seconds total after last block)
479-
time.Sleep(1500 * time.Millisecond)
480-
481-
health, err = rpcClient.GetHealth(ctx)
482-
require.NoError(err)
483-
require.Equal("FAIL", health.String(), "Health should be FAIL after 5x block time without new blocks")
469+
// Poll for health to transition away from PASS (to WARN or FAIL)
470+
// This is more robust than fixed time.Sleep as it handles timing variations
471+
require.Eventually(func() bool {
472+
health, err := rpcClient.GetHealth(ctx)
473+
if err != nil {
474+
return false
475+
}
476+
return health.String() != "PASS"
477+
}, 5*time.Second, 100*time.Millisecond, "Health should transition away from PASS after block production stops")
478+
479+
// Poll for health to reach FAIL state
480+
// Timeout is set to 10 seconds to be safe, but should happen around 2.5s
481+
require.Eventually(func() bool {
482+
health, err := rpcClient.GetHealth(ctx)
483+
if err != nil {
484+
return false
485+
}
486+
return health.String() == "FAIL"
487+
}, 10*time.Second, 100*time.Millisecond, "Health should be FAIL after 5x block time without new blocks")
484488

485489
// Stop the node and wait for shutdown
486490
shutdownAndWait(t, []context.CancelFunc{cancel}, &runningWg, 10*time.Second)

0 commit comments

Comments
 (0)