forked from coinpaprika/ratelimiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratelimiter_test.go
146 lines (138 loc) · 3.98 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
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
package ratelimiter
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRateLimiter_IsLimited(t *testing.T) {
tests := []struct {
name string
requestsLimit int64
windowSize time.Duration
incNumber int
wantLimitStatus *LimitStatus
}{
{
name: "test_RateLimiter_IsLimited_not_limited",
requestsLimit: int64(5),
windowSize: 10 * time.Second,
incNumber: 4,
wantLimitStatus: &LimitStatus{
IsLimited: false,
CurrentRate: 4,
},
},
{
name: "test_RateLimiter_IsLimited_limited",
requestsLimit: int64(5),
windowSize: 10 * time.Second,
incNumber: 6,
wantLimitStatus: &LimitStatus{
IsLimited: true,
CurrentRate: 6,
},
},
}
for _, tt := range tests {
store := NewMapLimitStore(1*time.Hour, 1*time.Hour)
r := New(store, tt.requestsLimit, tt.windowSize)
for i := 0; i < tt.incNumber; i++ {
err := r.Inc("key")
assert.NoError(t, err, tt.name)
}
limitStatus, err := r.Check("key")
assert.NoError(t, err, tt.name)
assert.Equal(t, tt.wantLimitStatus.IsLimited, limitStatus.IsLimited, tt.name)
assert.Equal(t, tt.wantLimitStatus.CurrentRate, limitStatus.CurrentRate, tt.name)
}
}
func TestRateLimiter_calcLimitDuration(t *testing.T) {
tests := []struct {
name string
prevValue int64
currValue int64
timeFromCurrWindow time.Duration
requestsLimit int64
windowSize time.Duration
want time.Duration
}{
{
name: "TestRateLimiter_calcLimitDuration_prev_value_is_not_zero",
prevValue: 5,
currValue: 6,
timeFromCurrWindow: 1 * time.Second,
requestsLimit: 5,
windowSize: 10 * time.Second,
want: time.Duration(11 * time.Second), // 10*(1.0-( (5-6)/5)) - 1
},
{
name: "TestRateLimiter_calcLimitDuration_prev_value_is_zero",
prevValue: 0,
currValue: 6,
timeFromCurrWindow: 1 * time.Second,
requestsLimit: 5,
windowSize: 10 * time.Second,
want: time.Duration(10666666666 * time.Nanosecond), // 10*(1.0-(5/6)) + (10-1)
},
}
for _, tt := range tests {
store := NewMapLimitStore(1*time.Hour, 1*time.Hour)
r := New(store, tt.requestsLimit, tt.windowSize)
dur := r.calcLimitDuration(tt.prevValue, tt.currValue, tt.timeFromCurrWindow)
assert.InDelta(t, tt.want, dur, 3)
}
}
func TestRateLimiter_calcRate(t *testing.T) {
tests := []struct {
name string
requestsLimit int64
windowSize time.Duration
timeFromCurrWindow time.Duration
prevValue int64
currentValue int64
want float64
}{
{
name: "TestRateLimiter_calcRate_prev_not_zero",
requestsLimit: 5,
windowSize: 10 * time.Second,
timeFromCurrWindow: 1 * time.Second,
prevValue: 5,
currentValue: 6,
want: (0.9 * 5) + 6.0,
},
{
name: "TestRateLimiter_calcRate_prev_zero",
requestsLimit: 5,
windowSize: 10 * time.Second,
timeFromCurrWindow: 1 * time.Second,
prevValue: 0,
currentValue: 6,
want: 6.0,
},
{
name: "TestRateLimiter_calcRate_timeFromCurrWindow_zero",
requestsLimit: 5,
windowSize: 10 * time.Second,
timeFromCurrWindow: 0 * time.Second,
prevValue: 5,
currentValue: 0,
want: 5.0,
},
{
name: "TestRateLimiter_calcRate_timeFromCurrWindow_max",
requestsLimit: 5,
windowSize: 10 * time.Second,
timeFromCurrWindow: 10 * time.Second,
prevValue: 5,
currentValue: 6,
want: 6.0,
},
}
for _, tt := range tests {
store := NewMapLimitStore(1*time.Hour, 1*time.Hour)
r := New(store, tt.requestsLimit, tt.windowSize)
rate := r.calcRate(tt.timeFromCurrWindow, tt.prevValue, tt.currentValue)
assert.Equal(t, tt.want, rate)
}
}