Run the waited-for function before sleeping, and ramp up the sleep exponentially.

This commit is contained in:
James Phillips 2017-01-26 17:11:16 -08:00
parent 0a7aa91607
commit dd9ecbf440
No known key found for this signature in database
GPG Key ID: 77183E682AC5FC11
2 changed files with 15 additions and 7 deletions

View File

@ -310,7 +310,6 @@ func (s *TestServer) waitForLeader() {
// Ensure we have a leader and a node registration. // Ensure we have a leader and a node registration.
if leader := resp.Header.Get("X-Consul-KnownLeader"); leader != "true" { if leader := resp.Header.Get("X-Consul-KnownLeader"); leader != "true" {
fmt.Println(leader)
return false, fmt.Errorf("Consul leader status: %#v", leader) return false, fmt.Errorf("Consul leader status: %#v", leader)
} }
index, err = strconv.ParseInt(resp.Header.Get("X-Consul-Index"), 10, 64) index, err = strconv.ParseInt(resp.Header.Get("X-Consul-Index"), 10, 64)

View File

@ -10,19 +10,28 @@ import (
type testFn func() (bool, error) type testFn func() (bool, error)
type errorFn func(error) type errorFn func(error)
func WaitForResult(test testFn, error errorFn) { const (
for retries := 100; retries > 0; retries-- { baseWait = 1 * time.Millisecond
time.Sleep(100 * time.Millisecond) maxWait = 100 * time.Millisecond
)
success, err := test() func WaitForResult(try testFn, fail errorFn) {
var err error
wait := baseWait
for retries := 100; retries > 0; retries-- {
var success bool
success, err = try()
if success { if success {
return return
} }
if retries == 0 { time.Sleep(wait)
error(err) wait *= 2
if wait > maxWait {
wait = maxWait
} }
} }
fail(err)
} }
type rpcFn func(string, interface{}, interface{}) error type rpcFn func(string, interface{}, interface{}) error