Skip to content
This repository has been archived by the owner on Feb 28, 2019. It is now read-only.

Commit

Permalink
clean up health check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Skelcy committed Nov 28, 2017
1 parent 5dc4c6a commit 2df1160
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
7 changes: 1 addition & 6 deletions health/service.go
Expand Up @@ -72,15 +72,10 @@ func hostName() string {
func m3ctlHealthCheck(w http.ResponseWriter, r *http.Request) {
start := time.Now()
host := hostName()
h := healthCheckResult{Host: host, Timestamp: start}
status := healthCheck()
h := healthCheckResult{Host: host, Timestamp: start, Status: status}
h.ResponseTime = time.Since(start)

h.Status = status
if status != ok {
w.WriteHeader(http.StatusInternalServerError)
}

body, err := json.Marshal(h)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
18 changes: 10 additions & 8 deletions health/service_test.go
Expand Up @@ -27,6 +27,8 @@ import (
"os"
"testing"

"github.com/gorilla/mux"
"github.com/m3db/m3x/instrument"
"github.com/stretchr/testify/require"
)

Expand All @@ -42,18 +44,18 @@ func TestHealthCheck(t *testing.T) {
rr := httptest.NewRecorder()
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req := http.Request{Method: "GET", RequestURI: "/health"}
req, err := http.NewRequest("GET", "/health", nil)
require.NoError(t, err)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
m3ctlHealthCheck(w, r)
})
opts := instrument.NewOptions()
service := NewService(opts)
mux := mux.NewRouter().PathPrefix(service.URLPrefix()).Subrouter()
service.RegisterHandlers(mux)

// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, &req)
mux.ServeHTTP(rr, req)

rawResult := make([]byte, rr.Body.Len())
_, err := rr.Body.Read(rawResult)
_, err = rr.Body.Read(rawResult)
require.NoError(t, err, "Encountered error parsing response")

var actualResult healthCheckResult
Expand Down

0 comments on commit 2df1160

Please sign in to comment.