api: improve test reliability

This commit is contained in:
Ryan Uber 2015-01-06 16:48:54 -08:00
parent 0c6c06cb0c
commit 6c4ff58d2a
3 changed files with 40 additions and 56 deletions

23
api/.gitignore vendored
View File

@ -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

View File

@ -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,

View File

@ -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)
})
}