refactor: back the e2e Eventually helpers with testify

Delegate the hand-rolled deadline loops to require.Eventually and
require.EventuallyWithT instead of open-coding the polling.
This commit is contained in:
Julian Sommer
2026-09-01 12:02:39 +10:00
parent a5d942ec24
commit ee258fd248
+7 -26
View File
@@ -6,44 +6,25 @@ import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Eventually polls cond until it returns true or the timeout expires.
// Replaces every `sleep N; <probe>` loop in the bash scripts.
func Eventually(t *testing.T, cond func() bool, timeout, interval time.Duration, msgAndArgs ...any) {
t.Helper()
deadline := time.Now().Add(timeout)
for {
if cond() {
return
}
if time.Now().After(deadline) {
if len(msgAndArgs) == 0 {
t.Fatalf("Eventually: condition not met within %s", timeout)
}
t.Fatalf("Eventually: condition not met within %s: %v", timeout, msgAndArgs)
}
time.Sleep(interval)
}
require.Eventually(t, cond, timeout, interval, msgAndArgs...)
}
// EventuallyErr is like Eventually but lets cond return an error for the
// failure message. Useful when the probe itself yields diagnostic detail.
func EventuallyErr(t *testing.T, cond func() error, timeout, interval time.Duration) {
t.Helper()
deadline := time.Now().Add(timeout)
var lastErr error
for {
if err := cond(); err == nil {
return
} else {
lastErr = err
}
if time.Now().After(deadline) {
t.Fatalf("EventuallyErr: condition not met within %s: %v", timeout, lastErr)
}
time.Sleep(interval)
}
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.NoError(c, cond())
}, timeout, interval)
}
// RetryWithReset runs fn as "attempt-1"; on failure it calls ResetAllNodes and