forked from go-redis/redis_rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_test.go
142 lines (118 loc) · 3.13 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
package redis_rate_test
import (
"context"
"testing"
"time"
"github.com/redis/go-redis/v9"
"golang.org/x/time/rate"
"github.com/molotovtv/redis_rate/v11"
)
func rateLimiter() *redis_rate.Limiter {
ring := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{"server0": ":6379"},
})
if err := ring.FlushDB(context.Background()).Err(); err != nil {
panic(err)
}
return redis_rate.NewLimiter(ring)
}
func TestAllow(t *testing.T) {
l := rateLimiter()
rate, delay, allow := l.Allow(context.Background(), "test_id", 1, time.Minute)
if !allow {
t.Fatalf("rate limited with rate %d", rate)
}
if rate != 1 {
t.Fatalf("got %d, wanted 1", rate)
}
if delay > time.Minute {
t.Fatalf("got %s, wanted <= %s", delay, time.Minute)
}
rate, _, allow = l.Allow(context.Background(), "test_id", 1, time.Minute)
if allow {
t.Fatalf("not rate limited with rate %d", rate)
}
if rate != 2 {
t.Fatalf("got %d, wanted 2", rate)
}
}
func TestAllowRateMinute(t *testing.T) {
const n = 2
const dur = time.Minute
l := rateLimiter()
_, allow := l.AllowRate(context.Background(), "rate", n*rate.Every(dur))
if !allow {
t.Fatal("rate limited")
}
delay, allow := l.AllowRate(context.Background(), "rate", n*rate.Every(dur))
if allow {
t.Fatal("not rate limited")
}
if !durEqual(delay, dur/n) {
t.Fatalf("got %s, wanted 0 < dur < %s", delay, dur/n)
}
}
func TestAllowRateSecond(t *testing.T) {
const n = 10
const dur = time.Second
l := rateLimiter()
for i := 0; i < n; i++ {
_, allow := l.AllowRate(context.Background(), "rate", n*rate.Every(dur))
if !allow {
t.Fatal("rate limited")
}
}
delay, allow := l.AllowRate(context.Background(), "rate", n*rate.Every(dur))
if allow {
t.Fatal("not rate limited")
}
if !durEqual(delay, time.Second) {
t.Fatalf("got %s, wanted 0 < dur < %s", delay, dur)
}
}
func TestRedisIsDown(t *testing.T) {
ring := redis.NewRing(&redis.RingOptions{})
l := redis_rate.NewLimiter(ring)
l.Fallback = rate.NewLimiter(rate.Every(time.Second), 1)
rate, _, allow := l.AllowMinute(context.Background(), "test_id", 1)
if !allow {
t.Fatalf("rate limited with rate %d", rate)
}
if rate != 0 {
t.Fatalf("got %d, wanted 0", rate)
}
rate, _, allow = l.AllowMinute(context.Background(), "test_id", 1)
if allow {
t.Fatalf("not rate limited with rate %d", rate)
}
if rate != 0 {
t.Fatalf("got %d, wanted 0", rate)
}
}
func TestAllowN(t *testing.T) {
l := rateLimiter()
rate, delay, allow := l.AllowN(context.Background(), "test_allow_n", 1, time.Minute, 1)
if !allow {
t.Fatalf("rate limited with rate %d", rate)
}
if rate != 1 {
t.Fatalf("got %d, wanted 1", rate)
}
if delay > time.Minute {
t.Fatalf("got %s, wanted <= %s", delay, time.Minute)
}
l.AllowN(context.Background(), "test_allow_n", 1, time.Minute, 2)
rate, delay, allow = l.AllowN(context.Background(), "test_allow_n", 1, time.Minute, 0)
if allow {
t.Fatalf("should rate limit with rate %d", rate)
}
if rate != 3 {
t.Fatalf("got %d, wanted 3", rate)
}
if delay > time.Minute {
t.Fatalf("got %s, wanted <= %s", delay, time.Minute)
}
}
func durEqual(got, wanted time.Duration) bool {
return got > 0 && got < wanted
}