Skip to content

Commit

Permalink
Make Status implement fmt.Stringer (#58)
Browse files Browse the repository at this point in the history
Debugging is easier if `Status` implements `fmt.Stringer`.

---------

Co-authored-by: Akshay Shah <akshay@akshayshah.org>
  • Loading branch information
VoyTechnology and akshayjshah authored Oct 3, 2023
1 parent 3c26b0d commit 3e1b33f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions grpchealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ const (
StatusNotServing Status = 2
)

// String representation of the status.
func (s Status) String() string {
switch s {
case StatusUnknown:
return "unknown"
case StatusServing:
return "serving"
case StatusNotServing:
return "not_serving"
}

return fmt.Sprintf("status_%d", s)
}

// NewHandler wraps the supplied Checker to build an HTTP handler for gRPC's
// health-checking API. It returns the path on which to mount the handler and
// the HTTP handler itself.
Expand Down
27 changes: 27 additions & 0 deletions grpchealth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,39 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/quick"

"connectrpc.com/connect"
healthv1 "connectrpc.com/grpchealth/internal/gen/go/connectext/grpc/health/v1"
)

func TestCode(t *testing.T) {
t.Parallel()

knownStatuses := map[Status]struct{}{
StatusUnknown: {},
StatusServing: {},
StatusNotServing: {},
}
check := func(s Status) bool {
got := s.String()
_, known := knownStatuses[s]
return known != strings.HasPrefix(got, "status_")
}
// always check named statuses
for status := range knownStatuses {
if !check(status) {
t.Fatalf("expected string representation of %q to be customized", status)
}
}
// probabilistically explore other statuses
if err := quick.Check(check, nil /* config */); err != nil {
t.Fatal(err)
}
}

func TestHealth(t *testing.T) {
const (
userFQN = "acme.user.v1.UserService"
Expand Down

0 comments on commit 3e1b33f

Please sign in to comment.