2015-06-04 11:02:39 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-09-07 01:07:05 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-06-04 11:26:16 +00:00
|
|
|
)
|
2015-06-04 11:02:39 +00:00
|
|
|
|
|
|
|
type testFn func() (bool, error)
|
|
|
|
type errorFn func(error)
|
|
|
|
|
|
|
|
func WaitForResult(test testFn, error errorFn) {
|
|
|
|
retries := 1000
|
|
|
|
|
|
|
|
for retries > 0 {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
retries--
|
|
|
|
|
|
|
|
success, err := test()
|
|
|
|
if success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if retries == 0 {
|
|
|
|
error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 11:26:16 +00:00
|
|
|
|
|
|
|
type rpcFn func(string, interface{}, interface{}) error
|
|
|
|
|
|
|
|
func WaitForLeader(t *testing.T, rpc rpcFn) {
|
|
|
|
WaitForResult(func() (bool, error) {
|
2015-09-07 01:07:05 +00:00
|
|
|
args := &structs.GenericRequest{}
|
2015-06-04 11:26:16 +00:00
|
|
|
var leader string
|
|
|
|
err := rpc("Status.Leader", args, &leader)
|
|
|
|
return leader != "", err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to find leader: %v", err)
|
|
|
|
})
|
|
|
|
}
|