From 9beeb6c81b6005eaf123c432851fcf2978afb2cc Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Tue, 6 Jan 2015 16:18:50 -0800 Subject: [PATCH] api: use WaitForResult() to improve health tests --- api/health_test.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/api/health_test.go b/api/health_test.go index 03c66a3d9..088d431ff 100644 --- a/api/health_test.go +++ b/api/health_test.go @@ -1,8 +1,10 @@ package api import ( + "fmt" "testing" - "time" + + "github.com/hashicorp/consul/testutil" ) func TestHealth_Node(t *testing.T) { @@ -50,20 +52,21 @@ func TestHealth_Checks(t *testing.T) { } defer agent.ServiceDeregister("foo") - // Wait for the register... - time.Sleep(20 * time.Millisecond) - - checks, meta, err := health.Checks("foo", nil) - if err != nil { - t.Fatalf("err: %v", err) - } - - if meta.LastIndex == 0 { - t.Fatalf("bad: %v", meta) - } - if len(checks) == 0 { - t.Fatalf("Bad: %v", checks) - } + testutil.WaitForResult(func() (bool, error) { + checks, meta, err := health.Checks("foo", nil) + if err != nil { + return false, err + } + if meta.LastIndex == 0 { + return false, fmt.Errorf("bad: %v", meta) + } + if len(checks) == 0 { + return false, fmt.Errorf("Bad: %v", checks) + } + return true, nil + }, func(err error) { + t.Fatalf("err: %s", err) + }) } func TestHealth_Service(t *testing.T) {