Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 673e331

Browse files
committed
Improve testutil.Eventually autocomplete signature and add message
1 parent ecee858 commit 673e331

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

testutil/eventually.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testutil
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
"time"
78

@@ -20,22 +21,31 @@ import (
2021
//
2122
// condition is not run in a goroutine; use the provided
2223
// context argument for cancellation if required.
23-
func Eventually(ctx context.Context, t testing.TB, condition func(context.Context) bool, tick time.Duration) bool {
24+
func Eventually(ctx context.Context, t testing.TB, condition func(ctx context.Context) (done bool), tick time.Duration, msgAndArgs ...interface{}) (done bool) {
2425
t.Helper()
2526

2627
if _, ok := ctx.Deadline(); !ok {
2728
panic("developer error: must set deadline or timeout on ctx")
2829
}
2930

31+
msg := "Eventually timed out"
32+
if len(msgAndArgs) > 0 {
33+
if m, ok := msgAndArgs[0].(string); ok {
34+
msg = fmt.Sprintf(m, msgAndArgs[1:]...)
35+
} else {
36+
panic("developer error: first argument of msgAndArgs must be a string")
37+
}
38+
}
39+
3040
ticker := time.NewTicker(tick)
3141
defer ticker.Stop()
3242
for tick := ticker.C; ; {
3343
select {
3444
case <-ctx.Done():
35-
assert.NoError(t, ctx.Err(), "Eventually timed out")
45+
assert.NoError(t, ctx.Err(), msg)
3646
return false
3747
case <-tick:
38-
if !assert.NoError(t, ctx.Err(), "Eventually timed out") {
48+
if !assert.NoError(t, ctx.Err(), msg) {
3949
return false
4050
}
4151
if condition(ctx) {

0 commit comments

Comments
 (0)