-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolimit_test.go
71 lines (51 loc) · 1.17 KB
/
golimit_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
package golimit
import (
"testing"
"time"
)
func TestBan(t *testing.T) {
lim := NewLimiter(1, 1)
if lim.IsBanned("user") {
t.Error("User should not be banned")
}
if lim.BannedCount() != 0 {
t.Error("Banned count should be 0")
}
lim.Ban("user", time.Second*1)
if !lim.IsBanned("user") {
t.Error("User should be banned")
}
if lim.BannedCount() != 1 {
t.Error("Banned count should be 1")
}
}
func TestClean(t *testing.T) {
lim := NewLimiter(1*time.Second, 2)
lim.Allow("id")
lim.Allow("id")
lim.clean(time.Now())
if len(lim.Visitors) != 1 {
t.Error("Should not be cleaned")
}
time.Sleep(time.Second * 1)
lim.clean(time.Now())
if len(lim.Visitors) != 0 {
t.Error("Should be cleaned")
}
}
func TestAllow(t *testing.T) {
lim := NewLimiter(1*time.Second, 2)
if lim.Allow("id user") != true {
t.Error("Should be allowed at first time")
}
if lim.Allow("id user") != true {
t.Error("Should be allowed 2 messages in a second")
}
if lim.Allow("id user") != false {
t.Error("Should be blocked on third message in a second")
}
time.Sleep(1 * time.Second)
if lim.Allow("id user") != true {
t.Error("Should be allowed after a second sleep")
}
}