1- package discard
1+ package cardinality
22
33import (
44 "sync"
@@ -9,13 +9,12 @@ import (
99)
1010
1111func TestNewCache (t * testing.T ) {
12- cache , err := NewCache (time .Minute )
13- assert .NoError (t , err )
12+ cache := NewCache (time .Minute )
1413 assert .NotNil (t , cache )
1514}
1615
1716func TestSetAndExists (t * testing.T ) {
18- cache , _ := NewCache (time .Minute )
17+ cache := NewCache (time .Minute )
1918
2019 t .Run ("basic set and get" , func (t * testing.T ) {
2120 key := "test-key"
@@ -32,25 +31,47 @@ func TestSetAndExists(t *testing.T) {
3231}
3332
3433func TestDelete (t * testing.T ) {
35- cache , _ := NewCache (time .Minute )
34+ cache := NewCache (time .Minute )
3635
37- key := "to-delete"
38- cache .Set (key )
36+ t .Run ("delete existing key with lock=true" , func (t * testing.T ) {
37+ key := "to-delete-1"
38+ cache .Set (key )
39+
40+ cache .delete (key , true )
3941
40- t .Run ("delete existing key" , func (t * testing.T ) {
41- cache .delete (key )
4242 found := cache .IsExists (key )
43- assert .False (t , found )
43+ assert .False (t , found , "Key should be deleted" )
44+ })
45+
46+ t .Run ("delete existing key with lock=false" , func (t * testing.T ) {
47+ key := "to-delete-2"
48+ cache .Set (key )
49+
50+ // Manually acquire lock since we're using lock=false
51+ cache .mu .Lock ()
52+ cache .delete (key , false )
53+ cache .mu .Unlock ()
54+
55+ found := cache .IsExists (key )
56+ assert .False (t , found , "Key should be deleted when lock=false" )
4457 })
4558
4659 t .Run ("delete non-existent key" , func (t * testing.T ) {
47- // Should not panic
48- cache .delete ("never-existed" )
60+ // Should not panic or cause issues
61+ assert .NotPanics (t , func () {
62+ cache .delete ("never-existed-1" , true )
63+ })
64+
65+ // Verify cache is still functional
66+ key := "test-after-non-existent"
67+ cache .Set (key )
68+ found := cache .IsExists (key )
69+ assert .True (t , found , "Cache should still work after deleting non-existent key" )
4970 })
5071}
5172
5273func TestCountPrefix (t * testing.T ) {
53- cache , _ := NewCache (time .Minute )
74+ cache := NewCache (time .Minute )
5475
5576 keys := []string {
5677 "key1_subkey1" ,
@@ -80,13 +101,13 @@ func TestCountPrefix(t *testing.T) {
80101 }
81102
82103 t .Run ("count after delete" , func (t * testing.T ) {
83- cache .delete ("key1_subkey1" )
104+ cache .delete ("key1_subkey1" , true )
84105 assert .Equal (t , 1 , cache .CountPrefix ("key1" ))
85106 })
86107}
87108
88109func TestConcurrentOperations (t * testing.T ) {
89- cache , _ := NewCache (time .Minute )
110+ cache := NewCache (time .Minute )
90111
91112 var wg sync.WaitGroup
92113 keys := []string {"key1" , "key2" , "key3" }
@@ -128,7 +149,7 @@ func TestConcurrentOperations(t *testing.T) {
128149 go func (k string ) {
129150 defer wg .Done ()
130151 for i := 0 ; i < 100 ; i ++ {
131- cache .delete (k )
152+ cache .delete (k , true )
132153 }
133154 }(key )
134155 }
@@ -147,14 +168,14 @@ func TestConcurrentOperations(t *testing.T) {
147168 for i := 0 ; i < 100 ; i ++ {
148169 cache .Set ("key-x" )
149170 cache .Set ("key-y" )
150- cache .delete ("key-x" )
171+ cache .delete ("key-x" , true )
151172 }
152173 }()
153174 wg .Wait ()
154175}
155176
156177func TestTTL (t * testing.T ) {
157- cache , _ := NewCache (100 * time .Millisecond )
178+ cache := NewCache (100 * time .Millisecond )
158179
159180 key := "ttl-key"
160181 cache .Set (key )
0 commit comments