2014-05-07 02:48:25 +02:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2017-01-26 19:05:56 -08:00
|
|
|
"fmt"
|
2014-05-16 15:49:47 -07:00
|
|
|
"time"
|
2017-01-26 00:13:34 -05:00
|
|
|
|
2017-03-23 20:04:39 -07:00
|
|
|
"github.com/pkg/errors"
|
2014-05-07 02:48:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testFn func() (bool, error)
|
|
|
|
|
2017-01-26 17:11:16 -08:00
|
|
|
const (
|
|
|
|
baseWait = 1 * time.Millisecond
|
|
|
|
maxWait = 100 * time.Millisecond
|
|
|
|
)
|
2014-05-07 02:48:25 +02:00
|
|
|
|
2017-03-23 16:26:05 -04:00
|
|
|
func WaitForResult(try testFn) error {
|
2017-01-26 17:11:16 -08:00
|
|
|
var err error
|
|
|
|
wait := baseWait
|
|
|
|
for retries := 100; retries > 0; retries-- {
|
|
|
|
var success bool
|
|
|
|
success, err = try()
|
2014-05-07 02:48:25 +02:00
|
|
|
if success {
|
2017-01-26 19:05:56 -08:00
|
|
|
time.Sleep(25 * time.Millisecond)
|
2017-03-23 16:26:05 -04:00
|
|
|
return nil
|
2014-05-07 02:48:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 17:11:16 -08:00
|
|
|
time.Sleep(wait)
|
|
|
|
wait *= 2
|
|
|
|
if wait > maxWait {
|
|
|
|
wait = maxWait
|
2014-05-07 02:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 20:04:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "timed out with error")
|
|
|
|
}
|
2017-04-20 18:59:42 -07:00
|
|
|
return fmt.Errorf("timed out")
|
2014-05-07 02:48:25 +02:00
|
|
|
}
|