2014-05-07 00:48:25 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2014-05-16 22:49:47 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-01-26 05:13:34 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-05-07 00:48:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testFn func() (bool, error)
|
|
|
|
type errorFn func(error)
|
|
|
|
|
|
|
|
func WaitForResult(test testFn, error errorFn) {
|
2017-01-27 00:55:49 +00:00
|
|
|
for retries := 100; retries > 0; retries-- {
|
2017-01-26 05:13:34 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2014-05-07 00:48:25 +00:00
|
|
|
|
|
|
|
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-06-11 01:04:24 +00:00
|
|
|
args := &structs.DCSpecificRequest{
|
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
|
|
|
}
|