-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
335 lines (250 loc) · 6.9 KB
/
server_test.go
File metadata and controls
335 lines (250 loc) · 6.9 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package grpctest_test
import (
"context"
"errors"
"net"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/tomasbasham/grpctest"
echopb "github.com/tomasbasham/grpctest/testdata/echo/v1"
)
func TestServer_Connectivity(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
conn, err := s.ClientConn()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "connectivity test")
}
func TestServer_Lifecycle(t *testing.T) {
t.Parallel()
t.Run("serve is idempotent", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
// First serve.
s.Serve()
// Must be safe to call multiple times.
s.Serve()
conn, err := s.ClientConn()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "serve twice")
})
t.Run("close shuts down connections", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
conn, err := s.ClientConn()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "before close")
s.Close()
req := &echopb.EchoRequest{
Message: "after close",
}
client := echopb.NewEchoServiceClient(conn)
_, err = client.Echo(context.Background(), req)
if err == nil {
t.Fatal("expected RPC to fail after Close(), but it succeeded")
}
})
}
func TestServerWithCustomOptions(t *testing.T) {
t.Parallel()
var interceptorCalled bool
interceptor := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
interceptorCalled = true
return handler(ctx, req)
}
s := grpctest.NewServer(
grpc.MaxRecvMsgSize(1024*1024*10),
grpc.UnaryInterceptor(interceptor),
)
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
conn, err := s.ClientConn()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "custom options")
if !interceptorCalled {
t.Error("interceptor was not called")
}
}
func TestClientConnOverridesCustomDialer(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
badDialer := grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return nil, errors.New("this dialer must not be used")
})
conn, err := s.ClientConn(
badDialer,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "dialer overridden")
}
func TestClientConnWithCustomInterceptor(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
var clientInterceptorCalled bool
clientInterceptor := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
clientInterceptorCalled = true
return invoker(ctx, method, req, reply, cc, opts...)
}
conn, err := s.ClientConn(grpc.WithUnaryInterceptor(clientInterceptor))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "client interceptor")
if !clientInterceptorCalled {
t.Error("client interceptor was not called")
}
}
func TestClientConnContext(t *testing.T) {
t.Parallel()
t.Run("succeeds with sufficient timeout", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := s.ClientConnContext(ctx)
if err != nil {
t.Fatalf("ClientConnContext failed: %v", err)
}
mustEcho(t, conn, "context success")
})
t.Run("respects context cancellation", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
// Ensure context is cancelled.
time.Sleep(10 * time.Millisecond)
_, err := s.ClientConnContext(ctx)
if err == nil {
t.Fatal("expected error due to cancelled context, got nil")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("expected context.DeadlineExceeded, got: %v", err)
}
})
}
func TestErr(t *testing.T) {
t.Parallel()
t.Run("returns nil on graceful shutdown", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
errCh := make(chan error, 1)
go func() {
errCh <- s.Err()
}()
s.Close()
select {
case err := <-errCh:
if err != nil && err.Error() != grpc.ErrServerStopped.Error() {
t.Errorf("unexpected error: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("Err() did not return after Close()")
}
})
t.Run("blocks until server stops", func(t *testing.T) {
t.Parallel()
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
errCh := make(chan error, 1)
returned := make(chan struct{})
go func() {
errCh <- s.Err()
close(returned)
}()
// Verify Err() hasn't returned yet
select {
case <-returned:
t.Fatal("Err() returned before Close()")
case <-time.After(100 * time.Millisecond):
// Expected behaviour
}
s.Close()
// Now Err() should return
select {
case <-returned:
// Expected
case <-time.After(5 * time.Second):
t.Fatal("Err() did not return after Close()")
}
})
}
// This test affects global state, so it's isolated and cannot be run in
// parallel.
func TestSetBufferSize(t *testing.T) {
originalSize := 1 << 20 // 1 MiB default
defer func() {
// Reset to original size.
grpctest.SetBufferSize(originalSize)
}()
grpctest.SetBufferSize(2 << 20) // 2 MiB
s := grpctest.NewServer()
s.CloseOnCleanup(t)
echopb.RegisterEchoServiceServer(s, &echoServer{})
s.Serve()
conn, err := s.ClientConn()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
mustEcho(t, conn, "custom buffer size")
}
type echoServer struct {
echopb.UnimplementedEchoServiceServer
}
func (s *echoServer) Echo(ctx context.Context, in *echopb.EchoRequest) (*echopb.EchoResponse, error) {
return &echopb.EchoResponse{Message: in.GetMessage()}, nil
}
func mustEcho(t *testing.T, conn *grpc.ClientConn, msg string) {
t.Helper()
req := &echopb.EchoRequest{
Message: msg,
}
client := echopb.NewEchoServiceClient(conn)
resp, err := client.Echo(context.Background(), req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got := resp.GetMessage()
if got != msg {
t.Errorf("mismatch:\n got: %q\n want: %q", got, msg)
}
}