2014-05-07 00:48:25 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2017-01-27 03:05:56 +00:00
|
|
|
"fmt"
|
2014-05-16 22:49:47 +00:00
|
|
|
"time"
|
2014-05-07 00:48:25 +00:00
|
|
|
)
|
|
|
|
|
2017-01-27 01:11:16 +00:00
|
|
|
const (
|
2017-04-27 00:42:50 +00:00
|
|
|
wait = 25 * time.Millisecond
|
|
|
|
timeout = 5 * time.Second
|
2017-01-27 01:11:16 +00:00
|
|
|
)
|
2014-05-07 00:48:25 +00:00
|
|
|
|
2017-04-27 00:42:50 +00:00
|
|
|
func WaitForResult(f func() (bool, error)) error {
|
|
|
|
stop := time.Now().Add(timeout)
|
|
|
|
for {
|
|
|
|
ok, err := f()
|
|
|
|
if ok {
|
2017-03-23 20:26:05 +00:00
|
|
|
return nil
|
2014-05-07 00:48:25 +00:00
|
|
|
}
|
2017-04-27 00:42:50 +00:00
|
|
|
if time.Now().After(stop) {
|
|
|
|
return fmt.Errorf("timeout: %s", err)
|
2014-05-07 00:48:25 +00:00
|
|
|
}
|
2017-04-27 00:42:50 +00:00
|
|
|
time.Sleep(wait)
|
2014-05-07 00:48:25 +00:00
|
|
|
}
|
|
|
|
}
|