File: ratelimit_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-middleware 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,464 kB
  • sloc: makefile: 107; sh: 9
file content (190 lines) | stat: -rw-r--r-- 5,748 bytes parent folder | download | duplicates (3)
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
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.

package ratelimit

import (
	"context"
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

const errMsgFake = "fake error"

type ctxKey string

var ctxKeyShouldLimit = ctxKey("shouldLimit")

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 {
	shouldLimit, _ := ctx.Value(ctxKeyShouldLimit).(bool)

	if shouldLimit {
		return errors.New("rate limit exceeded")
	}

	return nil
}

func TestUnaryServerInterceptor_RateLimitPass(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, 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(), ctxKeyShouldLimit, 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(), ctxKeyShouldLimit, true)

	interceptor := UnaryServerInterceptor(limiter)
	called := false
	handler := func(ctx context.Context, req any) (any, error) {
		called = true
		return nil, errors.New(errMsgFake)
	}
	info := &grpc.UnaryServerInfo{
		FullMethod: "FakeMethod",
	}
	resp, err := interceptor(ctx, nil, info, handler)
	expErr := status.Errorf(
		codes.ResourceExhausted,
		"%s is rejected by grpc_ratelimit middleware, please retry later. %s",
		info.FullMethod,
		"rate limit exceeded",
	)
	assert.Nil(t, resp)
	assert.EqualError(t, err, expErr.Error())
	assert.False(t, called)
}

func TestStreamServerInterceptor_RateLimitFail(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, true)

	interceptor := StreamServerInterceptor(limiter)
	called := false
	handler := func(srv any, stream grpc.ServerStream) error {
		called = true
		return errors.New(errMsgFake)
	}
	info := &grpc.StreamServerInfo{
		FullMethod: "FakeMethod",
	}
	err := interceptor(nil, &mockGRPCServerStream{ctx: ctx}, info, handler)
	expErr := status.Errorf(
		codes.ResourceExhausted,
		"%s is rejected by grpc_ratelimit middleware, please retry later. %s",
		info.FullMethod,
		"rate limit exceeded",
	)

	assert.EqualError(t, err, expErr.Error())
	assert.False(t, called)
}

func TestUnaryClientInterceptor_RateLimitPass(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, false)

	interceptor := UnaryClientInterceptor(limiter)
	invoker := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
		return errors.New(errMsgFake)
	}
	err := interceptor(ctx, "FakeMethod", nil, nil, nil, invoker)
	assert.EqualError(t, err, errMsgFake)
}

func TestStreamClientInterceptor_RateLimitPass(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, false)

	interceptor := StreamClientInterceptor(limiter)
	invoker := func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
		return nil, errors.New(errMsgFake)
	}
	_, err := interceptor(ctx, nil, nil, "FakeMethod", invoker)
	assert.EqualError(t, err, errMsgFake)
}

func TestUnaryClientInterceptor_RateLimitFail(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, true)

	interceptor := UnaryClientInterceptor(limiter)
	called := false
	invoker := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
		called = true
		return errors.New(errMsgFake)
	}
	err := interceptor(ctx, "FakeMethod", nil, nil, nil, invoker)
	expErr := status.Errorf(
		codes.ResourceExhausted,
		"%s is rejected by grpc_ratelimit middleware, please retry later. %s",
		"FakeMethod",
		"rate limit exceeded",
	)
	assert.EqualError(t, err, expErr.Error())
	assert.False(t, called)
}

func TestStreamClientInterceptor_RateLimitFail(t *testing.T) {
	limiter := new(mockContextBasedLimiter)
	ctx := context.WithValue(context.Background(), ctxKeyShouldLimit, true)

	interceptor := StreamClientInterceptor(limiter)
	called := false
	invoker := func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
		called = true
		return nil, errors.New(errMsgFake)
	}
	_, err := interceptor(ctx, nil, nil, "FakeMethod", invoker)
	expErr := status.Errorf(
		codes.ResourceExhausted,
		"%s is rejected by grpc_ratelimit middleware, please retry later. %s",
		"FakeMethod",
		"rate limit exceeded",
	)
	assert.EqualError(t, err, expErr.Error())
	assert.False(t, called)
}