2016-03-11 02:52:46 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-03-11 03:51:02 +00:00
|
|
|
// WaitFor polls the given function 'f', once every 'interval', up to 'timeout'.
|
2016-03-11 02:52:46 +00:00
|
|
|
func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error {
|
|
|
|
var lastErr string
|
2016-03-11 03:51:02 +00:00
|
|
|
timeup := time.After(timeout)
|
2016-03-11 02:52:46 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeup:
|
|
|
|
return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-03-11 03:51:02 +00:00
|
|
|
time.Sleep(interval)
|
2016-03-11 02:52:46 +00:00
|
|
|
}
|
|
|
|
}
|