-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
209 lines (176 loc) · 5.69 KB
/
server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package testutil
import (
"context"
"fmt"
"net"
"sync"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/code-payments/code-server/pkg/grpc/headers"
"github.com/code-payments/code-server/pkg/grpc/protobuf/validation"
"github.com/code-payments/code-server/pkg/netutil"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/retry/backoff"
)
// Server provides a local gRPC server with basic interceptors that
// can be used for testing with no external dependencies.
type Server struct {
closeFunc sync.Once
sync.Mutex
serv bool
listener net.Listener
grpcServer *grpc.Server
clientConn *grpc.ClientConn
}
// NewServer creates a new Server.
func NewServer(opts ...ServerOption) (*grpc.ClientConn, *Server, error) {
port, err := netutil.GetAvailablePortForAddress("localhost")
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to find free port")
}
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to start listener")
}
o := serverOpts{
unaryServerInterceptors: []grpc.UnaryServerInterceptor{
headers.UnaryServerInterceptor(),
validation.UnaryServerInterceptor(),
},
streamServerInterceptors: []grpc.StreamServerInterceptor{
headers.StreamServerInterceptor(),
validation.StreamServerInterceptor(),
},
unaryClientInterceptors: []grpc.UnaryClientInterceptor{
validation.UnaryClientInterceptor(),
},
streamClientInterceptors: []grpc.StreamClientInterceptor{
validation.StreamClientInterceptor(),
},
}
for _, opt := range opts {
opt(&o)
}
// note: this is safe since we don't specify grpc.WithBlock()
conn, err := grpc.Dial(
fmt.Sprintf("localhost:%d", port),
grpc.WithInsecure(),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(o.unaryClientInterceptors...)),
grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(o.streamClientInterceptors...)),
)
if err != nil {
listener.Close()
return nil, nil, errors.Wrapf(err, "failed to create grpc.ClientConn")
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(o.unaryServerInterceptors...)),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(o.streamServerInterceptors...)),
)
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
return conn, &Server{
listener: listener,
grpcServer: grpcServer,
clientConn: conn,
}, nil
}
// RegisterService registers a gRPC service with Server.
func (s *Server) RegisterService(registerFunc func(s *grpc.Server)) {
registerFunc(s.grpcServer)
}
// Serve asynchronously starts the server, provided it has not been previously
// started or stopped. Callers should use stopFunc to stop the server in order
// to cleanup the underlying resources.
func (s *Server) Serve() (stopFunc func(), err error) {
// Start the server
err = func() error {
s.Lock()
defer s.Unlock()
if s.serv {
return nil
}
if s.grpcServer == nil {
return errors.Errorf("testserver already stopped")
}
stopFunc = func() {
s.closeFunc.Do(func() {
s.Lock()
defer s.Unlock()
s.grpcServer.Stop()
s.listener.Close()
s.grpcServer = nil
s.listener = nil
})
}
go func() {
s.Lock()
lis := s.listener
serv := s.grpcServer
s.Unlock()
if lis == nil || serv == nil {
return
}
err := serv.Serve(lis)
logrus.
StandardLogger().
WithField("type", "testutil/server").
WithError(err).
Debug("stopped")
stopFunc()
}()
s.serv = true
return nil
}()
if err != nil {
return nil, err
}
// Verify we can call a RPC
_, err = retry.Retry(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
healthClient := grpc_health_v1.NewHealthClient(s.clientConn)
_, err := healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
return err
}, retry.Limit(10), retry.Backoff(backoff.Constant(250*time.Millisecond), 250*time.Millisecond))
if err != nil {
return nil, errors.Wrap(err, "error executing sanity test rpc call")
}
return stopFunc, nil
}
type serverOpts struct {
unaryClientInterceptors []grpc.UnaryClientInterceptor
streamClientInterceptors []grpc.StreamClientInterceptor
unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor
}
// ServerOption configures the settings when creating a test server.
type ServerOption func(o *serverOpts)
// WithUnaryClientInterceptor adds a unary client interceptor to the test client.
func WithUnaryClientInterceptor(i grpc.UnaryClientInterceptor) ServerOption {
return func(o *serverOpts) {
o.unaryClientInterceptors = append(o.unaryClientInterceptors, i)
}
}
// WithStreamClientInterceptor adds a stream client interceptor to the test client.
func WithStreamClientInterceptor(i grpc.StreamClientInterceptor) ServerOption {
return func(o *serverOpts) {
o.streamClientInterceptors = append(o.streamClientInterceptors, i)
}
}
// WithUnaryServerInterceptor adds a unary server interceptor to the test client.
func WithUnaryServerInterceptor(i grpc.UnaryServerInterceptor) ServerOption {
return func(o *serverOpts) {
o.unaryServerInterceptors = append(o.unaryServerInterceptors, i)
}
}
// WithStreamServerInterceptor adds a stream server interceptor to the test client.
func WithStreamServerInterceptor(i grpc.StreamServerInterceptor) ServerOption {
return func(o *serverOpts) {
o.streamServerInterceptors = append(o.streamServerInterceptors, i)
}
}