Back to all reviewers

avoid timing dependencies

istio/istio
Based on 3 comments
Go

Eliminate timing dependencies in tests by using proper retry mechanisms and assertions instead of sleep statements. Sleep-based waits make tests flaky and unreliable, while retry mechanisms with conditions provide more robust test execution.

Testing Go

Reviewer Prompt

Eliminate timing dependencies in tests by using proper retry mechanisms and assertions instead of sleep statements. Sleep-based waits make tests flaky and unreliable, while retry mechanisms with conditions provide more robust test execution.

Replace time.Sleep() calls with assert.EventuallyEqual() or appropriate retry functions that wait for specific conditions to be met. Choose retry.Converge() over retry.MaxAttempts() when you need consistent state verification, as it waits for multiple consecutive successful checks rather than potentially giving up early during system startup.

Example of problematic code:

time.Sleep(2 * time.Second) // wait for the namespace to be created

Preferred approach:

assert.EventuallyEqual(t, func() int {
    se := rig.se.Get("pre-existing", "default")
    return len(autoallocate.GetAddressesFromServiceEntry(se))
}, 2, retry.Converge(10), retry.Delay(time.Millisecond*5))

This approach makes tests more reliable by waiting for actual conditions rather than arbitrary time periods, reducing flakiness and improving test execution speed.

3
Comments Analyzed
Go
Primary Language
Testing
Category

Source Discussions