-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathattribute_limiter_test.go
324 lines (267 loc) · 7.58 KB
/
attribute_limiter_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
318
319
320
321
322
323
324
package ratelimiter
import (
"fmt"
"sync"
"testing"
"time"
)
func TestAttributeMapGetSetDelete(t *testing.T) {
duration := 1 * time.Second
limit := 100
attributeLimiter := NewAttributeBasedLimiter(true)
// create a new key attribute:
// Example scenario, the rate-limiter
testKey1 := "/api/getArticle?id=10"
testKey2 := "/api/getArticle?id=20"
testKey3 := "/api/getArticle?id=30"
// check keys:
if attributeLimiter.HasKey(testKey1) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned true for non-existing key %s",
testKey1,
)
}
if attributeLimiter.HasKey(testKey2) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned true for non-existing key %s",
testKey2,
)
}
// create key:
if err := attributeLimiter.CreateNewKey(testKey1, uint64(limit), duration); err != nil {
t.Fatalf(
"AttributeBasedLimiter.CreateNewKey() failed, returned error on creating key %s, Error: %v\n",
testKey1, err,
)
}
if err := attributeLimiter.CreateNewKey(testKey2, uint64(limit), duration); err != nil {
t.Fatalf(
"AttributeBasedLimiter.CreateNewKey() failed, returned error on creating key %s, Error: %v\n",
testKey2, err,
)
}
// create an already existing key:
if err := attributeLimiter.CreateNewKey(testKey2, uint64(limit), duration); err == nil {
t.Fatalf(
"AttributeBasedLimiter.CreateNewKey() failed, did not return error when creating existing key %s\n",
testKey2,
)
}
// create key:
if ok := attributeLimiter.HasOrCreateKey(testKey3, uint64(limit), duration); !ok {
t.Fatalf(
"AttributeBasedLimiter.HasOrCreateKey() failed, returned false on creating key %s",
testKey3,
)
}
// create an already existing key:
if ok := attributeLimiter.HasOrCreateKey(testKey2, uint64(limit), duration); !ok {
t.Fatalf(
"AttributeBasedLimiter.HasOrCreateKey() failed, returned false for existing key %s",
testKey2,
)
}
// check existing keys:
if !attributeLimiter.HasKey(testKey1) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned false for existing key %s",
testKey1,
)
}
if !attributeLimiter.HasKey(testKey1) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned false for existing key %s",
testKey2,
)
}
// remove key
if err := attributeLimiter.DeleteKey(testKey1); err != nil {
t.Fatalf(
"AttributeBasedLimiter.DeleteKey() failed, returned error when removing existing key %s, Error: %v",
testKey1, err,
)
}
if err := attributeLimiter.DeleteKey(testKey2); err != nil {
t.Fatalf(
"AttributeBasedLimiter.DeleteKey() failed, returned error when removing existing key %s, Error: %v",
testKey2, err,
)
}
// check keys again:
if attributeLimiter.HasKey(testKey1) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned true for non-existing key %s",
testKey1,
)
}
if attributeLimiter.HasKey(testKey2) {
t.Fatalf(
"AttributeBasedLimiter.HasKey() failed, returned true for non-existing key %s",
testKey2,
)
}
// check ShouldAllow on non-existing key:
if _, err := attributeLimiter.ShouldAllow("noKey", 5); err == nil {
t.Fatalf(
"AttributeBasedLimiter.ShouldAllow() failed, did not return error when checking non-existing key.",
)
}
// check ShouldAllow on non-existing key:
if ok := attributeLimiter.MustShouldAllow("newKey", 5, uint64(limit), duration); !ok {
t.Fatalf(
"AttributeBasedLimiter.MustShouldAllow() failed, did not return false when checking non-existing key.",
)
}
// check ShouldAllow on existing key:
if _, err := attributeLimiter.ShouldAllow("newKey", 5); err != nil {
t.Fatalf(
"AttributeBasedLimiter.ShouldAllow() failed, did not return error when checking existing key.",
)
}
// check ShouldAllow on existing key:
if ok := attributeLimiter.MustShouldAllow("newKey", 5, uint64(limit), duration); !ok {
t.Fatalf(
"AttributeBasedLimiter.MustShouldAllow() failed, did not return false when checking non-existing key.",
)
}
// Remove the non-existing key:
if err := attributeLimiter.DeleteKey("noKey"); err == nil {
t.Fatalf(
"AttributeBasedLimiter.DeleteKey failed, did not return error when deleting non-existing key.",
)
}
}
func TestAttributeBasedLimiterAccuracy(t *testing.T) {
// number of unique keys to be tested
keys := []string{"/api/getArticle?id=10", "/api/getArticle?id=20"}
// key1 has limit of 100 hits/sec and key2 has 123 hits/sec allowed.
limits := []uint64{100, 123}
counters := make([]uint64, len(keys))
// per second window
duration := 1 * time.Second
// 10 samples will be executed.
nRuns := 5
// test with accuracy +/- 3, modify this variable to
// test accuracy for various error offsets, 0 is the most
// ideal case.
var allowanceRange uint64 = 15
sharedLimiter := NewAttributeBasedLimiter(true)
for idx, key := range keys {
err := sharedLimiter.CreateNewKey(key, limits[idx], duration)
if err != nil {
t.Fatalf("%v", err)
}
}
routine := func(key string, idx int, wg *sync.WaitGroup) {
defer wg.Done()
j := 0
counters[idx] = 0
for range time.Tick(2 * time.Millisecond) {
allowed, err := sharedLimiter.ShouldAllow(key, 1)
if err != nil {
break
}
if allowed {
counters[idx]++
}
j++
if j%500 == 0 {
break
}
}
}
// run for nRuns:
for i := 0; i < nRuns; i++ {
wg := sync.WaitGroup{}
for idx, key := range keys {
wg.Add(1)
go routine(key, idx, &wg)
}
wg.Wait()
// loop over the keys and check rate-limit:
for idx, count := range counters {
limit := limits[idx]
// check accuracy of counter
if (limit-allowanceRange) <= count && count <= (limit+allowanceRange) {
fmt.Printf(
"Iteration %d, Allowed tasks: %d, passed rate limiting accuracy test.\n",
i+1, count,
)
} else {
t.Fatalf(
"Accuracy test failed, expected results to be in +/- %d error range, but got %d",
allowanceRange, count,
)
}
}
}
}
func TestAttributeBasedLimiterAccuracySync(t *testing.T) {
// number of unique keys to be tested
keys := []string{"/api/getArticle?id=10", "/api/getArticle?id=20"}
// key1 has limit of 100 hits/sec and key2 has 123 hits/sec allowed.
limits := []uint64{100, 123}
counters := make([]uint64, len(keys))
// per second window
duration := 1 * time.Second
// 10 samples will be executed.
nRuns := 6
// test with accuracy +/- 3, modify this variable to
// test accuracy for various error offsets, 0 is the most
// ideal case.
var allowanceRange uint64 = 15
sharedLimiter := NewAttributeBasedLimiter(false)
isDry := true
for idx, key := range keys {
err := sharedLimiter.CreateNewKey(key, limits[idx], duration)
if err != nil {
t.Fatalf("%v", err)
}
}
routine := func(key string, idx int, wg *sync.WaitGroup) {
defer wg.Done()
j := 0
counters[idx] = 0
for range time.Tick(2 * time.Millisecond) {
allowed, err := sharedLimiter.ShouldAllow(key, 1)
if err != nil {
break
}
if allowed {
counters[idx]++
}
j++
if j%500 == 0 {
break
}
}
}
// run for nRuns:
for i := 0; i < nRuns; i++ {
wg := sync.WaitGroup{}
for idx, key := range keys {
wg.Add(1)
go routine(key, idx, &wg)
}
wg.Wait()
// loop over the keys and check rate-limit:
if !isDry {
for idx, count := range counters {
limit := limits[idx]
// check accuracy of counter
if (limit-allowanceRange) <= count && count <= (limit+allowanceRange) {
fmt.Printf(
"Iteration %d, Allowed tasks: %d, passed rate limiting accuracy test.\n",
i, count,
)
} else {
t.Fatalf(
"Accuracy test failed, expected results to be in +/- %d error range, but got %d",
allowanceRange, count,
)
}
}
}
isDry = false
}
}