-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathblocker_test.go
More file actions
38 lines (31 loc) · 798 Bytes
/
blocker_test.go
File metadata and controls
38 lines (31 loc) · 798 Bytes
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
package ratelimit
import (
"testing"
"time"
"aahframework.org/test.v0/assert"
"github.com/bouk/monkey"
)
func Test_it_block_as_expected(t *testing.T) {
l1 := CreateLimit("1r/s,spam:3,block:2d")
key := "127.0.0.1"
l1.Hit(key)
l1.Hit(key)
time.Sleep(1 * time.Second)
l1.Hit(key)
l1.Hit(key)
time.Sleep(1 * time.Second)
l1.Hit(key)
l1.Hit(key)
assert.NotNil(t, l1.Blocker.Values[key])
}
func Test_it_clears_block_after_expeced_duration(t *testing.T) {
l1 := CreateLimit("1r/s,spam:3,block:2d")
key := "127.0.0.1"
l1.Hit(key)
l1.Hit(key)
travelTowDayWithOneHour := time.Now().Add(49 * time.Hour)
patch := monkey.Patch(time.Now, func() time.Time { return travelTowDayWithOneHour })
defer patch.Unpatch()
time.Sleep(2 * time.Second)
assert.Nil(t, l1.Blocker.Values[key])
}