-
Notifications
You must be signed in to change notification settings - Fork 10
/
hashers_test.go
134 lines (96 loc) · 2.86 KB
/
hashers_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
package speedbump
import (
"fmt"
"testing"
"time"
"github.com/facebookgo/clock"
"github.com/stretchr/testify/assert"
)
func Test_PerSecond_Hash(t *testing.T) {
mock := clock.NewMock()
hasher := PerSecondHasher{
Clock: mock,
}
resultOne := hasher.Hash("127.0.0.1")
mock.Add(time.Second)
resultTwo := hasher.Hash("127.0.0.1")
assert.NotEqual(t, resultOne, resultTwo)
resultThree := hasher.Hash("127.0.0.1")
resultFour := hasher.Hash("127.0.0.1")
resultFive := hasher.Hash("127.0.0.2")
assert.Equal(t, resultThree, resultFour)
assert.NotEqual(t, resultFour, resultFive)
// Test that it can create a new clock
hasher = PerSecondHasher{}
hasher.Hash("127.0.0.1")
}
// The following example shows how to create mock hashers for testing the rate
// limiter in your code:
func ExamplePerSecondHasher() {
// Create a mock clock.
mock := clock.NewMock()
// Create a new per second hasher with the mock clock.
hasher := PerMinuteHasher{
Clock: mock,
}
// Generate two consecutive hashes. On most systems, the following should
// generate two identical hashes.
hashOne := hasher.Hash("127.0.0.1")
hashTwo := hasher.Hash("127.0.0.1")
// Now we push the clock forward by a minute (time travel).
mock.Add(time.Minute)
// The third hash should be different now.
hashThree := hasher.Hash("127.0.0.1")
fmt.Println(hashOne == hashTwo)
fmt.Println(hashOne == hashThree)
// Output: true
// false
}
func Test_PerSecond_Duration(t *testing.T) {
hasher := PerSecondHasher{}
assert.Equal(t, time.Second, hasher.Duration())
}
func Test_PerMinute_Hash(t *testing.T) {
mock := clock.NewMock()
hasher := PerMinuteHasher{
Clock: mock,
}
resultOne := hasher.Hash("127.0.0.1")
mock.Add(time.Minute)
resultTwo := hasher.Hash("127.0.0.1")
assert.NotEqual(t, resultOne, resultTwo)
resultThree := hasher.Hash("127.0.0.1")
resultFour := hasher.Hash("127.0.0.1")
resultFive := hasher.Hash("127.0.0.2")
assert.Equal(t, resultThree, resultFour)
assert.NotEqual(t, resultFour, resultFive)
// Test that it can create a new clock
hasher = PerMinuteHasher{}
hasher.Hash("127.0.0.1")
}
func Test_PerMinute_Duration(t *testing.T) {
hasher := PerMinuteHasher{}
assert.Equal(t, time.Minute, hasher.Duration())
}
func Test_PerHour_Hash(t *testing.T) {
mock := clock.NewMock()
hasher := PerHourHasher{
Clock: mock,
}
resultOne := hasher.Hash("127.0.0.1")
mock.Add(time.Hour)
resultTwo := hasher.Hash("127.0.0.1")
assert.NotEqual(t, resultOne, resultTwo)
resultThree := hasher.Hash("127.0.0.1")
resultFour := hasher.Hash("127.0.0.1")
resultFive := hasher.Hash("127.0.0.2")
assert.Equal(t, resultThree, resultFour)
assert.NotEqual(t, resultFour, resultFive)
// Test that it can create a new clock
hasher = PerHourHasher{}
hasher.Hash("127.0.0.1")
}
func Test_PerHour_Duration(t *testing.T) {
hasher := PerHourHasher{}
assert.Equal(t, time.Hour, hasher.Duration())
}