open-nomad/testutil/wait.go

43 lines
729 B
Go
Raw Normal View History

2015-06-04 11:02:39 +00:00
package testutil
2015-06-04 11:26:16 +00:00
import (
"testing"
"time"
"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) {
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)
})
}