-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththrottler_test.go
58 lines (48 loc) · 1.19 KB
/
throttler_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
package throttlers
import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/time/rate"
)
func TestIsThrottledAllowsRequestsUptoStartingRate(t *testing.T) {
throttle := newThrottler(paramThrottler{
StartingRate: 1,
Burst: 1,
})
assert.False(t, throttle.IsThrottled())
assert.True(t, throttle.IsThrottled())
}
func TestIncrIncrementLimit(t *testing.T) {
throttle := newThrottler(paramThrottler{
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), throttle.limiter.Limit())
throttle.Incr()
}
}
func TestDecrDecrementLimit(t *testing.T) {
throttle := newThrottler(paramThrottler{
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), throttle.limiter.Limit())
throttle.Decr()
}
}
func TestCancelWait(t *testing.T) {
// allow no request
throttle := newThrottler(paramThrottler{
StartingRate: 0,
})
go throttle.CancelWait()
throttle.Wait()
}