consul/testutil/wait.go

45 lines
838 B
Go
Raw Normal View History

package testutil
import (
"github.com/hashicorp/consul/consul/structs"
2014-05-16 22:49:47 +00:00
"testing"
"time"
)
type testFn func() (bool, error)
type errorFn func(error)
func WaitForResult(test testFn, error errorFn) {
2014-05-09 01:00:43 +00:00
retries := 1000
for retries > 0 {
2014-05-09 01:00:43 +00:00
time.Sleep(10 * time.Millisecond)
retries--
success, err := test()
if success {
return
}
if retries == 0 {
error(err)
}
}
}
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
WaitForResult(func() (bool, error) {
args := &structs.RegisterRequest{
Datacenter: dc,
}
err := rpc("Catalog.ListNodes", args, &out)
return out.QueryMeta.KnownLeader && out.Index > 0, err
}, func(err error) {
t.Fatalf("failed to find leader: %v", err)
})
return out
}