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 5, 2024
1 parent 0b185fb commit 0b2e166
Show file tree
Hide file tree
Showing 2 changed files with 86 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
80 changes: 80 additions & 0 deletions health/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 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 * time.Second) //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) {
checkPath := healthCheckService + healthCheckPath
healthCheckAged = 0
//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.Errorf("should pass health check after 5 seconds")
}
}

// 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 0b2e166

Please sign in to comment.