open-consul/testutil/wait.go

67 lines
1.3 KiB
Go
Raw Normal View History

package testutil
import (
"fmt"
2014-05-16 22:49:47 +00:00
"testing"
"time"
"github.com/hashicorp/consul/consul/structs"
"github.com/pkg/errors"
)
type testFn func() (bool, error)
const (
baseWait = 1 * time.Millisecond
maxWait = 100 * time.Millisecond
)
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)
return nil
}
time.Sleep(wait)
wait *= 2
if wait > maxWait {
wait = maxWait
}
}
if err != nil {
return errors.Wrap(err, "timed out with error")
} else {
return fmt.Errorf("timed out")
}
}
2014-05-16 22:49:47 +00:00
type rpcFn func(string, interface{}, interface{}) error
func WaitForLeader(t *testing.T, rpc rpcFn, dc string) structs.IndexedNodes {
var out structs.IndexedNodes
if err := WaitForResult(func() (bool, error) {
// Ensure we have a leader and a node registration.
2014-06-11 01:04:24 +00:00
args := &structs.DCSpecificRequest{
Datacenter: dc,
}
if err := rpc("Catalog.ListNodes", args, &out); err != nil {
return false, fmt.Errorf("Catalog.ListNodes failed: %v", err)
}
if !out.QueryMeta.KnownLeader {
return false, fmt.Errorf("No leader")
}
if out.Index == 0 {
return false, fmt.Errorf("Consul index is 0")
}
return true, nil
}); err != nil {
t.Fatalf("failed to find leader: %v", err)
}
return out
}