-
Notifications
You must be signed in to change notification settings - Fork 444
/
testgrpcservice.go
112 lines (94 loc) · 2.99 KB
/
testgrpcservice.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package testgrpcservice
import (
"context"
"errors"
"net"
"strconv"
"time"
"github.com/onsi/ginkgo/v2"
glootest "github.com/solo-io/gloo/test/v1helpers/test_grpc_service/glootest/protos"
"github.com/solo-io/go-utils/healthchecker"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
// For reflection to work, this uses the golang/proto plugin. To install it, run this command:
// go get -u github.com/golang/protobuf/protoc-gen-go
// In the unlikely event that you need to re-generate this proto, open this directory in a terminal
// and run the following commands:
// mkdir -p glootest
// mkdir -p descriptors
// cd /tmp/
// git clone https://github.com/protocolbuffers/protobuf
// git clone http://github.com/googleapis/googleapis
// export PROTOBUF_HOME=$PWD/protobuf/src
// export GOOGLE_PROTOS_HOME=$PWD/googleapis
// cd -
// protoc -I. -I${GOOGLE_PROTOS_HOME} -I${PROTOBUF_HOME} --include_source_info --include_imports --go_out=plugins=grpc:glootest --descriptor_set_out=descriptors/proto.pb protos/glootest.proto
func RunServer(ctx context.Context) *TestGRPCServer {
lis, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
grpcServer := grpc.NewServer()
reflection.Register(grpcServer)
srv := newServer()
hc := healthchecker.NewGrpc("TestService", health.NewServer(), false, healthpb.HealthCheckResponse_SERVING)
healthpb.RegisterHealthServer(grpcServer, hc.GetServer())
glootest.RegisterTestServiceServer(grpcServer, srv)
go func() {
defer ginkgo.GinkgoRecover()
_ = grpcServer.Serve(lis)
}()
go func() {
defer ginkgo.GinkgoRecover()
<-ctx.Done()
grpcServer.Stop()
}()
time.Sleep(time.Millisecond)
addr := lis.Addr().String()
_, portstr, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
port, err := strconv.Atoi(portstr)
if err != nil {
panic(err)
}
srv.Port = uint32(port)
srv.HealthChecker = hc
return srv
}
func newServer() *TestGRPCServer {
return &TestGRPCServer{
C: make(chan *glootest.TestRequest),
}
}
type TestGRPCServer struct {
C chan *glootest.TestRequest
Port uint32
HealthChecker healthchecker.HealthChecker
glootest.UnimplementedTestServiceServer
}
// Returns a list of all shelves in the bookstore.
func (s *TestGRPCServer) TestMethod(_ context.Context, req *glootest.TestRequest) (*glootest.TestResponse, error) {
if req == nil {
return &glootest.TestResponse{Str: "cannot be nil"}, nil
//return nil, errors.New("cannot be nil")
}
go func() {
s.C <- req
}()
return &glootest.TestResponse{Str: req.GetStr()}, nil
}
// Returns a list of all shelves in the bookstore.
func (s *TestGRPCServer) TestParameterMethod(_ context.Context, req *glootest.TestRequest) (*glootest.TestResponse, error) {
if req == nil {
return nil, errors.New("cannot be nil")
}
go func() {
s.C <- &glootest.TestRequest{Str: req.GetStr()}
}()
return &glootest.TestResponse{Str: req.GetStr()}, nil
}