test: Retry more aggressively

This commit is contained in:
Frank Schroeder 2017-04-26 17:42:50 -07:00 committed by Frank Schröder
parent e371a5a9bf
commit 36380826b0
1 changed files with 10 additions and 23 deletions

View File

@ -3,36 +3,23 @@ package testutil
import (
"fmt"
"time"
"github.com/pkg/errors"
)
type testFn func() (bool, error)
const (
baseWait = 1 * time.Millisecond
maxWait = 100 * time.Millisecond
wait = 25 * time.Millisecond
timeout = 5 * time.Second
)
func WaitForResult(try testFn) error {
var err error
wait := baseWait
for retries := 100; retries > 0; retries-- {
var success bool
success, err = try()
if success {
time.Sleep(25 * time.Millisecond)
func WaitForResult(f func() (bool, error)) error {
stop := time.Now().Add(timeout)
for {
ok, err := f()
if ok {
return nil
}
time.Sleep(wait)
wait *= 2
if wait > maxWait {
wait = maxWait
if time.Now().After(stop) {
return fmt.Errorf("timeout: %s", err)
}
time.Sleep(wait)
}
if err != nil {
return errors.Wrap(err, "timed out with error")
}
return fmt.Errorf("timed out")
}