5
5
"testing"
6
6
"time"
7
7
8
- "github.com/stretchr/testify/require "
8
+ "fortio.org/assert "
9
9
)
10
10
11
11
const noEvictionTTL = 0
@@ -15,28 +15,28 @@ func TestSetAndGet(t *testing.T) {
15
15
cache .Set ("hello" , "world" )
16
16
17
17
value , ok := cache .Get ("hello" )
18
- require .True (t , ok )
19
- require .Equal (t , "world" , value )
18
+ assert .True (t , ok )
19
+ assert .Equal (t , "world" , value )
20
20
}
21
21
22
22
func TestRemove (t * testing.T ) {
23
23
cache := New [int , int ](10 , noEvictionTTL )
24
24
cache .Set (1 , 10 )
25
25
26
26
val , ok := cache .Get (1 )
27
- require .True (t , ok )
28
- require .Equal (t , 10 , val )
27
+ assert .True (t , ok )
28
+ assert .Equal (t , 10 , val )
29
29
30
30
// After removing the key, it should not be found
31
31
removed := cache .Remove (1 )
32
- require .True (t , removed )
32
+ assert .True (t , removed )
33
33
34
34
_ , ok = cache .Get (1 )
35
- require .False (t , ok )
35
+ assert .False (t , ok )
36
36
37
37
// This should not panic
38
38
removed = cache .Remove (- 1 )
39
- require .False (t , removed )
39
+ assert .False (t , removed )
40
40
}
41
41
42
42
func TestEvictOneHitWonders (t * testing.T ) {
@@ -55,14 +55,14 @@ func TestEvictOneHitWonders(t *testing.T) {
55
55
// hit one-hit wonders only once
56
56
for _ , v := range oneHitWonders {
57
57
_ , ok := cache .Get (v )
58
- require .True (t , ok )
58
+ assert .True (t , ok )
59
59
}
60
60
61
61
// hit the popular objects
62
62
for i := 0 ; i < 3 ; i ++ {
63
63
for _ , v := range popularObjects {
64
64
_ , ok := cache .Get (v )
65
- require .True (t , ok )
65
+ assert .True (t , ok )
66
66
}
67
67
}
68
68
@@ -74,13 +74,13 @@ func TestEvictOneHitWonders(t *testing.T) {
74
74
75
75
for _ , v := range oneHitWonders {
76
76
_ , ok := cache .Get (v )
77
- require .False (t , ok )
77
+ assert .False (t , ok )
78
78
}
79
79
80
80
// popular objects should still be in the cache
81
81
for _ , v := range popularObjects {
82
82
_ , ok := cache .Get (v )
83
- require .True (t , ok )
83
+ assert .True (t , ok )
84
84
}
85
85
}
86
86
@@ -97,8 +97,8 @@ func TestPeek(t *testing.T) {
97
97
for i := 0 ; i < 10 ; i ++ {
98
98
for _ , v := range entries {
99
99
value , exist := cache .Peek (v )
100
- require .True (t , exist )
101
- require .Equal (t , v * 10 , value )
100
+ assert .True (t , exist )
101
+ assert .Equal (t , v * 10 , value )
102
102
}
103
103
}
104
104
@@ -112,7 +112,7 @@ func TestPeek(t *testing.T) {
112
112
// they should not exist in the cache
113
113
for _ , v := range entries {
114
114
_ , exist := cache .Peek (v )
115
- require .False (t , exist )
115
+ assert .False (t , exist )
116
116
}
117
117
}
118
118
@@ -126,27 +126,27 @@ func TestContains(t *testing.T) {
126
126
127
127
// check if each entry exists in the cache
128
128
for _ , v := range entries {
129
- require .True (t , cache .Contains (v ))
129
+ assert .True (t , cache .Contains (v ))
130
130
}
131
131
132
132
for i := 6 ; i <= 10 ; i ++ {
133
- require .False (t , cache .Contains (i ))
133
+ assert .False (t , cache .Contains (i ))
134
134
}
135
135
}
136
136
137
137
func TestLength (t * testing.T ) {
138
138
cache := New [string , string ](10 , noEvictionTTL )
139
139
140
140
cache .Set ("hello" , "world" )
141
- require .Equal (t , 1 , cache .Len ())
141
+ assert .Equal (t , 1 , cache .Len ())
142
142
143
143
cache .Set ("hello2" , "world" )
144
144
cache .Set ("hello" , "changed" )
145
- require .Equal (t , 2 , cache .Len ())
145
+ assert .Equal (t , 2 , cache .Len ())
146
146
147
147
value , ok := cache .Get ("hello" )
148
- require .True (t , ok )
149
- require .Equal (t , "changed" , value )
148
+ assert .True (t , ok )
149
+ assert .Equal (t , "changed" , value )
150
150
}
151
151
152
152
func TestClean (t * testing.T ) {
@@ -156,15 +156,15 @@ func TestClean(t *testing.T) {
156
156
for _ , v := range entries {
157
157
cache .Set (v , v * 10 )
158
158
}
159
- require .Equal (t , 5 , cache .Len ())
159
+ assert .Equal (t , 5 , cache .Len ())
160
160
cache .Purge ()
161
161
162
162
// check if each entry exists in the cache
163
163
for _ , v := range entries {
164
164
_ , exist := cache .Peek (v )
165
- require .False (t , exist )
165
+ assert .False (t , exist )
166
166
}
167
- require .Equal (t , 0 , cache .Len ())
167
+ assert .Equal (t , 0 , cache .Len ())
168
168
}
169
169
170
170
func TestTimeToLive (t * testing.T ) {
@@ -175,16 +175,16 @@ func TestTimeToLive(t *testing.T) {
175
175
for num := 1 ; num <= numberOfEntries ; num ++ {
176
176
cache .Set (num , num )
177
177
val , ok := cache .Get (num )
178
- require .True (t , ok )
179
- require .Equal (t , num , val )
178
+ assert .True (t , ok )
179
+ assert .Equal (t , num , val )
180
180
}
181
181
182
182
time .Sleep (ttl * 2 )
183
183
184
184
// check all entries are evicted
185
185
for num := 1 ; num <= numberOfEntries ; num ++ {
186
186
_ , ok := cache .Get (num )
187
- require .False (t , ok )
187
+ assert .False (t , ok )
188
188
}
189
189
}
190
190
@@ -206,8 +206,8 @@ func TestEvictionCallback(t *testing.T) {
206
206
207
207
// check the first object is evicted
208
208
_ , ok := cache .Get (1 )
209
- require .False (t , ok )
210
- require .Equal (t , 1 , evicted [1 ])
209
+ assert .False (t , ok )
210
+ assert .Equal (t , 1 , evicted [1 ])
211
211
212
212
cache .Close ()
213
213
}
@@ -237,7 +237,7 @@ func TestEvictionCallbackWithTTL(t *testing.T) {
237
237
mu .Lock ()
238
238
if len (evicted ) == 10 {
239
239
for i := 1 ; i <= 10 ; i ++ {
240
- require .Equal (t , i , evicted [i ])
240
+ assert .Equal (t , i , evicted [i ])
241
241
}
242
242
return
243
243
}
0 commit comments