Skip to content

Commit

Permalink
Merge pull request #6 from hellofresh/feature/reset
Browse files Browse the repository at this point in the history
Added checks reset function
  • Loading branch information
vgarvardt authored Jul 3, 2017
2 parents ddba0b2 + c5f77ab commit 9f26f20
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import (
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/hellofresh/health-go"
"github.com/pressly/chi"
)

func main() {
Expand Down
12 changes: 11 additions & 1 deletion health.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
statusOK = "OK"
statusPartiallyAvailable = "Partially Available"
statusUnavailable = "Unavailable"

failureTimeout = "Timeout during health check"
)

// Config carries the parameters to run the check.
Expand Down Expand Up @@ -58,6 +60,14 @@ func Register(c Config) {
checks = append(checks, c)
}

// Reset unregisters all previously set check configs
func Reset() {
mu.Lock()
defer mu.Unlock()

checks = []Config{}
}

// Handler returns an HTTP handler (http.HandlerFunc).
func Handler() http.Handler {
return http.HandlerFunc(HandlerFunc)
Expand All @@ -79,7 +89,7 @@ func HandlerFunc(w http.ResponseWriter, r *http.Request) {
for {
select {
case <-time.After(c.Timeout):
failures[c.Name] = "Timeout during health check"
failures[c.Name] = failureTimeout
setStatus(&status, c.SkipOnErr)
break loop
case res := <-resChan:
Expand Down
24 changes: 22 additions & 2 deletions health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)

const (
checkErr = "Failed duriung rabbitmq health check"
checkErr = "Failed during RabbitMQ health check"
)

func TestHealthHandler(t *testing.T) {
Expand All @@ -30,6 +31,16 @@ func TestHealthHandler(t *testing.T) {
Check: func() error { return nil },
})

Register(Config{
Name: "snail-service",
SkipOnErr: true,
Timeout: time.Second * 1,
Check: func() error {
time.Sleep(time.Second * 2)
return nil
},
})

h := http.Handler(Handler())
h.ServeHTTP(res, req)

Expand Down Expand Up @@ -57,6 +68,15 @@ func TestHealthHandler(t *testing.T) {
}

if f["rabbitmq"] != checkErr {
t.Errorf("body returned wrong status: got %s want %s", failure, checkErr)
t.Errorf("body returned wrong status for rabbitmq: got %s want %s", failure, checkErr)
}

if f["snail-service"] != failureTimeout {
t.Errorf("body returned wrong status for snail-service: got %s want %s", failure, failureTimeout)
}

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

0 comments on commit 9f26f20

Please sign in to comment.