@@ -10,30 +10,26 @@ import (
10
10
"time"
11
11
12
12
"github.com/mgnsk/evcache/v3"
13
- . "github.com/onsi/gomega "
13
+ . "github.com/mgnsk/evcache/v3/internal/testing "
14
14
)
15
15
16
16
func TestLoadOrStoreNotExists (t * testing.T ) {
17
- g := NewWithT (t )
18
-
19
17
c := evcache.New [string , int ](0 )
20
18
21
19
_ , loaded := c .LoadOrStore ("key" , 0 , 1 )
22
- g . Expect ( loaded ). To ( BeFalse () )
23
- g . Expect ( c .Exists ("key" )). To ( BeTrue () )
24
- g . Expect ( c .Len ()). To ( Equal ( 1 ) )
20
+ AssertEqual ( t , loaded , false )
21
+ AssertEqual ( t , c .Exists ("key" ), true )
22
+ AssertEqual ( t , c .Len (), 1 )
25
23
}
26
24
27
25
func TestLoadOrStoreExists (t * testing.T ) {
28
- g := NewWithT (t )
29
-
30
26
c := evcache.New [string , int ](0 )
31
27
32
28
c .LoadOrStore ("key" , 0 , 1 )
33
29
34
30
v , loaded := c .LoadOrStore ("key" , 0 , 2 )
35
- g . Expect ( loaded ). To ( BeTrue () )
36
- g . Expect ( v ). To ( Equal ( 1 ) )
31
+ AssertEqual ( t , loaded , true )
32
+ AssertEqual ( t , v , 1 )
37
33
}
38
34
39
35
func TestFetchCallbackBlocks (t * testing.T ) {
@@ -54,62 +50,50 @@ func TestFetchCallbackBlocks(t *testing.T) {
54
50
<- fetchStarted
55
51
56
52
t .Run ("non-blocking Exists" , func (t * testing.T ) {
57
- g := NewWithT (t )
58
-
59
- g .Expect (c .Exists ("key" )).To (BeFalse ())
53
+ AssertEqual (t , c .Exists ("key" ), false )
60
54
})
61
55
62
56
t .Run ("non-blocking Evict" , func (t * testing.T ) {
63
- g := NewWithT (t )
64
-
65
57
_ , ok := c .Evict ("key" )
66
- g . Expect ( ok ). To ( BeFalse () )
58
+ AssertEqual ( t , ok , false )
67
59
})
68
60
69
61
t .Run ("non-blocking Get" , func (t * testing.T ) {
70
- g := NewWithT (t )
71
-
72
62
_ , ok := c .Get ("key" )
73
- g . Expect ( ok ). To ( BeFalse () )
63
+ AssertEqual ( t , ok , false )
74
64
})
75
65
76
66
t .Run ("autoexpiry for other keys works" , func (t * testing.T ) {
77
- g := NewWithT (t )
78
-
79
67
c .LoadOrStore ("key1" , time .Millisecond , "value1" )
80
68
81
- g . Eventually ( func () bool {
82
- return c .Exists ("key1" )
83
- }). Should ( BeFalse ())
69
+ AssertEventuallyTrue ( t , func () bool {
70
+ return ! c .Exists ("key1" )
71
+ })
84
72
})
85
73
86
74
t .Run ("non-blocking Range" , func (t * testing.T ) {
87
- g := NewWithT (t )
88
-
89
75
c .LoadOrStore ("key1" , 0 , "value1" )
90
76
91
77
n := 0
92
78
c .Range (func (key , value string ) bool {
93
79
if key == "key" {
94
- g . Fail ("expected to skip key" )
80
+ t . Fatal ("expected to skip key" )
95
81
}
96
82
97
83
v , ok := c .Evict (key )
98
- g . Expect ( ok ). To ( BeTrue () )
99
- g . Expect ( v ). To ( Equal ( value ) )
100
- g . Expect ( c .Len ()). To ( Equal ( 0 ) )
84
+ AssertEqual ( t , ok , true )
85
+ AssertEqual ( t , v , value )
86
+ AssertEqual ( t , c .Len (), 0 )
101
87
102
88
n ++
103
89
return true
104
90
})
105
91
106
- g . Expect ( n ). To ( Equal ( 1 ) )
92
+ AssertEqual ( t , n , 1 )
107
93
})
108
94
}
109
95
110
96
func TestFetchCallbackPanic (t * testing.T ) {
111
- g := NewWithT (t )
112
-
113
97
c := evcache.New [string , string ](0 )
114
98
115
99
func () {
@@ -127,14 +111,12 @@ func TestFetchCallbackPanic(t *testing.T) {
127
111
return "new value" , nil
128
112
})
129
113
130
- g . Expect ( err ). NotTo ( HaveOccurred () )
131
- g . Expect ( v ). To ( Equal ( "new value" ) )
114
+ AssertSuccess ( t , err )
115
+ AssertEqual ( t , v , "new value" )
132
116
}
133
117
134
118
func TestConcurrentFetch (t * testing.T ) {
135
119
t .Run ("returns error" , func (t * testing.T ) {
136
- g := NewWithT (t )
137
-
138
120
c := evcache.New [string , string ](0 )
139
121
140
122
errCh := make (chan error )
@@ -154,13 +136,11 @@ func TestConcurrentFetch(t *testing.T) {
154
136
return "value" , nil
155
137
})
156
138
157
- g . Expect ( err ). NotTo ( HaveOccurred () )
158
- g . Expect ( v ). To ( Equal ( "value" ) )
139
+ AssertSuccess ( t , err )
140
+ AssertEqual ( t , v , "value" )
159
141
})
160
142
161
143
t .Run ("returns value" , func (t * testing.T ) {
162
- g := NewWithT (t )
163
-
164
144
c := evcache.New [string , string ](0 )
165
145
166
146
valueCh := make (chan string )
@@ -180,40 +160,37 @@ func TestConcurrentFetch(t *testing.T) {
180
160
return "value1" , nil
181
161
})
182
162
183
- g . Expect ( err ). NotTo ( HaveOccurred () )
184
- g . Expect ( v ). To ( Equal ( "value" ) )
163
+ AssertSuccess ( t , err )
164
+ AssertEqual ( t , v , "value" )
185
165
})
186
166
}
187
167
188
168
func TestEvict (t * testing.T ) {
189
- g := NewWithT (t )
190
-
191
169
c := evcache.New [string , string ](0 )
192
170
193
171
c .LoadOrStore ("key" , 0 , "value" )
194
172
195
173
v , ok := c .Evict ("key" )
196
- g .Expect (ok ).To (BeTrue ())
197
- g .Expect (v ).To (Equal ("value" ))
198
- g .Expect (c .Exists ("key" )).To (BeFalse ())
174
+
175
+ AssertEqual (t , ok , true )
176
+ AssertEqual (t , v , "value" )
177
+ AssertEqual (t , c .Exists ("key" ), false )
199
178
}
200
179
201
180
func TestOverflow (t * testing.T ) {
202
- g := NewWithT (t )
203
-
204
181
capacity := 100
205
182
c := evcache.New [int , int ](capacity )
206
183
207
184
for i := 0 ; i < 2 * capacity ; i ++ {
208
185
c .LoadOrStore (i , 0 , 0 )
209
186
}
210
187
211
- g .Eventually (c .Len ).Should (Equal (capacity ))
188
+ AssertEventuallyTrue (t , func () bool {
189
+ return c .Len () == capacity
190
+ })
212
191
}
213
192
214
193
func TestExpire (t * testing.T ) {
215
- g := NewWithT (t )
216
-
217
194
c := evcache.New [int , int ](0 )
218
195
219
196
n := 10
@@ -223,33 +200,35 @@ func TestExpire(t *testing.T) {
223
200
_ , _ = c .LoadOrStore (i , d , 0 )
224
201
}
225
202
226
- g .Eventually (c .Len ).Should (BeZero ())
203
+ AssertEventuallyTrue (t , func () bool {
204
+ return c .Len () == 0
205
+ })
227
206
}
228
207
229
208
func TestExpireEdgeCase (t * testing.T ) {
230
- g := NewWithT (t )
231
-
232
209
c := evcache.New [int , * string ](0 )
233
210
234
211
v1 := new (string )
235
212
236
213
c .LoadOrStore (0 , 10 * time .Millisecond , v1 )
237
214
238
215
// Wait until v1 expires.
239
- g .Eventually (c .Len ).Should (BeZero ())
216
+ AssertEventuallyTrue (t , func () bool {
217
+ return c .Len () == 0
218
+ })
240
219
241
220
// Assert that after v1 expires, v2 with a longer TTL than v1, can expire,
242
221
// specifically that runGC() resets earliestExpireAt to zero,
243
222
// so that LoadOrStore schedules the GC loop.
244
223
v2 := new (string )
245
224
c .LoadOrStore (1 , time .Second , v2 )
246
225
247
- g .Eventually (c .Len , 2 * time .Second ).Should (BeZero ())
226
+ AssertEventuallyTrue (t , func () bool {
227
+ return c .Len () == 0
228
+ }, 2 * time .Second )
248
229
}
249
230
250
231
func TestOverflowEvictionOrdering (t * testing.T ) {
251
- g := NewWithT (t )
252
-
253
232
capacity := 10
254
233
c := evcache.New [int , * string ](capacity )
255
234
evictedKeys := make (chan int )
@@ -263,19 +242,19 @@ func TestOverflowEvictionOrdering(t *testing.T) {
263
242
})
264
243
265
244
_ , loaded := c .LoadOrStore (i , 0 , value )
266
- g . Expect ( loaded ). To ( BeFalse () )
267
- g . Expect ( c .Len ()). To ( Equal ( i ) )
245
+ AssertEqual ( t , loaded , false )
246
+ AssertEqual ( t , c .Len (), i )
268
247
}
269
248
270
249
// Overflow the cache and catch evicted keys.
271
250
var keys []int
272
251
for i := capacity + 1 ; i <= 2 * capacity ; i ++ {
273
252
_ , loaded := c .LoadOrStore (i , 0 , nil )
274
- g . Expect ( loaded ). To ( BeFalse () )
253
+ AssertEqual ( t , loaded , false )
275
254
276
255
// Run the GC until the value is garbage collected
277
256
// and the value finalizer runs.
278
- g . Eventually ( func () bool {
257
+ AssertEventuallyTrue ( t , func () bool {
279
258
runtime .GC ()
280
259
select {
281
260
case key := <- evictedKeys :
@@ -284,11 +263,11 @@ func TestOverflowEvictionOrdering(t *testing.T) {
284
263
default :
285
264
return false
286
265
}
287
- }). Should ( BeTrue ())
266
+ })
288
267
}
289
268
290
- g . Expect ( keys ). To ( HaveLen ( capacity ) )
291
- g . Expect ( sort .IntsAreSorted (keys )). To ( BeTrue () )
269
+ AssertEqual ( t , len ( keys ), capacity )
270
+ AssertEqual ( t , sort .IntsAreSorted (keys ), true )
292
271
}
293
272
294
273
func BenchmarkFetchAndEvictParallel (b * testing.B ) {
0 commit comments