@@ -18,8 +18,8 @@ type mockPersister[K comparable, V any] struct {
1818}
1919
2020type mockEntry [V any ] struct {
21- value V
22- expiry time.Time
21+ value V
22+ expiry time.Time
2323 updatedAt time.Time
2424}
2525
@@ -33,7 +33,7 @@ func (m *mockPersister[K, V]) ValidateKey(key K) error {
3333 return nil
3434}
3535
36- func (m * mockPersister [K , V ]) Load (ctx context.Context , key K ) (V , time.Time , bool , error ) {
36+ func (m * mockPersister [K , V ]) Load (ctx context.Context , key K ) (v V , expiry time.Time , found bool , err error ) {
3737 m .mu .RLock ()
3838 defer m .mu .RUnlock ()
3939
@@ -166,7 +166,7 @@ func TestCache_WithPersistence(t *testing.T) {
166166 if err != nil {
167167 t .Fatalf ("New: %v" , err )
168168 }
169- defer cache .Close ()
169+ defer func () { _ = cache .Close () } ()
170170
171171 // Set should persist
172172 if err := cache .Set (ctx , "key1" , 42 , 0 ); err != nil {
@@ -202,13 +202,13 @@ func TestCache_GetFromPersistence(t *testing.T) {
202202 persister := newMockPersister [string , int ]()
203203
204204 // Pre-populate persistence
205- persister .Store (ctx , "key1" , 42 , time.Time {})
205+ _ = persister .Store (ctx , "key1" , 42 , time.Time {})
206206
207207 cache , err := New [string , int ](ctx , WithPersistence (persister ))
208208 if err != nil {
209209 t .Fatalf ("New: %v" , err )
210210 }
211- defer cache .Close ()
211+ defer func () { _ = cache .Close () } ()
212212
213213 // Get should load from persistence
214214 val , found , err := cache .Get (ctx , "key1" )
@@ -228,13 +228,13 @@ func TestCache_GetFromPersistenceExpired(t *testing.T) {
228228 persister := newMockPersister [string , int ]()
229229
230230 // Pre-populate with expired entry
231- persister .Store (ctx , "key1" , 42 , time .Now ().Add (- 1 * time .Hour ))
231+ _ = persister .Store (ctx , "key1" , 42 , time .Now ().Add (- 1 * time .Hour ))
232232
233233 cache , err := New [string , int ](ctx , WithPersistence (persister ))
234234 if err != nil {
235235 t .Fatalf ("New: %v" , err )
236236 }
237- defer cache .Close ()
237+ defer func () { _ = cache .Close () } ()
238238
239239 // Get should return not found for expired entry
240240 _ , found , err := cache .Get (ctx , "key1" )
@@ -252,7 +252,7 @@ func TestCache_WithWarmup(t *testing.T) {
252252
253253 // Pre-populate persistence with 10 items
254254 for i := range 10 {
255- persister .Store (ctx , fmt .Sprintf ("key%d" , i ), i , time.Time {})
255+ _ = persister .Store (ctx , fmt .Sprintf ("key%d" , i ), i , time.Time {})
256256 }
257257
258258 // Create cache with warmup limit of 5
@@ -262,7 +262,7 @@ func TestCache_WithWarmup(t *testing.T) {
262262 if err != nil {
263263 t .Fatalf ("New: %v" , err )
264264 }
265- defer cache .Close ()
265+ defer func () { _ = cache .Close () } ()
266266
267267 // Give warmup goroutine time to complete
268268 time .Sleep (100 * time .Millisecond )
@@ -282,9 +282,9 @@ func TestCache_WithCleanupOnStartup(t *testing.T) {
282282
283283 // Pre-populate with expired entries
284284 past := time .Now ().Add (- 2 * time .Hour )
285- persister .Store (ctx , "expired1" , 1 , past )
286- persister .Store (ctx , "expired2" , 2 , past )
287- persister .Store (ctx , "valid" , 3 , time.Time {})
285+ _ = persister .Store (ctx , "expired1" , 1 , past )
286+ _ = persister .Store (ctx , "expired2" , 2 , past )
287+ _ = persister .Store (ctx , "valid" , 3 , time.Time {})
288288
289289 // Create cache with cleanup
290290 cache , err := New [string , int ](ctx ,
@@ -293,16 +293,22 @@ func TestCache_WithCleanupOnStartup(t *testing.T) {
293293 if err != nil {
294294 t .Fatalf ("New: %v" , err )
295295 }
296- defer cache .Close ()
296+ defer func () { _ = cache .Close () } ()
297297
298298 // Expired entries should be cleaned up
299- _ , _ , found , _ := persister .Load (ctx , "expired1" )
299+ _ , _ , found , err := persister .Load (ctx , "expired1" )
300+ if err != nil {
301+ t .Fatalf ("persister.Load: %v" , err )
302+ }
300303 if found {
301304 t .Error ("expired1 should have been cleaned up" )
302305 }
303306
304307 // Valid entry should remain
305- _ , _ , found , _ = persister .Load (ctx , "valid" )
308+ _ , _ , found , err = persister .Load (ctx , "valid" )
309+ if err != nil {
310+ t .Fatalf ("persister.Load: %v" , err )
311+ }
306312 if ! found {
307313 t .Error ("valid entry should still exist" )
308314 }
@@ -316,15 +322,18 @@ func TestCache_SetAsyncWithPersistence(t *testing.T) {
316322 if err != nil {
317323 t .Fatalf ("New: %v" , err )
318324 }
319- defer cache .Close ()
325+ defer func () { _ = cache .Close () } ()
320326
321327 // SetAsync should not block but value should be available immediately
322328 if err := cache .SetAsync (ctx , "key1" , 42 , 0 ); err != nil {
323329 t .Fatalf ("SetAsync: %v" , err )
324330 }
325331
326332 // Value should be in memory
327- val , found , _ := cache .Get (ctx , "key1" )
333+ val , found , err := cache .Get (ctx , "key1" )
334+ if err != nil {
335+ t .Fatalf ("Get: %v" , err )
336+ }
328337 if ! found || val != 42 {
329338 t .Error ("key1 should be available immediately after SetAsync" )
330339 }
@@ -333,7 +342,10 @@ func TestCache_SetAsyncWithPersistence(t *testing.T) {
333342 time .Sleep (50 * time .Millisecond )
334343
335344 // Should also be persisted
336- val , _ , found , _ = persister .Load (ctx , "key1" )
345+ val , _ , found , err = persister .Load (ctx , "key1" )
346+ if err != nil {
347+ t .Fatalf ("persister.Load: %v" , err )
348+ }
337349 if ! found || val != 42 {
338350 t .Error ("key1 should be persisted after SetAsync" )
339351 }
@@ -366,7 +378,7 @@ func TestCache_PersistenceErrors(t *testing.T) {
366378 if err != nil {
367379 t .Fatalf ("New: %v" , err )
368380 }
369- defer cache .Close ()
381+ defer func () { _ = cache .Close () } ()
370382
371383 // Set returns error when persistence fails (by design)
372384 // Value is still in memory, but error is returned to caller
@@ -376,7 +388,10 @@ func TestCache_PersistenceErrors(t *testing.T) {
376388 }
377389
378390 // Value should still be in memory despite persistence error
379- val , found , _ := cache .Get (ctx , "key1" )
391+ val , found , err := cache .Get (ctx , "key1" )
392+ if err != nil {
393+ t .Fatalf ("Get: %v" , err )
394+ }
380395 if ! found || val != 42 {
381396 t .Error ("key1 should be in memory even though persistence failed" )
382397 }
@@ -388,7 +403,10 @@ func TestCache_PersistenceErrors(t *testing.T) {
388403 }
389404
390405 // Value should be in memory
391- val , found , _ = cache .Get (ctx , "key3" )
406+ val , found , err = cache .Get (ctx , "key3" )
407+ if err != nil {
408+ t .Fatalf ("Get: %v" , err )
409+ }
392410 if ! found || val != 300 {
393411 t .Error ("key3 should be in memory after SetAsync" )
394412 }
@@ -399,7 +417,10 @@ func TestCache_PersistenceErrors(t *testing.T) {
399417 if err := cache .Set (ctx , "key2" , 100 , 0 ); err != nil {
400418 t .Fatalf ("Set: %v" , err )
401419 }
402- val , found , _ = cache .Get (ctx , "key2" )
420+ val , found , err = cache .Get (ctx , "key2" )
421+ if err != nil {
422+ t .Fatalf ("Get: %v" , err )
423+ }
403424 if ! found || val != 100 {
404425 t .Error ("Get should work from memory even if persistence load fails" )
405426 }
@@ -413,7 +434,7 @@ func TestCache_Delete_Errors(t *testing.T) {
413434 if err != nil {
414435 t .Fatalf ("New: %v" , err )
415436 }
416- defer cache .Close ()
437+ defer func () { _ = cache .Close () } ()
417438
418439 // Store a value (with failSet = false)
419440 persister .failSet = false
@@ -422,7 +443,10 @@ func TestCache_Delete_Errors(t *testing.T) {
422443 }
423444
424445 // Verify it's in memory
425- val , found , _ := cache .Get (ctx , "key1" )
446+ val , found , err := cache .Get (ctx , "key1" )
447+ if err != nil {
448+ t .Fatalf ("Get: %v" , err )
449+ }
426450 if ! found || val != 42 {
427451 t .Fatal ("key1 should be in memory before delete" )
428452 }
@@ -434,7 +458,10 @@ func TestCache_Delete_Errors(t *testing.T) {
434458 // Note: Even though persistence delete failed, key is deleted from memory.
435459 // However, Get will load it back from persistence since it's still there.
436460 // This tests graceful degradation - memory is cleaned up even if persistence fails.
437- val2 , found2 , _ := cache .Get (ctx , "key1" )
461+ val2 , found2 , err := cache .Get (ctx , "key1" )
462+ if err != nil {
463+ t .Fatalf ("Get: %v" , err )
464+ }
438465 if ! found2 || val2 != 42 {
439466 // Get loads from persistence, so key is found again
440467 t .Logf ("key1 found from persistence after failed delete (expected): %v, %v" , val2 , found2 )
@@ -456,7 +483,7 @@ func TestCache_Get_InvalidKey(t *testing.T) {
456483 if err != nil {
457484 t .Fatalf ("New: %v" , err )
458485 }
459- defer cache .Close ()
486+ defer func () { _ = cache .Close () } ()
460487
461488 // Get with empty key (invalid)
462489 _ , found , err := cache .Get (ctx , "" )
@@ -476,10 +503,10 @@ func TestCache_Get_PersistenceLoadError(t *testing.T) {
476503 if err != nil {
477504 t .Fatalf ("New: %v" , err )
478505 }
479- defer cache .Close ()
506+ defer func () { _ = cache .Close () } ()
480507
481508 // Pre-populate persistence (not in memory)
482- persister .Store (ctx , "key1" , 42 , time.Time {})
509+ _ = persister .Store (ctx , "key1" , 42 , time.Time {})
483510
484511 // Make persistence Load fail
485512 persister .failGet = true
@@ -521,24 +548,24 @@ func TestCache_GhostQueue(t *testing.T) {
521548 if err != nil {
522549 t .Fatalf ("New: %v" , err )
523550 }
524- defer cache .Close ()
551+ defer func () { _ = cache .Close () } ()
525552
526553 // Fill small queue (10% of 10 = 1)
527554 // Insert items to trigger ghost queue
528- for i := 0 ; i < 20 ; i ++ {
555+ for i := range 20 {
529556 if err := cache .Set (ctx , fmt .Sprintf ("key%d" , i ), i , 0 ); err != nil {
530557 t .Fatalf ("Set: %v" , err )
531558 }
532559 }
533560
534561 // Access some items to create hot items
535- for i := 0 ; i < 5 ; i ++ {
536- cache .Get (ctx , fmt .Sprintf ("key%d" , i ))
562+ for i := range 5 {
563+ _ , _ , _ = cache .Get (ctx , fmt .Sprintf ("key%d" , i ))
537564 }
538565
539566 // Insert more to trigger evictions from small queue to ghost queue
540- for i := 20 ; i < 40 ; i ++ {
541- if err := cache .Set (ctx , fmt .Sprintf ("key%d" , i ), i , 0 ); err != nil {
567+ for i := range 15 {
568+ if err := cache .Set (ctx , fmt .Sprintf ("key%d" , i + 20 ), i + 20 , 0 ); err != nil {
542569 t .Fatalf ("Set: %v" , err )
543570 }
544571 }
@@ -555,25 +582,25 @@ func TestCache_MainQueueEviction(t *testing.T) {
555582 if err != nil {
556583 t .Fatalf ("New: %v" , err )
557584 }
558- defer cache .Close ()
585+ defer func () { _ = cache .Close () } ()
559586
560587 // Insert and access items to get them into Main queue
561- for i := 0 ; i < 15 ; i ++ {
588+ for i := range 15 {
562589 key := fmt .Sprintf ("key%d" , i )
563590 if err := cache .Set (ctx , key , i , 0 ); err != nil {
564591 t .Fatalf ("Set: %v" , err )
565592 }
566593 // Access to promote to Main
567- cache .Get (ctx , key )
594+ _ , _ , _ = cache .Get (ctx , key )
568595 }
569596
570597 // Insert more items to trigger eviction from Main queue
571- for i := 15 ; i < 25 ; i ++ {
572- key := fmt .Sprintf ("key%d" , i )
573- if err := cache .Set (ctx , key , i , 0 ); err != nil {
598+ for i := range 10 {
599+ key := fmt .Sprintf ("key%d" , i + 15 )
600+ if err := cache .Set (ctx , key , i + 15 , 0 ); err != nil {
574601 t .Fatalf ("Set: %v" , err )
575602 }
576- cache .Get (ctx , key )
603+ _ , _ , _ = cache .Get (ctx , key )
577604 }
578605
579606 // Verify cache is at capacity
@@ -589,13 +616,13 @@ func TestCache_CleanupExpiredEntries(t *testing.T) {
589616 if err != nil {
590617 t .Fatalf ("New: %v" , err )
591618 }
592- defer cache .Close ()
619+ defer func () { _ = cache .Close () } ()
593620
594621 // Store items with expiry
595622 past := time .Now ().Add (- 1 * time .Hour )
596- cache .Set (ctx , "expired1" , 1 , - 1 * time .Hour ) // Already expired
597- cache .Set (ctx , "expired2" , 2 , - 1 * time .Hour )
598- cache .Set (ctx , "valid" , 3 , 1 * time .Hour )
623+ _ = cache .Set (ctx , "expired1" , 1 , - 1 * time .Hour ) // Already expired
624+ _ = cache .Set (ctx , "expired2" , 2 , - 1 * time .Hour )
625+ _ = cache .Set (ctx , "valid" , 3 , 1 * time .Hour )
599626
600627 // Manually set expiry to past for testing
601628 if err := cache .Set (ctx , "test-expired" , 99 , 0 ); err != nil {
@@ -613,7 +640,9 @@ func TestCache_CleanupExpiredEntries(t *testing.T) {
613640 t .Logf ("Cleanup deleted %d entries" , deleted )
614641
615642 // Valid entry should still exist
616- if _ , found , _ := cache .Get (ctx , "valid" ); ! found {
643+ if _ , found , err := cache .Get (ctx , "valid" ); err != nil {
644+ t .Fatalf ("Get: %v" , err )
645+ } else if ! found {
617646 t .Error ("valid entry should still exist after cleanup" )
618647 }
619648}
0 commit comments