-
Notifications
You must be signed in to change notification settings - Fork 35
/
grpc_server_util_test.go
53 lines (41 loc) · 1.13 KB
/
grpc_server_util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"net"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
const _grpcService = "yab-grpc-test"
type grpcServer struct {
ln net.Listener
server *grpc.Server
healthHandler *health.Server
}
func newGRPCServer(t *testing.T) *grpcServer {
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "Failed to create TCP listener")
server := grpc.NewServer()
healthHandler := health.NewServer()
grpc_health_v1.RegisterHealthServer(server, healthHandler)
reflection.Register(server)
s := &grpcServer{ln, server, healthHandler}
s.SetHealth(true /* serving */)
go server.Serve(ln)
return s
}
func (s *grpcServer) HostPort() string {
return s.ln.Addr().String()
}
func (s *grpcServer) SetHealth(serving bool) {
status := grpc_health_v1.HealthCheckResponse_NOT_SERVING
if serving {
status = grpc_health_v1.HealthCheckResponse_SERVING
}
s.healthHandler.SetServingStatus(_grpcService, status)
}
func (s *grpcServer) Stop() {
s.server.Stop()
}