-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththrottlers_manager_test.go
60 lines (49 loc) · 1.28 KB
/
throttlers_manager_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
package throttlers
import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/time/rate"
)
func TestIsThrottledAllowsRequestsUptoStartingRatePerHost(t *testing.T) {
throttles := New(Params{
StartingRate: 1,
Burst: 1,
})
assert.False(t, throttles.IsThrottled("key"))
assert.True(t, throttles.IsThrottled("key"))
assert.False(t, throttles.IsThrottled("new key"))
}
func TestIncrIncrementLimitPerHost(t *testing.T) {
throttles := New(Params{
StartingRate: 0,
Burst: 1,
Increment: 1,
UpperBound: 3,
})
expectedLimits := []uint64{0, 1, 2, 3, 3, 3}
for _, expectedLimit := range expectedLimits {
assert.Equal(t, rate.Limit(expectedLimit), throttles.getThrottler("key").limiter.Limit())
throttles.Incr("key")
}
}
func TestDecrDecrementLimitPerHost(t *testing.T) {
throttles := New(Params{
StartingRate: 3,
Burst: 1,
Decrement: 1,
LowerBound: 1,
})
expectedLimits := []uint64{3, 2, 1, 1, 1}
for _, expectedLimit := range expectedLimits {
assert.Equal(t, rate.Limit(expectedLimit), throttles.getThrottler("key").limiter.Limit())
throttles.Decr("key")
}
}
func TestCancelWaitPerHost(t *testing.T) {
// allow no request
throttles := New(Params{
StartingRate: 0,
})
go throttles.CancelWait("key")
throttles.Wait("key")
}