Repository navigation
Expand file tree
/
Copy pathhandlers_limits_test.go
More file actions
528 lines (474 loc) · 19.6 KB
/
Copy pathhandlers_limits_test.go
File metadata and controls
528 lines (474 loc) · 19.6 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package grpcui
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/golang/protobuf/jsonpb"
oldproto "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func TestRPCInvokeHandlerStopsAtRetainedResponseCountLimit(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{
responseCount: maxInvokeRetainedResponses + 1,
payload: "bounded",
}
response := performInvokeRetentionRequest(t, method, connection, 0)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(result.Responses) != maxInvokeRetainedResponses {
t.Fatalf("retained responses = %d, want %d", len(result.Responses), maxInvokeRetainedResponses)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitResponseCount {
t.Fatalf("local limit = %#v, want response-count evidence", result.LocalLimit)
}
if result.LocalLimit.RetainedResponses != maxInvokeRetainedResponses {
t.Fatalf("reported retained responses = %d, want %d", result.LocalLimit.RetainedResponses, maxInvokeRetainedResponses)
}
if result.Error != nil {
t.Fatalf("gRPC status = %#v, want unobserved after local cancellation", result.Error)
}
if len(result.Trailers) != 0 || result.Timings.TrailersMs != nil {
t.Fatalf("trailers/status timing = %#v/%v, want unobserved", result.Trailers, result.Timings.TrailersMs)
}
if len(result.Headers) != 1 || result.Timings.HeadersMs == nil || result.Timings.FirstMessageMs == nil {
t.Fatalf("preserved response evidence = headers %#v, timings %#v", result.Headers, result.Timings)
}
select {
case <-connection.canceled:
default:
t.Fatal("underlying RPC context was not canceled at the retained response limit")
}
}
func TestRPCInvokeHandlerStopsBeforeRetainedResponseBytesExceedLimit(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{
responseCount: 4,
payload: strings.Repeat("x", maxInvokeRetainedResponseBytes/3),
}
response := performInvokeRetentionRequest(t, method, connection, 0)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(result.Responses) != 2 {
t.Fatalf("retained responses = %d, want 2 before the third crosses the byte limit", len(result.Responses))
}
retainedBytes := 0
for _, response := range result.Responses {
retainedBytes += len(response.Data)
}
if retainedBytes > maxInvokeRetainedResponseBytes {
t.Fatalf("retained response bytes = %d, limit %d", retainedBytes, maxInvokeRetainedResponseBytes)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitResponseBytes {
t.Fatalf("local limit = %#v, want response-bytes evidence", result.LocalLimit)
}
if result.LocalLimit.RetainedResponseBytes <= 2*len(connection.payload) || result.LocalLimit.RetainedResponseBytes > maxInvokeRetainedResponseBytes || result.LocalLimit.MaxResponseBytes != maxInvokeRetainedResponseBytes {
t.Fatalf("reported byte evidence = %#v, decoded envelope bytes %d", result.LocalLimit, retainedBytes)
}
if result.Error != nil || len(result.Trailers) != 0 || result.Timings.TrailersMs != nil {
t.Fatalf("fabricated final status evidence = error %#v, trailers %#v, timing %v", result.Error, result.Trailers, result.Timings.TrailersMs)
}
select {
case <-connection.canceled:
default:
t.Fatal("underlying RPC context was not canceled at the retained byte limit")
}
}
func TestRPCInvokeHandlerAppliesConfiguredWallWhenDeadlineIsAbsentOrExcessive(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
for _, timeoutSeconds := range []float64{0, 120} {
timeoutSeconds := timeoutSeconds
t.Run(strconv.FormatFloat(timeoutSeconds, 'f', -1, 64), func(t *testing.T) {
t.Parallel()
connection := &invokeRetentionClientConn{blockUntilCanceled: true}
response := performInvokeRetentionRequestWithOptions(
t,
method,
connection,
timeoutSeconds,
InvokeOptions{MaxDuration: 20 * time.Millisecond},
)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitDuration || result.LocalLimit.MaxDurationSeconds != 0.02 {
t.Fatalf("configured wall evidence = limit %#v, error %#v", result.LocalLimit, result.Error)
}
})
}
}
func TestRPCInvokeHandlerReportsLocalWallWithoutFabricatingGRPCStatus(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{blockUntilCanceled: true}
handler := rpcInvokeHandlerWithLimits(
connection,
[]*desc.MethodDescriptor{method},
InvokeOptions{},
invokeSafetyLimits{maxDuration: 20 * time.Millisecond},
)
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(`{"data":[{}]}`))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitDuration {
t.Fatalf("local limit = %#v, want duration evidence", result.LocalLimit)
}
if result.LocalLimit.MaxDurationSeconds != 0.02 {
t.Fatalf("reported wall = %v seconds, want 0.02", result.LocalLimit.MaxDurationSeconds)
}
if result.Error != nil || len(result.Trailers) != 0 || result.Timings.TrailersMs != nil {
t.Fatalf("fabricated final status evidence = error %#v, trailers %#v, timing %v", result.Error, result.Trailers, result.Timings.TrailersMs)
}
if len(result.Headers) != 1 || result.Timings.HeadersMs == nil || result.Timings.TotalMs < 15 {
t.Fatalf("preserved partial evidence = headers %#v, timings %#v", result.Headers, result.Timings)
}
select {
case <-connection.canceled:
default:
t.Fatal("wall timer did not cancel the underlying RPC")
}
}
func TestRPCInvokeHandlerWithOptionsReportsConfiguredLocalWall(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{blockUntilCanceled: true}
handler := RPCInvokeHandlerWithOptions(
connection,
[]*desc.MethodDescriptor{method},
InvokeOptions{MaxDuration: 20 * time.Millisecond},
)
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(`{"data":[{}]}`))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitDuration || result.LocalLimit.MaxDurationSeconds != 0.02 {
t.Fatalf("configured local wall evidence = %#v", result.LocalLimit)
}
if result.Error != nil || len(result.Trailers) != 0 || result.Timings.TrailersMs != nil {
t.Fatalf("configured local wall fabricated status = error %#v, trailers %#v, timing %v", result.Error, result.Trailers, result.Timings.TrailersMs)
}
}
func TestRPCInvokeHandlerOwnedWallSetsCauseBeforeTransportCancellation(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{
blockUntilCanceled: true,
preemptExposedDeadline: true,
}
handler := RPCInvokeHandlerWithOptions(
connection,
[]*desc.MethodDescriptor{method},
InvokeOptions{MaxDuration: 20 * time.Millisecond},
)
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(`{"data":[{}]}`))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if response.Code != http.StatusOK {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitDuration {
t.Fatalf("owned-wall evidence = limit %#v, error %#v", result.LocalLimit, result.Error)
}
if result.Error != nil || len(result.Trailers) != 0 {
t.Fatalf("owned wall fabricated final status = error %#v, trailers %#v", result.Error, result.Trailers)
}
}
func TestRPCInvokeHandlerPreservesTransportErrorReturnedBeforeOwnedWall(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{
newStreamError: errors.New("transport failed before local wall"),
}
handler := RPCInvokeHandlerWithOptions(
connection,
[]*desc.MethodDescriptor{method},
InvokeOptions{MaxDuration: 20 * time.Millisecond},
)
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(`{"data":[{}]}`))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if response.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, body = %q", response.Code, response.Body.String())
}
if body := response.Body.String(); !strings.Contains(body, "transport failed before local wall") || strings.Contains(body, "localLimit") {
t.Fatalf("transport error response = %q", body)
}
}
func TestRPCInvokeHandlerReportsItsActualInjectedWallForResponseLimits(t *testing.T) {
t.Parallel()
method := invokeRetentionMethod(t)
connection := &invokeRetentionClientConn{
responseCount: maxInvokeRetainedResponses + 1,
payload: "bounded",
}
handler := rpcInvokeHandlerWithLimits(
connection,
[]*desc.MethodDescriptor{method},
InvokeOptions{},
invokeSafetyLimits{maxDuration: 500 * time.Millisecond},
)
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(`{"data":[{}]}`))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
var result rpcResult
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
t.Fatalf("decode response: %v", err)
}
if result.LocalLimit == nil || result.LocalLimit.MaxDurationSeconds != 0.5 {
t.Fatalf("reported safety policy = %#v, want 0.5-second wall", result.LocalLimit)
}
}
func TestRPCResultPreservesNaturalCompletionAtExactRetentionBoundaries(t *testing.T) {
t.Parallel()
t.Run("response count", func(t *testing.T) {
result := newRPCResult(nil, false, nil)
for range maxInvokeRetainedResponses {
result.OnReceiveResponse(&invokeSizedJSONMessage{size: 2})
}
result.OnReceiveTrailers(status.New(codes.OK, ""), metadata.Pairs("x-final", "observed"))
if result.LocalLimit != nil || result.Error != nil || len(result.Responses) != maxInvokeRetainedResponses {
t.Fatalf("exact count result = limit %#v, error %#v, responses %d", result.LocalLimit, result.Error, len(result.Responses))
}
if len(result.Trailers) != 1 || result.Timings.TrailersMs == nil {
t.Fatalf("natural final evidence = trailers %#v, timing %v", result.Trailers, result.Timings.TrailersMs)
}
marshaled := 0
result.OnReceiveResponse(&invokeSizedJSONMessage{size: 2, marshaled: &marshaled})
if result.LocalLimit == nil || result.LocalLimit.Reason != invokeLimitResponseCount || marshaled != 0 {
t.Fatalf("response after exact count = limit %#v, marshal count %d", result.LocalLimit, marshaled)
}
})
t.Run("serialized bytes", func(t *testing.T) {
result := newRPCResult(nil, false, nil)
result.OnReceiveResponse(&invokeSizedJSONMessage{size: maxInvokeRetainedResponseBytes / 2})
result.OnReceiveResponse(&invokeSizedJSONMessage{size: maxInvokeRetainedResponseBytes / 2})
result.OnReceiveTrailers(status.New(codes.OK, ""), metadata.Pairs("x-final", "observed"))
if result.LocalLimit != nil || result.Error != nil || len(result.Responses) != 2 {
t.Fatalf("exact byte result = limit %#v, error %#v, responses %d", result.LocalLimit, result.Error, len(result.Responses))
}
if len(result.Trailers) != 1 || result.Timings.TrailersMs == nil {
t.Fatalf("natural final evidence = trailers %#v, timing %v", result.Trailers, result.Timings.TrailersMs)
}
})
}
func TestRPCResultDurationRacePreservesOnlyStatusObservedBeforeLocalWall(t *testing.T) {
t.Parallel()
t.Run("local wall wins", func(t *testing.T) {
result := newRPCResult(nil, false, nil)
result.contextCause = func() error { return errInvokeDurationLimit }
result.OnReceiveHeaders(metadata.Pairs("x-header", "observed"))
result.OnReceiveTrailers(
status.New(codes.DeadlineExceeded, "client wall"),
metadata.Pairs("x-trailer", "not-server-evidence"),
)
result.applyDurationLimit(20 * time.Millisecond)
if result.LocalLimit == nil || result.Error != nil || len(result.Trailers) != 0 || result.Timings.TrailersMs != nil {
t.Fatalf("local-wall result = limit %#v, error %#v, trailers %#v, timing %v", result.LocalLimit, result.Error, result.Trailers, result.Timings.TrailersMs)
}
})
t.Run("server status wins", func(t *testing.T) {
cause := error(nil)
result := newRPCResult(nil, false, nil)
result.contextCause = func() error { return cause }
result.OnReceiveTrailers(
status.New(codes.Unavailable, "server unavailable"),
metadata.Pairs("x-trailer", "server-evidence"),
)
cause = errInvokeDurationLimit
result.applyDurationLimit(20 * time.Millisecond)
if result.LocalLimit != nil || result.Error == nil || result.Error.Code != uint32(codes.Unavailable) || len(result.Trailers) != 1 || result.Timings.TrailersMs == nil {
t.Fatalf("server-first result = limit %#v, error %#v, trailers %#v, timing %v", result.LocalLimit, result.Error, result.Trailers, result.Timings.TrailersMs)
}
})
}
func performInvokeRetentionRequest(t *testing.T, method *desc.MethodDescriptor, connection grpc.ClientConnInterface, timeoutSeconds float64) *httptest.ResponseRecorder {
return performInvokeRetentionRequestWithOptions(t, method, connection, timeoutSeconds, InvokeOptions{})
}
func performInvokeRetentionRequestWithOptions(t *testing.T, method *desc.MethodDescriptor, connection grpc.ClientConnInterface, timeoutSeconds float64, options InvokeOptions) *httptest.ResponseRecorder {
t.Helper()
body := `{"data":[{}]}`
if timeoutSeconds != 0 {
body = `{"timeout_seconds":` + strconv.FormatFloat(timeoutSeconds, 'f', -1, 64) + `,"data":[{}]}`
}
request := httptest.NewRequest(http.MethodPost, "/"+method.GetFullyQualifiedName(), strings.NewReader(body))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
RPCInvokeHandlerWithOptions(connection, []*desc.MethodDescriptor{method}, options).ServeHTTP(response, request)
return response
}
func invokeRetentionMethod(t *testing.T) *desc.MethodDescriptor {
t.Helper()
file, err := desc.CreateFileDescriptor(&descriptor.FileDescriptorProto{
Name: oldproto.String("invoke_retention.proto"),
Package: oldproto.String("test"),
MessageType: []*descriptor.DescriptorProto{
{Name: oldproto.String("Empty")},
{
Name: oldproto.String("Payload"),
Field: []*descriptor.FieldDescriptorProto{{
Name: oldproto.String("payload"),
Number: oldproto.Int32(1),
Label: descriptor.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
Type: descriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
}},
},
},
Service: []*descriptor.ServiceDescriptorProto{{
Name: oldproto.String("RetentionService"),
Method: []*descriptor.MethodDescriptorProto{{
Name: oldproto.String("Watch"),
InputType: oldproto.String(".test.Empty"),
OutputType: oldproto.String(".test.Payload"),
ServerStreaming: oldproto.Bool(true),
}},
}},
})
if err != nil {
t.Fatalf("create retention descriptor: %v", err)
}
return file.FindService("test.RetentionService").FindMethodByName("Watch")
}
type invokeRetentionClientConn struct {
responseCount int
payload string
blockUntilCanceled bool
preemptExposedDeadline bool
newStreamError error
canceled chan struct{}
}
func (*invokeRetentionClientConn) Invoke(context.Context, string, any, any, ...grpc.CallOption) error {
return errors.New("unexpected unary invocation")
}
func (c *invokeRetentionClientConn) NewStream(ctx context.Context, _ *grpc.StreamDesc, _ string, _ ...grpc.CallOption) (grpc.ClientStream, error) {
if c.newStreamError != nil {
return nil, c.newStreamError
}
c.canceled = make(chan struct{})
return &invokeRetentionClientStream{
ctx: ctx,
responseCount: c.responseCount,
payload: c.payload,
blockUntilCanceled: c.blockUntilCanceled,
preemptExposedDeadline: c.preemptExposedDeadline,
canceled: c.canceled,
}, nil
}
type invokeRetentionClientStream struct {
ctx context.Context
responseCount int
payload string
blockUntilCanceled bool
preemptExposedDeadline bool
next int
canceled chan struct{}
cancelOnce sync.Once
}
func (s *invokeRetentionClientStream) Header() (metadata.MD, error) {
return metadata.Pairs("x-retention-header", "observed"), nil
}
func (*invokeRetentionClientStream) Trailer() metadata.MD {
return metadata.Pairs("x-retention-trailer", "not-observed")
}
func (*invokeRetentionClientStream) CloseSend() error { return nil }
func (s *invokeRetentionClientStream) Context() context.Context { return s.ctx }
func (*invokeRetentionClientStream) SendMsg(any) error { return nil }
func (s *invokeRetentionClientStream) RecvMsg(message any) error {
if s.preemptExposedDeadline {
if _, ok := s.ctx.Deadline(); ok {
return status.Error(codes.DeadlineExceeded, "transport observed the local wall before its cause")
}
}
if s.blockUntilCanceled {
<-s.ctx.Done()
s.cancelOnce.Do(func() { close(s.canceled) })
return status.Error(codes.DeadlineExceeded, "local wall")
}
select {
case <-s.ctx.Done():
s.cancelOnce.Do(func() { close(s.canceled) })
return status.Error(codes.Canceled, "local cancellation")
default:
}
if s.next >= s.responseCount {
return io.EOF
}
dynamicMessage, ok := message.(*dynamic.Message)
if !ok {
return errors.New("unexpected response message type")
}
if err := dynamicMessage.TrySetFieldByName("payload", s.payload); err != nil {
return err
}
s.next++
return nil
}
type invokeSizedJSONMessage struct {
size int
marshaled *int
}
func (*invokeSizedJSONMessage) Reset() {}
func (*invokeSizedJSONMessage) String() string { return "sized invoke response" }
func (*invokeSizedJSONMessage) ProtoMessage() {}
func (m *invokeSizedJSONMessage) MarshalJSONPB(*jsonpb.Marshaler) ([]byte, error) {
if m.marshaled != nil {
(*m.marshaled)++
}
payload := bytes.Repeat([]byte{' '}, m.size)
payload[0] = '{'
payload[len(payload)-1] = '}'
return payload, nil
}