From e108162020c02366e622adefd32b70d5f001e2a5 Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Tue, 6 Jan 2015 16:48:54 -0800 Subject: [PATCH] api: improve test reliability --- api/.gitignore | 23 -------------------- api/api_test.go | 19 ++++++++-------- api/health_test.go | 54 ++++++++++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 56 deletions(-) delete mode 100644 api/.gitignore diff --git a/api/.gitignore b/api/.gitignore deleted file mode 100644 index 836562412..000000000 --- a/api/.gitignore +++ /dev/null @@ -1,23 +0,0 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test diff --git a/api/api_test.go b/api/api_test.go index d308e0038..af9a02ac8 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -10,6 +10,8 @@ import ( "strings" "testing" "time" + + "github.com/hashicorp/consul/testutil" ) var consulConfig = `{ @@ -78,25 +80,22 @@ func newTestServer(t *testing.T) *testServer { // Allow the server some time to start, and verify we have a leader. client := new(http.Client) - for i := 0; ; i++ { - if i >= 20 { - t.Fatal("Server failed to start") - } - time.Sleep(100 * time.Millisecond) - + testutil.WaitForResult(func() (bool, error) { resp, err := client.Get("http://127.0.0.1:18800/v1/status/leader") if err != nil { - continue + return false, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil || !strings.Contains(string(body), "18000") { - continue + return false, fmt.Errorf("No leader") } - break - } + return true, nil + }, func(err error) { + t.Fatalf("err: %s", err) + }) return &testServer{ pid: cmd.Process.Pid, diff --git a/api/health_test.go b/api/health_test.go index 088d431ff..9a00645d1 100644 --- a/api/health_test.go +++ b/api/health_test.go @@ -75,18 +75,22 @@ func TestHealth_Service(t *testing.T) { health := c.Health() - // consul service should always exist... - checks, meta, err := health.Service("consul", "", true, 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) { + // consul service should always exist... + checks, meta, err := health.Service("consul", "", true, 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_State(t *testing.T) { @@ -95,15 +99,19 @@ func TestHealth_State(t *testing.T) { health := c.Health() - checks, meta, err := health.State("any", 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.State("any", 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) + }) }