@@ -13,6 +13,10 @@ import (
13
13
"google.golang.org/grpc/codes"
14
14
"google.golang.org/grpc/metadata"
15
15
"google.golang.org/grpc/status"
16
+ "google.golang.org/grpc/health"
17
+ healthpb "google.golang.org/grpc/health/grpc_health_v1"
18
+ "github.com/Semior001/groxy/pkg/grpcx/grpctest"
19
+ "google.golang.org/grpc/credentials/insecure"
16
20
)
17
21
18
22
func TestAppInfo (t * testing.T ) {
@@ -38,7 +42,7 @@ func TestAppInfo(t *testing.T) {
38
42
func TestRecoverer (t * testing.T ) {
39
43
bts := bytes .NewBuffer (nil )
40
44
slog .SetDefault (slog .New (slog .NewTextHandler (bts , & slog.HandlerOptions {})))
41
- mw := Recoverer ()(func (_ any , _ grpc.ServerStream ) error { panic ("test" ) })
45
+ mw := Recoverer ("{groxy} panic" )(func (_ any , _ grpc.ServerStream ) error { panic ("test" ) })
42
46
var err error
43
47
require .NotPanics (t , func () {
44
48
err = mw (nil , & mocks.ServerStreamMock {
@@ -75,3 +79,60 @@ func TestChain(t *testing.T) {
75
79
require .NoError (t , h (nil , nil ))
76
80
assert .Equal (t , []string {"mw1" , "mw2" , "mw3" }, calls )
77
81
}
82
+
83
+ func TestHealth (t * testing.T ) {
84
+ prepare := func () (* health.Server , healthpb.HealthClient ) {
85
+ h := health .NewServer ()
86
+ h .SetServingStatus ("" , healthpb .HealthCheckResponse_SERVING )
87
+
88
+ srv := grpc .NewServer (grpc .UnknownServiceHandler (Health (h )(func (_ any , _ grpc.ServerStream ) error {
89
+ return status .Error (codes .Internal , "must not be called" )
90
+ })))
91
+
92
+ addr := grpctest .StartServer (t , srv )
93
+
94
+ conn , err := grpc .Dial (addr , grpc .WithTransportCredentials (insecure .NewCredentials ()))
95
+ require .NoError (t , err )
96
+
97
+ cl := healthpb .NewHealthClient (conn )
98
+
99
+ return h , cl
100
+ }
101
+
102
+ t .Run ("unary" , func (t * testing.T ) {
103
+ h , cl := prepare ()
104
+
105
+ resp , err := cl .Check (context .Background (), & healthpb.HealthCheckRequest {})
106
+ require .NoError (t , err )
107
+
108
+ assert .Equal (t , healthpb .HealthCheckResponse_SERVING , resp .Status )
109
+
110
+ h .SetServingStatus ("" , healthpb .HealthCheckResponse_NOT_SERVING )
111
+
112
+ resp , err = cl .Check (context .Background (), & healthpb.HealthCheckRequest {})
113
+ require .NoError (t , err )
114
+
115
+ assert .Equal (t , healthpb .HealthCheckResponse_NOT_SERVING , resp .Status )
116
+ })
117
+
118
+ t .Run ("watch" , func (t * testing.T ) {
119
+ h , cl := prepare ()
120
+
121
+ stream , err := cl .Watch (context .Background (), & healthpb.HealthCheckRequest {})
122
+ require .NoError (t , err )
123
+ defer stream .CloseSend ()
124
+
125
+ resp , err := stream .Recv ()
126
+ require .NoError (t , err )
127
+
128
+ assert .Equal (t , healthpb .HealthCheckResponse_SERVING , resp .Status )
129
+
130
+ h .SetServingStatus ("" , healthpb .HealthCheckResponse_NOT_SERVING )
131
+
132
+ resp , err = stream .Recv ()
133
+ require .NoError (t , err )
134
+
135
+ assert .Equal (t , healthpb .HealthCheckResponse_NOT_SERVING , resp .Status )
136
+ })
137
+
138
+ }
0 commit comments