-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimiter_test.go
More file actions
174 lines (141 loc) · 4.18 KB
/
Copy pathratelimiter_test.go
File metadata and controls
174 lines (141 loc) · 4.18 KB
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
package ratelimiter_test
import (
"errors"
"testing"
"time"
"github.com/rcdmk/go-ratelimiter"
)
func TestRateLimiter_Allow(t *testing.T) {
sourceKey := "test"
options := ratelimiter.Options{
MaxRatePerSecond: 10,
MaxBurst: 5,
}
limiter := ratelimiter.New(options)
// Allow 5 events
for i := 0; i < 5; i++ {
if !limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Allow 5 more events after waiting for 1 second
time.Sleep(time.Second)
for i := 0; i < 5; i++ {
if !limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Try to allow 1 more event, which should be rate-limited
if limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
}
func TestRateLimiter_Allow_ZeroRate(t *testing.T) {
sourceKey := "test"
// Create a new RateLimiter with zero rate
options := ratelimiter.Options{
MaxRatePerSecond: 0,
MaxBurst: 5,
}
limiter := ratelimiter.New(options)
// Try to allow an event, which should be rate-limited
if limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
}
func TestRateLimiter_Allow_ZeroBurst(t *testing.T) {
limitKey := "test"
options := ratelimiter.Options{
MaxRatePerSecond: 10,
MaxBurst: 0,
}
limiter := ratelimiter.New(options)
// Try to allow an event, which should be rate-limited
if limiter.Allow(limitKey) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
}
func TestRateLimiter_Allow_BurstHigherThanMaxRate(t *testing.T) {
sourceKey := "test"
options := ratelimiter.Options{
MaxRatePerSecond: 10,
MaxBurst: 15,
}
limiter := ratelimiter.New(options)
// Allow 15 events
for i := 0; i < 15; i++ {
if !limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Try to allow 1 more event, which should be rate-limited
if limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
// Allow 10 more events after waiting for 1 second
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
if !limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Try to allow 1 more event, which should be rate-limited
if limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
}
func TestRateLimiter_Allow_Multiple_Keys(t *testing.T) {
sourceKey1 := "test1"
sourceKey2 := "test2"
options := ratelimiter.Options{
MaxRatePerSecond: 10,
MaxBurst: 5,
}
limiter := ratelimiter.New(options)
// Allow 5 events for limitKey1
for i := 0; i < 5; i++ {
if !limiter.Allow(sourceKey1) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Allow 5 events for limitKey2
for i := 0; i < 5; i++ {
if !limiter.Allow(sourceKey2) {
t.Errorf("Expected limiter to allow event, but it didn't")
}
}
// Try to allow 1 more event for limitKey1, which should be rate-limited
if limiter.Allow(sourceKey1) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
// Try to allow 1 more event for limitKey2, which should be rate-limited
if limiter.Allow(sourceKey2) {
t.Errorf("Expected limiter to rate-limit event, but it didn't")
}
}
func TestRateLimiter_Allow_Always_If_Cache_Fails(t *testing.T) {
sourceKey := "test"
options := ratelimiter.Options{
MaxRatePerSecond: 10,
MaxBurst: 5,
Cache: &mockFailedCache{},
}
limiter := ratelimiter.New(options)
// Allow 15 events
for i := 0; i < 30; i++ {
if !limiter.Allow(sourceKey) {
t.Errorf("Expected limiter to allow event, but it didn't: %d", i)
}
}
}
// mockFailedCache is a mock implementation of the cache.GetterSetter interface that fails on all operations.
type mockFailedCache struct{}
func (c *mockFailedCache) Get(key string) (int, error) {
return 0, errors.New("mock cache error: get")
}
func (c *mockFailedCache) Set(key string, value int) error {
return errors.New("mock cache error: set")
}
func (c *mockFailedCache) SetWithExpiration(key string, value int, expiration time.Duration) error {
return errors.New("mock cache error: set with expiration")
}