generated from denpeshkov/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_test.go
317 lines (267 loc) · 8.62 KB
/
rate_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
package throttle
import (
"context"
"errors"
"fmt"
"log"
"os"
"testing"
"time"
rds "github.com/redis/go-redis/v9"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/redis"
)
var (
redisAddr string
now = time.Now()
)
type rediser struct {
rds *rds.Client
}
func (r rediser) ScriptLoad(ctx context.Context, script string) (string, error) {
return r.rds.ScriptLoad(ctx, script).Result()
}
func (r rediser) EvalSHA(ctx context.Context, sha1 string, keys []string, args ...any) (any, error) {
return r.rds.EvalSha(ctx, sha1, keys, args...).Result()
}
func (r rediser) Del(ctx context.Context, keys ...string) (int64, error) {
return r.rds.Del(ctx, keys...).Result()
}
type allow struct {
now time.Time
status *Status
}
type noopLogger struct{}
func (noopLogger) Printf(format string, v ...interface{}) {}
func setupRedis() (teardown func(ctx context.Context) error, err error) {
reds, err := redis.Run(context.Background(), "redis:latest", testcontainers.WithLogger(noopLogger{}))
if err != nil {
return nil, err
}
addr, err := reds.Endpoint(context.Background(), "")
if err != nil {
return nil, err
}
redisAddr = addr
return reds.Terminate, nil
}
func initLimiter(t *testing.T, rds rediser, key string, limit Limit) *Limiter {
t.Helper()
l, err := NewLimiter(rds, key, limit)
if err != nil {
t.Fatalf("Failed to create a limiter: %v", err)
}
if err := l.Reset(context.Background()); err != nil {
t.Fatalf("Failed to reset a limiter: %v", err)
}
return l
}
func testAllow(t *testing.T, l *Limiter, allows ...allow) {
t.Helper()
for _, a := range allows {
status, err := l.allowAt(context.Background(), a.now, Inf)
if err != nil {
t.Errorf("allowAt(%s) unexpected error: %v", a.now.Format("15:04:05.000"), err)
}
if *status != *a.status {
t.Errorf("allowAt(%s) = %s, want %s", a.now.Format("15:04:05.000"), status, a.status)
}
}
}
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) (code int) {
teardown, err := setupRedis()
if err != nil {
log.Printf("Failed to setup Redis: %v\n", err)
return 1
}
defer func() {
if err := teardown(context.Background()); err != nil {
log.Printf("Failed to teardown Redis: %v\n", err)
code = 1
}
}()
return m.Run()
}
const delta = 1 * time.Millisecond // Limiter resolution.s
func TestAllow(t *testing.T) {
rds := rediser{rds.NewClient(&rds.Options{Addr: redisAddr})}
key := "ratelimit:test_allow"
t.Run("disallow all", func(t *testing.T) {
t.Parallel()
allow := allow{now, &Status{true, 0, Inf}}
l := initLimiter(t, rds, fmt.Sprintf("%s:%s", key, t.Name()), Limit{Events: 0, Interval: 1 * time.Second})
testAllow(t, l, allow)
})
t.Run("basic", func(t *testing.T) {
t.Parallel()
lims := []Limit{
{Events: 1, Interval: 10 * time.Millisecond},
{Events: 2, Interval: 10 * time.Millisecond},
{Events: 10, Interval: 10 * time.Millisecond},
{Events: 1, Interval: 999 * time.Millisecond},
{Events: 2, Interval: 999 * time.Millisecond},
{Events: 10, Interval: 999 * time.Millisecond},
{Events: 1, Interval: 1 * time.Second},
{Events: 2, Interval: 1 * time.Second},
{Events: 10, Interval: 1 * time.Second},
}
for _, lim := range lims {
t.Run(lim.String(), func(t *testing.T) {
t.Parallel()
now := now
t.Logf("now: %s", now.Format("15:04:05.000"))
l := initLimiter(t, rds, fmt.Sprintf("%s:%s", key, t.Name()), lim)
// Hit until last unlimited event.
want := Status{Limited: false, Remaining: lim.Events - 1, Delay: 0}
for want.Remaining > 0 {
s, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
if *s != want {
t.Errorf("allowAt(%s) = %s, want %s", now.Format("15:04:05.000"), s, want)
}
want.Remaining--
now = now.Add(delta)
}
// Last unlimited event should have some positive delay.
s, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
if s.Limited || s.Delay <= 0 || s.Remaining > 0 {
t.Errorf("allowAt(%s) = %s, want (unlimited, 0 req, positive delay)", now.Format("15:04:05.000"), s)
}
// These events should be limited.
stopTime := now.Add(s.Delay)
now = now.Add(delta)
want = Status{Limited: true, Remaining: 0, Delay: s.Delay - delta}
for now.Before(stopTime) {
s, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
if *s != want {
t.Errorf("allowAt(%s) = %s, want %s", now.Format("15:04:05.000"), s, want)
}
want.Delay -= delta
now = now.Add(delta)
}
})
}
})
t.Run("delta exceeds interval", func(t *testing.T) {
t.Parallel()
lims := []Limit{
{Events: 1, Interval: 1 * time.Millisecond},
{Events: 2, Interval: 1 * time.Millisecond},
{Events: 1, Interval: 1 * time.Second},
{Events: 2, Interval: 1 * time.Second},
}
for _, lim := range lims {
t.Run(lim.String(), func(t *testing.T) {
t.Parallel()
now := now
t.Logf("now: %s", now.Format("15:04:05.000"))
l := initLimiter(t, rds, fmt.Sprintf("%s:%s", key, t.Name()), lim)
// First hit.
s1, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
// Second hit. Limit should be reset.
now = now.Add(lim.Interval + 1*time.Millisecond)
s2, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
if *s1 != *s2 {
t.Errorf("Limit not reset. allowAt(%s) = %s, want %s", now.Format("15:04:05.000"), s2, s1)
}
})
}
})
}
func TestSetLimit(t *testing.T) {
rds := rediser{rds.NewClient(&rds.Options{Addr: redisAddr})}
key := "ratelimit:test_set_limit"
lim := Limit{Events: 1, Interval: 1 * time.Second}
l := initLimiter(t, rds, fmt.Sprintf("%s:%s", key, t.Name()), lim)
if err := l.SetLimit(context.Background(), lim); err != nil {
t.Fatalf("SetLimit(%s) error: %v", lim, err)
}
if l.Limit() != lim {
t.Errorf("SetLimit(%s) failed to set limit; Limit() = %v, want %v", lim, l.Limit(), lim)
}
lim = Limit{Events: 5, Interval: 10 * time.Second}
if err := l.SetLimit(context.Background(), lim); err != nil {
t.Fatalf("SetLimit(%s) error: %v", lim, err)
}
if l.Limit() != lim {
t.Errorf("SetLimit(%s) failed to set limit; Limit() = %v, want %v", lim, l.Limit(), lim)
}
}
func TestInvalidLimit(t *testing.T) {
t.Run("invalid interval", func(t *testing.T) {
intervals := []time.Duration{-1, 0, 999 * time.Nanosecond}
for _, in := range intervals {
lim := Limit{Events: 1, Interval: in}
l, err := NewLimiter(nil, "", lim)
if !errors.Is(err, errInvalidInterval) {
t.Fatalf("NewLimiter(%s) error: %v, want: %v", lim, err, errInvalidInterval)
}
if err := l.SetLimit(context.Background(), lim); !errors.Is(err, errInvalidInterval) {
t.Fatalf("SetLimit(%s) error: %v, want: %v", lim, err, errInvalidInterval)
}
}
})
t.Run("invalid events", func(t *testing.T) {
lim := Limit{Events: -1, Interval: 1 * time.Second}
l, err := NewLimiter(nil, "", lim)
if !errors.Is(err, errInvalidEvents) {
t.Fatalf("NewLimiter(%s) error: %v, want: %v", lim, err, errInvalidEvents)
}
if err := l.SetLimit(context.Background(), lim); !errors.Is(err, errInvalidEvents) {
t.Fatalf("SetLimit(%s) error: %v, want: %v", lim, err, errInvalidEvents)
}
})
}
func TestReset(t *testing.T) {
t.Parallel()
rds := rediser{rds.NewClient(&rds.Options{Addr: redisAddr})}
key := "ratelimit:test_reset"
tests := []struct {
name string
events int
}{
{"limited", 1},
{"unlimited", 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
now := now
l := initLimiter(t, rds, fmt.Sprintf("%s:%s", key, t.Name()), Limit{Events: 1, Interval: 1 * time.Second})
// First event.
s1, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
now = now.Add(delta)
// After Reset() second event should return the same status as the first event.
if err := l.Reset(context.Background()); err != nil {
t.Fatalf("Reset() error: %v", err)
}
s2, err := l.allowAt(context.Background(), now, Inf)
if err != nil {
t.Fatalf("allowAt(%s) error: %v", now.Format("15:04:05.000"), err)
}
if *s1 != *s2 {
t.Errorf("Limit not reset. allowAt(%s) = %s, want %s", now.Format("15:04:05.000"), s2, s1)
}
})
}
}