Skip to content

Commit

Permalink
include tests to ensure that double registration errors are enforced
Browse files Browse the repository at this point in the history
  • Loading branch information
puellanivis committed Jun 8, 2018
1 parent 68bdec5 commit 90cb3ad
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,63 @@ const (
checkErr = "Failed during RabbitMQ health check"
)

func TestRegisterWithNoName(t *testing.T) {
err := Register(Config{
Name: "",
Check: func() error {
return nil
},
})
if err == nil {
t.Error("health check registration with empty name should return an error, but did not get one")
}
}

func TestDoubleRegister(t *testing.T) {
Reset()
if len(checkMap) != 0 {
t.Errorf("checks lenght differes from zero: got %d", len(checkMap))
}

healthcheckName := "succeed"

conf := Config{
Name: healthcheckName,
Check: func() error {
return nil
},
}

err := Register(conf)
if err != nil {
// If the first registration failed, then further testing makes no sense.
t.Fatal("the first registration of a health check should not return an error, but got: ", err)
}

err = Register(conf)
if err == nil {
t.Error("the second registration of a health check should return an error, but did not")
}

err = Register(Config{
Name: healthcheckName,
Check: func() error {
// this function is non-trival solely to ensure that the compiler does not get optimized.
if len(checkMap) > 0 {
return nil
}

return errors.New("no health checks registered")
},
})
if err == nil {
t.Error("health check registration with same name different details should still return an error, but did not")
}
}

func TestHealthHandler(t *testing.T) {
Reset()

res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/status", nil)
if err != nil {
Expand Down

0 comments on commit 90cb3ad

Please sign in to comment.