66 "sync"
77 "testing"
88 "time"
9+
10+ "github.com/codeGROOVE-dev/sfcache/pkg/persist"
911)
1012
1113// mockStore is a simple in-memory store for testing.
@@ -29,6 +31,18 @@ func newMockStore[K comparable, V any]() *mockStore[K, V] {
2931 }
3032}
3133
34+ func (m * mockStore [K , V ]) setFailGet (v bool ) {
35+ m .mu .Lock ()
36+ m .failGet = v
37+ m .mu .Unlock ()
38+ }
39+
40+ func (m * mockStore [K , V ]) setFailSet (v bool ) {
41+ m .mu .Lock ()
42+ m .failSet = v
43+ m .mu .Unlock ()
44+ }
45+
3246func (m * mockStore [K , V ]) ValidateKey (key K ) error {
3347 return nil
3448}
@@ -87,8 +101,8 @@ func (m *mockStore[K, V]) Delete(ctx context.Context, key K) error {
87101}
88102
89103//nolint:gocritic // Channel returns are clearer without named results
90- func (m * mockStore [K , V ]) LoadRecent (ctx context.Context , limit int ) (<- chan Entry [K , V ], <- chan error ) {
91- entryCh := make (chan Entry [K , V ], 10 )
104+ func (m * mockStore [K , V ]) LoadRecent (ctx context.Context , limit int ) (<- chan persist. Entry [K , V ], <- chan error ) {
105+ entryCh := make (chan persist. Entry [K , V ], 10 )
92106 errCh := make (chan error , 1 )
93107
94108 go func () {
@@ -115,7 +129,7 @@ func (m *mockStore[K, V]) LoadRecent(ctx context.Context, limit int) (<-chan Ent
115129 key = sk
116130 }
117131
118- entryCh <- Entry [K , V ]{
132+ entryCh <- persist. Entry [K , V ]{
119133 Key : key ,
120134 Value : entry .value ,
121135 Expiry : entry .expiry ,
@@ -360,7 +374,7 @@ func TestPersistentCache_Errors(t *testing.T) {
360374
361375 // Set returns error when persistence fails (by design)
362376 // Value is still in memory, but error is returned to caller
363- store .failSet = true
377+ store .setFailSet ( true )
364378 if err := cache .Set (ctx , "key1" , 42 , 0 ); err == nil {
365379 t .Error ("Set should return error when persistence fails" )
366380 }
@@ -375,7 +389,7 @@ func TestPersistentCache_Errors(t *testing.T) {
375389 }
376390
377391 // SetAsync logs persistence errors but doesn't return them
378- store .failSet = true
392+ store .setFailSet ( true )
379393 if err := cache .SetAsync (ctx , "key3" , 300 , 0 ); err != nil {
380394 t .Fatalf ("SetAsync should not fail synchronously: %v" , err )
381395 }
@@ -393,8 +407,8 @@ func TestPersistentCache_Errors(t *testing.T) {
393407 time .Sleep (50 * time .Millisecond )
394408
395409 // Get should work from memory even if persistence fails
396- store .failGet = true
397- store .failSet = false
410+ store .setFailGet ( true )
411+ store .setFailSet ( false )
398412 if err := cache .Set (ctx , "key2" , 100 , 0 ); err != nil {
399413 t .Fatalf ("Set: %v" , err )
400414 }
@@ -418,7 +432,7 @@ func TestPersistentCache_Delete_Errors(t *testing.T) {
418432 defer func () { _ = cache .Close () }() //nolint:errcheck // Test cleanup
419433
420434 // Store a value (with failSet = false)
421- store .failSet = false
435+ store .setFailSet ( false )
422436 if err := cache .Set (ctx , "key1" , 42 , 0 ); err != nil {
423437 t .Fatalf ("Set: %v" , err )
424438 }
@@ -433,7 +447,7 @@ func TestPersistentCache_Delete_Errors(t *testing.T) {
433447 }
434448
435449 // Now make persistence delete fail
436- store .failSet = true // failSet affects Delete too in mock
450+ store .setFailSet ( true ) // failSet affects Delete too in mock
437451 err = cache .Delete (ctx , "key1" )
438452 if err == nil {
439453 t .Error ("Delete should return error when persistence fails" )
@@ -492,7 +506,7 @@ func TestPersistentCache_Get_PersistenceLoadError(t *testing.T) {
492506 _ = store .Store (ctx , "key1" , 42 , time.Time {}) //nolint:errcheck // Test fixture
493507
494508 // Make persistence Load fail
495- store .failGet = true
509+ store .setFailGet ( true )
496510
497511 // Get should return error on persistence failure
498512 _ , found , err := cache .Get (ctx , "key1" )
0 commit comments