forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimit_test.go
96 lines (78 loc) · 2.55 KB
/
ratelimit_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
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package ratelimit
import (
"context"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)
const errMsgFake = "fake error"
var ctxLimitKey = struct{}{}
type mockGRPCServerStream struct {
grpc.ServerStream
ctx context.Context
}
func (m *mockGRPCServerStream) Context() context.Context {
return m.ctx
}
type mockContextBasedLimiter struct{}
func (*mockContextBasedLimiter) Limit(ctx context.Context) error {
l, _ := ctx.Value(ctxLimitKey).(error)
return l
}
func TestUnaryServerInterceptor_RateLimitPass(t *testing.T) {
limiter := new(mockContextBasedLimiter)
ctx := context.WithValue(context.Background(), ctxLimitKey, false)
interceptor := UnaryServerInterceptor(limiter)
handler := func(ctx context.Context, req any) (any, error) {
return nil, errors.New(errMsgFake)
}
info := &grpc.UnaryServerInfo{
FullMethod: "FakeMethod",
}
resp, err := interceptor(ctx, nil, info, handler)
assert.Nil(t, resp)
assert.EqualError(t, err, errMsgFake)
}
func TestStreamServerInterceptor_RateLimitPass(t *testing.T) {
limiter := new(mockContextBasedLimiter)
ctx := context.WithValue(context.Background(), ctxLimitKey, false)
interceptor := StreamServerInterceptor(limiter)
handler := func(srv any, stream grpc.ServerStream) error {
return errors.New(errMsgFake)
}
info := &grpc.StreamServerInfo{
FullMethod: "FakeMethod",
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler)
assert.EqualError(t, err, errMsgFake)
}
func TestUnaryServerInterceptor_RateLimitFail(t *testing.T) {
limiter := new(mockContextBasedLimiter)
ctx := context.WithValue(context.Background(), ctxLimitKey, true)
interceptor := UnaryServerInterceptor(limiter)
handler := func(ctx context.Context, req any) (any, error) {
return nil, errors.New(errMsgFake)
}
info := &grpc.UnaryServerInfo{
FullMethod: "FakeMethod",
}
resp, err := interceptor(ctx, nil, info, handler)
assert.Nil(t, resp)
assert.EqualError(t, err, errMsgFake)
}
func TestStreamServerInterceptor_RateLimitFail(t *testing.T) {
limiter := new(mockContextBasedLimiter)
ctx := context.WithValue(context.Background(), ctxLimitKey, true)
interceptor := StreamServerInterceptor(limiter)
handler := func(srv interface{}, stream grpc.ServerStream) error {
return errors.New(errMsgFake)
}
info := &grpc.StreamServerInfo{
FullMethod: "FakeMethod",
}
err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler)
assert.EqualError(t, err, errMsgFake)
}