-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathserver_test.go
328 lines (287 loc) · 10.9 KB
/
server_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
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
/*
*
* Copyright 2020 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package test
import (
"context"
"io"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/stubserver"
"google.golang.org/grpc/status"
testgrpc "google.golang.org/grpc/interop/grpc_testing"
testpb "google.golang.org/grpc/interop/grpc_testing"
)
type ctxKey string
// TestServerReturningContextError verifies that if a context error is returned
// by the service handler, the status will have the correct status code, not
// Unknown.
func (s) TestServerReturningContextError(t *testing.T) {
ss := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return nil, context.DeadlineExceeded
},
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
return context.DeadlineExceeded
},
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
_, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
if s, ok := status.FromError(err); !ok || s.Code() != codes.DeadlineExceeded {
t.Fatalf("ss.Client.EmptyCall() got error %v; want <status with Code()=DeadlineExceeded>", err)
}
stream, err := ss.Client.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("unexpected error starting the stream: %v", err)
}
_, err = stream.Recv()
if s, ok := status.FromError(err); !ok || s.Code() != codes.DeadlineExceeded {
t.Fatalf("ss.Client.FullDuplexCall().Recv() got error %v; want <status with Code()=DeadlineExceeded>", err)
}
}
func (s) TestChainUnaryServerInterceptor(t *testing.T) {
var (
firstIntKey = ctxKey("firstIntKey")
secondIntKey = ctxKey("secondIntKey")
)
firstInt := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if ctx.Value(firstIntKey) != nil {
return nil, status.Errorf(codes.Internal, "first interceptor should not have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
return nil, status.Errorf(codes.Internal, "first interceptor should not have %v in context", secondIntKey)
}
firstCtx := context.WithValue(ctx, firstIntKey, 0)
resp, err := handler(firstCtx, req)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to handle request at firstInt")
}
simpleResp, ok := resp.(*testpb.SimpleResponse)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to get *testpb.SimpleResponse at firstInt")
}
return &testpb.SimpleResponse{
Payload: &testpb.Payload{
Type: simpleResp.GetPayload().GetType(),
Body: append(simpleResp.GetPayload().GetBody(), '1'),
},
}, nil
}
secondInt := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if ctx.Value(firstIntKey) == nil {
return nil, status.Errorf(codes.Internal, "second interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) != nil {
return nil, status.Errorf(codes.Internal, "second interceptor should not have %v in context", secondIntKey)
}
secondCtx := context.WithValue(ctx, secondIntKey, 1)
resp, err := handler(secondCtx, req)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to handle request at secondInt")
}
simpleResp, ok := resp.(*testpb.SimpleResponse)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to get *testpb.SimpleResponse at secondInt")
}
return &testpb.SimpleResponse{
Payload: &testpb.Payload{
Type: simpleResp.GetPayload().GetType(),
Body: append(simpleResp.GetPayload().GetBody(), '2'),
},
}, nil
}
lastInt := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if ctx.Value(firstIntKey) == nil {
return nil, status.Errorf(codes.Internal, "last interceptor should have %v in context", firstIntKey)
}
if ctx.Value(secondIntKey) == nil {
return nil, status.Errorf(codes.Internal, "last interceptor should not have %v in context", secondIntKey)
}
resp, err := handler(ctx, req)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to handle request at lastInt at lastInt")
}
simpleResp, ok := resp.(*testpb.SimpleResponse)
if !ok {
return nil, status.Errorf(codes.Internal, "failed to get *testpb.SimpleResponse at lastInt")
}
return &testpb.SimpleResponse{
Payload: &testpb.Payload{
Type: simpleResp.GetPayload().GetType(),
Body: append(simpleResp.GetPayload().GetBody(), '3'),
},
}, nil
}
sopts := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(firstInt, secondInt, lastInt),
}
ss := &stubserver.StubServer{
UnaryCallF: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 0)
if err != nil {
return nil, status.Errorf(codes.Aborted, "failed to make payload: %v", err)
}
return &testpb.SimpleResponse{
Payload: payload,
}, nil
},
}
if err := ss.Start(sopts); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
resp, err := ss.Client.UnaryCall(ctx, &testpb.SimpleRequest{})
if s, ok := status.FromError(err); !ok || s.Code() != codes.OK {
t.Fatalf("ss.Client.UnaryCall(ctx, _) = %v, %v; want nil, <status with Code()=OK>", resp, err)
}
respBytes := resp.Payload.GetBody()
if string(respBytes) != "321" {
t.Fatalf("invalid response: want=%s, but got=%s", "321", resp)
}
}
func (s) TestChainOnBaseUnaryServerInterceptor(t *testing.T) {
baseIntKey := ctxKey("baseIntKey")
baseInt := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if ctx.Value(baseIntKey) != nil {
return nil, status.Errorf(codes.Internal, "base interceptor should not have %v in context", baseIntKey)
}
baseCtx := context.WithValue(ctx, baseIntKey, 1)
return handler(baseCtx, req)
}
chainInt := func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if ctx.Value(baseIntKey) == nil {
return nil, status.Errorf(codes.Internal, "chain interceptor should have %v in context", baseIntKey)
}
return handler(ctx, req)
}
sopts := []grpc.ServerOption{
grpc.UnaryInterceptor(baseInt),
grpc.ChainUnaryInterceptor(chainInt),
}
ss := &stubserver.StubServer{
EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
return &testpb.Empty{}, nil
},
}
if err := ss.Start(sopts); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
resp, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
if s, ok := status.FromError(err); !ok || s.Code() != codes.OK {
t.Fatalf("ss.Client.EmptyCall(ctx, _) = %v, %v; want nil, <status with Code()=OK>", resp, err)
}
}
func (s) TestChainStreamServerInterceptor(t *testing.T) {
callCounts := make([]int, 4)
firstInt := func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if callCounts[0] != 0 {
return status.Errorf(codes.Internal, "callCounts[0] should be 0, but got=%d", callCounts[0])
}
if callCounts[1] != 0 {
return status.Errorf(codes.Internal, "callCounts[1] should be 0, but got=%d", callCounts[1])
}
if callCounts[2] != 0 {
return status.Errorf(codes.Internal, "callCounts[2] should be 0, but got=%d", callCounts[2])
}
if callCounts[3] != 0 {
return status.Errorf(codes.Internal, "callCounts[3] should be 0, but got=%d", callCounts[3])
}
callCounts[0]++
return handler(srv, stream)
}
secondInt := func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if callCounts[0] != 1 {
return status.Errorf(codes.Internal, "callCounts[0] should be 1, but got=%d", callCounts[0])
}
if callCounts[1] != 0 {
return status.Errorf(codes.Internal, "callCounts[1] should be 0, but got=%d", callCounts[1])
}
if callCounts[2] != 0 {
return status.Errorf(codes.Internal, "callCounts[2] should be 0, but got=%d", callCounts[2])
}
if callCounts[3] != 0 {
return status.Errorf(codes.Internal, "callCounts[3] should be 0, but got=%d", callCounts[3])
}
callCounts[1]++
return handler(srv, stream)
}
lastInt := func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if callCounts[0] != 1 {
return status.Errorf(codes.Internal, "callCounts[0] should be 1, but got=%d", callCounts[0])
}
if callCounts[1] != 1 {
return status.Errorf(codes.Internal, "callCounts[1] should be 1, but got=%d", callCounts[1])
}
if callCounts[2] != 0 {
return status.Errorf(codes.Internal, "callCounts[2] should be 0, but got=%d", callCounts[2])
}
if callCounts[3] != 0 {
return status.Errorf(codes.Internal, "callCounts[3] should be 0, but got=%d", callCounts[3])
}
callCounts[2]++
return handler(srv, stream)
}
sopts := []grpc.ServerOption{
grpc.ChainStreamInterceptor(firstInt, secondInt, lastInt),
}
ss := &stubserver.StubServer{
FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
if callCounts[0] != 1 {
return status.Errorf(codes.Internal, "callCounts[0] should be 1, but got=%d", callCounts[0])
}
if callCounts[1] != 1 {
return status.Errorf(codes.Internal, "callCounts[1] should be 1, but got=%d", callCounts[1])
}
if callCounts[2] != 1 {
return status.Errorf(codes.Internal, "callCounts[2] should be 0, but got=%d", callCounts[2])
}
if callCounts[3] != 0 {
return status.Errorf(codes.Internal, "callCounts[3] should be 0, but got=%d", callCounts[3])
}
callCounts[3]++
return nil
},
}
if err := ss.Start(sopts); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
stream, err := ss.Client.FullDuplexCall(ctx)
if err != nil {
t.Fatalf("failed to FullDuplexCall: %v", err)
}
_, err = stream.Recv()
if err != io.EOF {
t.Fatalf("failed to recv from stream: %v", err)
}
if callCounts[3] != 1 {
t.Fatalf("callCounts[3] should be 1, but got=%d", callCounts[3])
}
}