forked from myzhan/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratelimiter_test.go
117 lines (101 loc) · 2.9 KB
/
ratelimiter_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
package boomer
import (
"testing"
"time"
)
func TestStableRateLimiter(t *testing.T) {
rateLimiter := NewStableRateLimiter(1, 10*time.Millisecond)
rateLimiter.Start()
defer rateLimiter.Stop()
blocked := rateLimiter.Acquire()
if blocked {
t.Error("Unexpected blocked by rate limiter")
}
blocked = rateLimiter.Acquire()
if !blocked {
t.Error("Should be blocked")
}
}
func TestStableRateLimiterStartAndStopManyTimes(t *testing.T) {
ratelimiter := NewStableRateLimiter(100, 5*time.Millisecond)
for i := 0; i < 500; i++ {
ratelimiter.Start()
time.Sleep(5 * time.Millisecond)
ratelimiter.Stop()
}
}
func TestRampUpRateLimiter(t *testing.T) {
rateLimiter, _ := NewRampUpRateLimiter(100, "10/200ms", 100*time.Millisecond)
rateLimiter.Start()
defer rateLimiter.Stop()
for i := 0; i < 10; i++ {
blocked := rateLimiter.Acquire()
if blocked {
t.Error("Unexpected blocked by rate limiter")
}
}
blocked := rateLimiter.Acquire()
if !blocked {
t.Error("Should be blocked")
}
time.Sleep(210 * time.Millisecond)
// now, the threshold is 20
for i := 0; i < 20; i++ {
blocked := rateLimiter.Acquire()
if blocked {
t.Error("Unexpected blocked by rate limiter")
}
}
blocked = rateLimiter.Acquire()
if !blocked {
t.Error("Should be blocked")
}
}
func TestRampUpRateLimiterStartAndStopManyTimes(t *testing.T) {
ratelimiter, _ := NewRampUpRateLimiter(100, "10/200ms", 100*time.Millisecond)
for i := 0; i < 500; i++ {
ratelimiter.Start()
time.Sleep(5 * time.Millisecond)
ratelimiter.Stop()
}
}
func TestParseRampUpRate(t *testing.T) {
rateLimiter := &RampUpRateLimiter{}
rampUpStep, rampUpPeriod, _ := rateLimiter.parseRampUpRate("100")
if rampUpStep != 100 {
t.Error("Wrong rampUpStep, expected: 100, was:", rampUpStep)
}
if rampUpPeriod != time.Second {
t.Error("Wrong rampUpPeriod, expected: 1s, was:", rampUpPeriod)
}
rampUpStep, rampUpPeriod, _ = rateLimiter.parseRampUpRate("200/10s")
if rampUpStep != 200 {
t.Error("Wrong rampUpStep, expected: 200, was:", rampUpStep)
}
if rampUpPeriod != 10*time.Second {
t.Error("Wrong rampUpPeriod, expected: 10s, was:", rampUpPeriod)
}
}
func TestParseInvalidRampUpRate(t *testing.T) {
rateLimiter := &RampUpRateLimiter{}
_, _, err := rateLimiter.parseRampUpRate("A/1m")
if err == nil || err != ErrParsingRampUpRate {
t.Error("Expected ErrParsingRampUpRate")
}
_, _, err = rateLimiter.parseRampUpRate("A")
if err == nil || err != ErrParsingRampUpRate {
t.Error("Expected ErrParsingRampUpRate")
}
_, _, err = rateLimiter.parseRampUpRate("200/1s/")
if err == nil || err != ErrParsingRampUpRate {
t.Error("Expected ErrParsingRampUpRate")
}
_, _, err = rateLimiter.parseRampUpRate("200/1")
if err == nil || err != ErrParsingRampUpRate {
t.Error("Expected ErrParsingRampUpRate")
}
_, err = NewRampUpRateLimiter(1, "200/1", time.Second)
if err == nil || err != ErrParsingRampUpRate {
t.Error("Expected ErrParsingRampUpRate")
}
}