2014-05-07 00:48:25 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2014-05-07 09:43:42 +00:00
|
|
|
"testing"
|
|
|
|
"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) {
|
2014-05-07 09:43:42 +00:00
|
|
|
retries := 100 // 5 seconds timeout
|
2014-05-07 00:48:25 +00:00
|
|
|
|
|
|
|
for retries > 0 {
|
2014-05-07 09:43:42 +00:00
|
|
|
time.Sleep(50 * 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-07 11:53:29 +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)
|
|
|
|
return out.QueryMeta.KnownLeader, err
|
|
|
|
}, 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
|
|
|
}
|