Skip to content

Commit 3e1b33f

Browse files
Make Status implement fmt.Stringer (#58)
Debugging is easier if `Status` implements `fmt.Stringer`. --------- Co-authored-by: Akshay Shah <akshay@akshayshah.org>
1 parent 3c26b0d commit 3e1b33f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

grpchealth.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ const (
5555
StatusNotServing Status = 2
5656
)
5757

58+
// String representation of the status.
59+
func (s Status) String() string {
60+
switch s {
61+
case StatusUnknown:
62+
return "unknown"
63+
case StatusServing:
64+
return "serving"
65+
case StatusNotServing:
66+
return "not_serving"
67+
}
68+
69+
return fmt.Sprintf("status_%d", s)
70+
}
71+
5872
// NewHandler wraps the supplied Checker to build an HTTP handler for gRPC's
5973
// health-checking API. It returns the path on which to mount the handler and
6074
// the HTTP handler itself.

grpchealth_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,39 @@ import (
1919
"errors"
2020
"net/http"
2121
"net/http/httptest"
22+
"strings"
2223
"testing"
24+
"testing/quick"
2325

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

30+
func TestCode(t *testing.T) {
31+
t.Parallel()
32+
33+
knownStatuses := map[Status]struct{}{
34+
StatusUnknown: {},
35+
StatusServing: {},
36+
StatusNotServing: {},
37+
}
38+
check := func(s Status) bool {
39+
got := s.String()
40+
_, known := knownStatuses[s]
41+
return known != strings.HasPrefix(got, "status_")
42+
}
43+
// always check named statuses
44+
for status := range knownStatuses {
45+
if !check(status) {
46+
t.Fatalf("expected string representation of %q to be customized", status)
47+
}
48+
}
49+
// probabilistically explore other statuses
50+
if err := quick.Check(check, nil /* config */); err != nil {
51+
t.Fatal(err)
52+
}
53+
}
54+
2855
func TestHealth(t *testing.T) {
2956
const (
3057
userFQN = "acme.user.v1.UserService"

0 commit comments

Comments
 (0)