-
Notifications
You must be signed in to change notification settings - Fork 104
/
rate_limit_test.go
50 lines (44 loc) · 2.71 KB
/
rate_limit_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
package dnc
import (
"slices"
"testing"
"time"
)
/*
TestRateLimiter tests solution(s) with the following signature and problem description:
func IsAllowed(limitPerSecond int) bool
Given a number of allowed requests calls per second (calls/time) write an IsAllowed
function which returns false if the request should be rate limited because it exceeds the
limit and true if the request should be allowed.
*/
func TestRateLimiter(t *testing.T) {
tests := []struct {
limitPerSecond int
firstCallTimes int
sleep int
secondCallTimes int
want []bool
}{
{0, 0, 0, 0, []bool{}},
{0, 1, 0, 0, []bool{false}},
{0, 1, 0, 1, []bool{false, false}},
{1, 2, 0, 2, []bool{true, false, false, false}},
{2, 5, 0, 5, []bool{true, true, false, false, false, false, false, false, false, false}},
{3, 5, 1, 5, []bool{true, true, true, false, false, true, true, true, false, false}},
{10, 100, 1, 100, []bool{true, true, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}},
}
for i, test := range tests {
rateLimitEvents = make([]int64, 0)
got := make([]bool, 0)
for range test.firstCallTimes {
got = append(got, IsAllowed(test.limitPerSecond))
}
time.Sleep(time.Duration(test.sleep) * time.Second)
for range test.secondCallTimes {
got = append(got, IsAllowed(test.limitPerSecond))
}
if !slices.Equal(got, test.want) {
t.Fatalf("Failed test case #%d. Want %v got %v", i, test.want, got)
}
}
}