2015-06-04 11:02:39 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
import (
|
2016-01-20 20:00:20 +00:00
|
|
|
"os"
|
2015-06-04 11:26:16 +00:00
|
|
|
"time"
|
2015-09-07 01:07:05 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-07-25 21:21:59 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2015-06-04 11:26:16 +00:00
|
|
|
)
|
2015-06-04 11:02:39 +00:00
|
|
|
|
2016-01-20 20:00:20 +00:00
|
|
|
const (
|
|
|
|
// TravisRunEnv is an environment variable that is set if being run by
|
|
|
|
// Travis.
|
2016-02-06 08:35:55 +00:00
|
|
|
TravisRunEnv = "CI"
|
2016-01-20 20:00:20 +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) {
|
2016-02-04 22:19:27 +00:00
|
|
|
WaitForResultRetries(2000*TestMultiplier(), test, error)
|
2016-01-20 20:00:20 +00:00
|
|
|
}
|
2015-06-04 11:02:39 +00:00
|
|
|
|
2016-01-21 20:29:13 +00:00
|
|
|
func WaitForResultRetries(retries int64, test testFn, error errorFn) {
|
2015-06-04 11:02:39 +00:00
|
|
|
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
|
|
|
|
2017-04-07 00:05:09 +00:00
|
|
|
// AssertUntil asserts the test function passes throughout the given duration.
|
|
|
|
// Otherwise error is called on failure.
|
|
|
|
func AssertUntil(until time.Duration, test testFn, error errorFn) {
|
|
|
|
deadline := time.Now().Add(until)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
success, err := test()
|
|
|
|
if !success {
|
|
|
|
error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Sleep some arbitrary fraction of the deadline
|
|
|
|
time.Sleep(until / 30)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:00:20 +00:00
|
|
|
// TestMultiplier returns a multiplier for retries and waits given environment
|
|
|
|
// the tests are being run under.
|
2016-01-21 20:29:13 +00:00
|
|
|
func TestMultiplier() int64 {
|
2016-01-21 21:28:48 +00:00
|
|
|
if IsTravis() {
|
2016-01-20 20:00:20 +00:00
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:28:48 +00:00
|
|
|
func IsTravis() bool {
|
|
|
|
_, ok := os.LookupEnv(TravisRunEnv)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
type rpcFn func(string, interface{}, interface{}) error
|
|
|
|
|
2017-07-25 21:21:59 +00:00
|
|
|
func WaitForLeader(t testing.T, rpc rpcFn) {
|
2015-06-04 11:26:16 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|