@@ -2,6 +2,7 @@ package testutil
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"testing"
6
7
"time"
7
8
@@ -20,22 +21,31 @@ import (
20
21
//
21
22
// condition is not run in a goroutine; use the provided
22
23
// 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 ) {
24
25
t .Helper ()
25
26
26
27
if _ , ok := ctx .Deadline (); ! ok {
27
28
panic ("developer error: must set deadline or timeout on ctx" )
28
29
}
29
30
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
+
30
40
ticker := time .NewTicker (tick )
31
41
defer ticker .Stop ()
32
42
for tick := ticker .C ; ; {
33
43
select {
34
44
case <- ctx .Done ():
35
- assert .NoError (t , ctx .Err (), "Eventually timed out" )
45
+ assert .NoError (t , ctx .Err (), msg )
36
46
return false
37
47
case <- tick :
38
- if ! assert .NoError (t , ctx .Err (), "Eventually timed out" ) {
48
+ if ! assert .NoError (t , ctx .Err (), msg ) {
39
49
return false
40
50
}
41
51
if condition (ctx ) {
0 commit comments