Skip to content

Commit

Permalink
health check: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bpeng committed Nov 8, 2024
1 parent 5628e15 commit 56e45ca
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
7 changes: 6 additions & 1 deletion health/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"
)

// Check calls the given service endpoint with a given context and timeout.
// An error will be returned if the connection fails, or the response status
// is not 200 (i.e. StatusOK). A successful check will return only the check message reply.
func Check(ctx context.Context, servicePath string, timeout time.Duration) ([]byte, error) {
req, err := url.Parse("http://" + servicePath)
checkUrl := servicePath
if !strings.HasPrefix(checkUrl, "http") {
checkUrl = "http://" + servicePath
}
req, err := url.Parse(checkUrl)
if err != nil {
return nil, err
}
Expand Down
79 changes: 79 additions & 0 deletions health/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package health_test

import (
"context"
"log"
"testing"
"time"

"github.com/GeoNet/kit/health"
)

var (
healthCheckAged = 5 * time.Second //need to have a good heartbeat within this time
healthCheckStartup = 5 * time.Second //ignore heartbeat messages for this time after starting
healthCheckTimeout = 30 * time.Second //health check timeout
healthCheckService = ":7777" //end point to listen to for SOH checks
healthCheckPath = "/soh"
)

func TestExistingSoh(t *testing.T) {
checkPath := "https://api.geonet.org.nz/soh"
if err := healthCheck(checkPath); err != nil {
t.Error("should pass health check at start ")
}
}

func TestHealth(t *testing.T) {
checkPath := healthCheckService + healthCheckPath
//1. should fail at start
if err := healthCheck(checkPath); err == nil {
t.Error("should fail health check at start ")
}
//2. start the process
health := health.New(healthCheckService, healthCheckAged, healthCheckStartup)
health.Ok()
time.Sleep(1 * time.Millisecond) //let the service to start
if err := healthCheck(checkPath); err != nil {
t.Error("should pass health check after started ")
}
//3. test after healthCheckAged
time.Sleep(healthCheckAged) //wait for the healthCheckAged
if err := healthCheck(checkPath); err == nil {
t.Errorf("should fail health check after %v", healthCheckAged)
}

//4. test after heartbeat
health.Ok()
if err := healthCheck(checkPath); err != nil {
t.Error("should pass health check after heartbeat ")
}
}

func TestHealthWithoutAgeCheck(t *testing.T) {
healthCheckAged = 0 * time.Second
healthCheckService = ":7778"
checkPath := healthCheckService + healthCheckPath
//1. start the process
health := health.New(healthCheckService, healthCheckAged, healthCheckStartup)
health.Ok()
time.Sleep(1 * time.Millisecond) //let the service to start
if err := healthCheck(checkPath); err != nil {
t.Error("should pass health check after started ")
}

//2. test after healthCheckAged
time.Sleep(5 * time.Second) //wait for 5 seconds
if err := healthCheck(checkPath); err != nil {
t.Error("should pass health check after 5 seconds", err)
}
}

// check health by calling the http soh endpoint
func healthCheck(sohPath string) error {
ctx, cancel := context.WithTimeout(context.Background(), healthCheckTimeout)
defer cancel()
msg, err := health.Check(ctx, sohPath, healthCheckTimeout)
log.Printf("status: %s", string(msg))
return err
}

0 comments on commit 56e45ca

Please sign in to comment.