itest: timeout while waiting for predicate result
This commit sends the returned value of pred() inside a goroutine such that when a predicate function never returns during the first run, the wait package can timeout properly.
This commit is contained in:
1 file changed
+14
-5
+14
-5
@@ -15,17 +15,26 @@ const PollInterval = 200 * time.Millisecond
|
|||||||
// some property is upheld within a particular time frame.
|
// some property is upheld within a particular time frame.
|
||||||
func Predicate(pred func() bool, timeout time.Duration) error {
|
func Predicate(pred func() bool, timeout time.Duration) error {
|
||||||
exitTimer := time.After(timeout)
|
exitTimer := time.After(timeout)
|
||||||
|
result := make(chan bool, 1)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
<-time.After(PollInterval)
|
<-time.After(PollInterval)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
result <- pred()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Each time we call the pred(), we expect a result to be
|
||||||
|
// returned otherwise it will timeout.
|
||||||
select {
|
select {
|
||||||
case <-exitTimer:
|
case <-exitTimer:
|
||||||
return fmt.Errorf("predicate not satisfied after time out")
|
return fmt.Errorf("predicate not satisfied after " +
|
||||||
default:
|
"time out")
|
||||||
}
|
|
||||||
|
|
||||||
if pred() {
|
case succeed := <-result:
|
||||||
return nil
|
if succeed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user