2014-05-07 00:48:25 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2014-05-07 09:43:42 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-05-16 22:49:47 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-05-07 00:48:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2014-05-07 00:48:25 +00:00
|
|
|
|
|
|
|
for retries > 0 {
|
2014-05-09 01:00:43 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2014-05-07 00:48:25 +00:00
|
|
|
retries--
|
|
|
|
|
|
|
|
success, err := test()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if retries == 0 {
|
|
|
|
error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-05-16 22:49:47 +00:00
|
|
|
type rpcFn func(string, interface{}, interface{}) error
|
2014-05-07 09:43:42 +00:00
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
func WaitForLeader(t *testing.T, rpc rpcFn, dc string) structs.IndexedNodes {
|
2014-05-07 11:53:29 +00:00
|
|
|
var out structs.IndexedNodes
|
2014-05-07 09:43:42 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
2014-05-07 21:41:14 +00:00
|
|
|
args := &structs.RegisterRequest{
|
2014-05-08 23:17:35 +00:00
|
|
|
Datacenter: dc,
|
2014-05-07 21:41:14 +00:00
|
|
|
}
|
2014-05-07 09:43:42 +00:00
|
|
|
err := rpc("Catalog.ListNodes", args, &out)
|
2014-05-26 20:26:42 +00:00
|
|
|
return out.QueryMeta.KnownLeader && out.Index > 0, err
|
2014-05-07 09:43:42 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
2014-05-07 11:53:29 +00:00
|
|
|
return out
|
2014-05-07 09:43:42 +00:00
|
|
|
}
|