Create AssertUntil helper func

This commit is contained in:
Michael Schurter
2017-04-06 17:05:09 -07:00
parent 985bde83d6
commit df30a110fb
2 changed files with 39 additions and 16 deletions

View File

@@ -414,14 +414,18 @@ func TestClient_MixedTLS(t *testing.T) {
QueryOptions: structs.QueryOptions{Region: "global"},
}
var out structs.SingleNodeResponse
deadline := time.Now().Add(100 * time.Millisecond)
for time.Now().Before(deadline) {
err := c1.RPC("Node.GetNode", &req, &out)
if err == nil {
t.Fatalf("client RPC succeeded when it should have failed:\n%+v", out)
}
time.Sleep(3 * time.Millisecond)
}
testutil.AssertUntil(100*time.Millisecond,
func() (bool, error) {
err := c1.RPC("Node.GetNode", &req, &out)
if err == nil {
return false, fmt.Errorf("client RPC succeeded when it should have failed:\n%+v", out)
}
return true, nil
},
func(err error) {
t.Fatalf(err.Error())
},
)
}
// TestClient_BadTLS asserts that when a client and server are running with TLS
@@ -467,14 +471,18 @@ func TestClient_BadTLS(t *testing.T) {
QueryOptions: structs.QueryOptions{Region: "global"},
}
var out structs.SingleNodeResponse
deadline := time.Now().Add(100 * time.Millisecond)
for time.Now().Before(deadline) {
err := c1.RPC("Node.GetNode", &req, &out)
if err == nil {
t.Fatalf("client RPC succeeded when it should have failed:\n%+v", out)
}
time.Sleep(3 * time.Millisecond)
}
testutil.AssertUntil(100*time.Millisecond,
func() (bool, error) {
err := c1.RPC("Node.GetNode", &req, &out)
if err == nil {
return false, fmt.Errorf("client RPC succeeded when it should have failed:\n%+v", out)
}
return true, nil
},
func(err error) {
t.Fatalf(err.Error())
},
)
}
func TestClient_Register(t *testing.T) {

View File

@@ -37,6 +37,21 @@ func WaitForResultRetries(retries int64, test testFn, error errorFn) {
}
}
// 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)
}
}
// TestMultiplier returns a multiplier for retries and waits given environment
// the tests are being run under.
func TestMultiplier() int64 {