-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorted_set_counter_test.go
103 lines (94 loc) · 2.24 KB
/
sorted_set_counter_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
package redis_rate_limiter
import (
"context"
"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestSortedSetCounterStrategy_Run(t *testing.T) {
tt := []struct {
name string
runs int64
request *Request
lastResult *Result
lastErr string
advance time.Duration
}{
{
name: "returns Allow for requests under limit",
request: &Request{
Key: "some-user",
Limit: 100,
Duration: time.Minute,
},
lastResult: &Result{
State: Allow,
TotalRequests: 50,
ExpiresAt: time.Date(2020, time.March, 25, 10, 16, 30, 0, time.UTC),
},
runs: 50,
},
{
name: "returns Deny for requests over limit",
request: &Request{
Key: "some-user",
Limit: 100,
Duration: time.Minute,
},
lastResult: &Result{
State: Deny,
TotalRequests: 100,
ExpiresAt: time.Date(2020, time.March, 25, 10, 16, 30, 0, time.UTC),
},
runs: 101,
},
{
name: "expires and starts again as it goes over the TTL",
request: &Request{
Key: "some-user",
Limit: 100,
Duration: time.Minute,
},
lastResult: &Result{
State: Allow,
TotalRequests: 60,
ExpiresAt: time.Date(2020, time.March, 25, 10, 18, 9, 0, time.UTC),
},
runs: 100,
advance: time.Second,
},
}
for _, ts := range tt {
t.Run(ts.name, func(t *testing.T) {
server, err := miniredis.Run()
require.NoError(t, err)
defer server.Close()
now := time.Date(2020, 3, 25, 10, 15, 30, 0, time.UTC)
client := redis.NewClient(&redis.Options{
Addr: server.Addr(),
})
defer client.Close()
counter := NewSortedSetCounterStrategy(client, func() time.Time {
return now
})
var lastResult *Result
var lastErr error
for x := int64(0); x < ts.runs; x++ {
lastResult, lastErr = counter.Run(context.Background(), ts.request)
if ts.advance != 0 {
server.FastForward(ts.advance)
now = now.Add(ts.advance)
}
}
assert.Equal(t, ts.lastResult, lastResult)
if ts.lastErr != "" {
assert.EqualError(t, lastErr, ts.lastErr)
} else {
assert.NoError(t, lastErr)
}
})
}
}