-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcaddylimiter_test.go
95 lines (83 loc) · 2.55 KB
/
caddylimiter_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
package ratelimit
import (
"fmt"
"strconv"
"testing"
"time"
"golang.org/x/time/rate"
)
func TestAllowNAndRetryAfter(t *testing.T) {
tests := []struct {
keys []string
rule Rule
qps int
shouldRetryAfter time.Duration
shouldErr bool
expected bool
}{
{
[]string{"127.0.0.1", "get", "", "/"}, Rule{Methods: "", Status: "", Rate: 2, Burst: 2, Unit: "second"}, 2, 0, false, true,
},
{
[]string{"127.0.0.1", "get,post", "*", "/"}, Rule{Methods: "get,post", Status: "*", Rate: 1, Burst: 2, Unit: "minute"}, 2, 30 * time.Second, false, true,
},
{
[]string{"127.0.0.1", "*", "", "/"}, Rule{Methods: "*", Status: "", Rate: 1, Burst: 0, Unit: "hour"}, 1, rate.InfDuration, false, false,
},
{
[]string{"127.0.0.1", "", "*", "/"}, Rule{Methods: "", Status: "*", Rate: 0, Burst: 0}, 2, 0, false, true,
},
{
[]string{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6ImNhZGR5LXJhdGUtbGltaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.-i8APTDIVPfX4XjLQIEJH-GugwBoDizdUJjDgnOJpCI", "", "*", "/"}, Rule{Methods: "", Status: "*", Rate: 0, Burst: 0}, 2, 0, false, true,
},
}
for i, test := range tests {
test.keys = append(test.keys, strconv.Itoa(i))
actual := cl.AllowN(test.keys, test.rule, test.qps)
retryAfter := cl.RetryAfter(test.keys)
if retryAfter < test.shouldRetryAfter {
t.Errorf("E! test %d: shouldRetryAfter %d, got %d", i, test.shouldRetryAfter, retryAfter)
}
if actual != test.expected {
t.Errorf("E! test %d: expected %t, got %t", i, test.expected, actual)
}
}
// spawn multiple goroutines to test concurrent read/write in map
num := make([]int, 1000)
for range num {
go func() {
for {
cl.AllowN(tests[0].keys, tests[0].rule, tests[0].qps)
}
}()
go func() {
for {
cl.AllowN(tests[0].keys, tests[0].rule, tests[0].qps)
}
}()
}
}
func BenchmarkSingleKey(b *testing.B) {
keys := []string{"127.0.0.1", "get", "", "/"}
for i := 1; i <= 8; i *= 2 {
b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
benchmarkAllowNAndRetryAfter(keys)
}
})
}
}
func BenchmarkRandomKey(b *testing.B) {
for i := 1; i <= 8; i *= 2 {
b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
for n := 0; n < b.N; n++ {
keys := []string{"127.0.0.1", "get", "", "/" + strconv.Itoa(i) + "-" + strconv.Itoa(n)}
benchmarkAllowNAndRetryAfter(keys)
}
})
}
}
func benchmarkAllowNAndRetryAfter(keys []string) {
cl.AllowN(keys, Rule{Methods: "get", Status: "", Rate: 2, Burst: 2, Unit: "second"}, 1)
cl.RetryAfter(keys)
}