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>
2022-08-05 15:34:44 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-09 15:27:05 +00:00
|
|
|
"fmt"
|
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>
2022-08-05 15:34:44 +00:00
|
|
|
"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.
|
2022-11-09 15:27:05 +00:00
|
|
|
func Eventually(ctx context.Context, t testing.TB, condition func(ctx context.Context) (done bool), tick time.Duration, msgAndArgs ...interface{}) (done bool) {
|
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>
2022-08-05 15:34:44 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if _, ok := ctx.Deadline(); !ok {
|
|
|
|
panic("developer error: must set deadline or timeout on ctx")
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:27:05 +00:00
|
|
|
msg := "Eventually timed out"
|
|
|
|
if len(msgAndArgs) > 0 {
|
2023-02-02 18:36:27 +00:00
|
|
|
m, ok := msgAndArgs[0].(string)
|
|
|
|
if !ok {
|
2022-11-09 15:27:05 +00:00
|
|
|
panic("developer error: first argument of msgAndArgs must be a string")
|
|
|
|
}
|
2023-02-02 18:36:27 +00:00
|
|
|
msg = fmt.Sprintf(m, msgAndArgs[1:]...)
|
2022-11-09 15:27:05 +00:00
|
|
|
}
|
|
|
|
|
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>
2022-08-05 15:34:44 +00:00
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for tick := ticker.C; ; {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-11-09 15:27:05 +00:00
|
|
|
assert.NoError(t, ctx.Err(), msg)
|
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>
2022-08-05 15:34:44 +00:00
|
|
|
return false
|
|
|
|
case <-tick:
|
2022-11-09 15:27:05 +00:00
|
|
|
if !assert.NoError(t, ctx.Err(), msg) {
|
2022-08-05 16:09:20 +00:00
|
|
|
return false
|
|
|
|
}
|
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>
2022-08-05 15:34:44 +00:00
|
|
|
if condition(ctx) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|