chore: add testutil.Eventually and friends (#3389)

This PR adds a `testutil` function aimed to replace `require.Eventually`.

Before:
```go
require.Eventually(t, func() bool { ... }, testutil.WaitShort, testutil.IntervalFast)
```

After:
```go
require.True(t, testutil.EventuallyShort(t, func(ctx context.Context) bool { ... }))

// or the full incantation if you need more control
ctx, cancel := context.WithTimeout(ctx.Background(), testutil.WaitLong)
require.True(t, testutil.Eventually(t, ctx, func(ctx context.Context) bool { ... }, testutil.IntervalSlow))
```

Co-authored-by: Mathias Fredriksson <mafredri@gmail.com>
This commit is contained in:
Cian Johnston
2022-08-05 16:34:44 +01:00
committed by GitHub
parent 46d64c624a
commit 01fe5e668e
5 changed files with 150 additions and 12 deletions

68
testutil/eventually.go Normal file
View File

@ -0,0 +1,68 @@
package testutil
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Eventually is like require.Eventually except it allows passing
// a context into the condition. It is safe to use with `require.*`.
//
// If ctx times out, the test will fail, but not immediately.
// It is the caller's responsibility to exit early if required.
//
// It is the caller's responsibility to ensure that ctx has a
// deadline or timeout set. Eventually will panic if this is not
// the case in order to avoid potentially waiting forever.
//
// condition is not run in a goroutine; use the provided
// context argument for cancellation if required.
func Eventually(ctx context.Context, t testing.TB, condition func(context.Context) bool, tick time.Duration) bool {
t.Helper()
if _, ok := ctx.Deadline(); !ok {
panic("developer error: must set deadline or timeout on ctx")
}
ticker := time.NewTicker(tick)
defer ticker.Stop()
for tick := ticker.C; ; {
select {
case <-ctx.Done():
assert.NoError(t, ctx.Err(), "Eventually timed out")
return false
case <-tick:
assert.NoError(t, ctx.Err(), "Eventually timed out")
if condition(ctx) {
return true
}
}
}
}
// EventuallyShort is a convenience function that runs Eventually with
// IntervalFast and times out after WaitShort.
func EventuallyShort(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitShort)
defer cancel()
return Eventually(ctx, t, condition, IntervalFast)
}
// EventuallyMedium is a convenience function that runs Eventually with
// IntervalMedium and times out after WaitMedium.
func EventuallyMedium(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitMedium)
defer cancel()
return Eventually(ctx, t, condition, IntervalMedium)
}
// EventuallyLong is a convenience function that runs Eventually with
// IntervalSlow and times out after WaitLong.
func EventuallyLong(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitLong)
defer cancel()
return Eventually(ctx, t, condition, IntervalSlow)
}