| 1 |
package sievecache |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
func TestSyncSieveCache(t *testing.T) { |
| 10 |
cache, err := NewSync[string, string](100) |
| 11 |
if err != nil { |
| 12 |
t.Fatalf("Failed to create cache: %v", err) |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
inserted := cache.Insert("key1", "value1") |
| 17 |
if !inserted { |
| 18 |
t.Error("Expected insert to return true for new key") |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
val, found := cache.Get("key1") |
| 23 |
if !found || val != "value1" { |
| 24 |
t.Errorf("Expected value1, got %v", val) |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
if !cache.ContainsKey("key1") { |
| 29 |
t.Error("Expected ContainsKey to return true") |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
if cache.Capacity() != 100 { |
| 34 |
t.Errorf("Expected capacity 100, got %d", cache.Capacity()) |
| 35 |
} |
| 36 |
if cache.Len() != 1 { |
| 37 |
t.Errorf("Expected length 1, got %d", cache.Len()) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
val, found = cache.Remove("key1") |
| 42 |
if !found || val != "value1" { |
| 43 |
t.Errorf("Expected value1, got %v", val) |
| 44 |
} |
| 45 |
|
| 46 |
if cache.Len() != 0 { |
| 47 |
t.Errorf("Expected length 0, got %d", cache.Len()) |
| 48 |
} |
| 49 |
|
| 50 |
if !cache.IsEmpty() { |
| 51 |
t.Error("Expected IsEmpty to return true") |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func TestMultithreadedAccess(t *testing.T) { |
| 56 |
cache, _ := NewSync[string, string](100) |
| 57 |
|
| 58 |
|
| 59 |
cache.Insert("shared", "initial") |
| 60 |
|
| 61 |
var wg sync.WaitGroup |
| 62 |
wg.Add(2) |
| 63 |
|
| 64 |
|
| 65 |
go func() { |
| 66 |
defer wg.Done() |
| 67 |
cache.Insert("shared", "updated") |
| 68 |
cache.Insert("thread_only", "thread_value") |
| 69 |
}() |
| 70 |
|
| 71 |
|
| 72 |
go func() { |
| 73 |
defer wg.Done() |
| 74 |
cache.Insert("main_only", "main_value") |
| 75 |
}() |
| 76 |
|
| 77 |
|
| 78 |
wg.Wait() |
| 79 |
|
| 80 |
|
| 81 |
val, _ := cache.Get("shared") |
| 82 |
if val != "updated" { |
| 83 |
t.Errorf("Expected updated, got %v", val) |
| 84 |
} |
| 85 |
|
| 86 |
val, found := cache.Get("thread_only") |
| 87 |
if !found || val != "thread_value" { |
| 88 |
t.Errorf("Expected thread_value, got %v", val) |
| 89 |
} |
| 90 |
|
| 91 |
val, found = cache.Get("main_only") |
| 92 |
if !found || val != "main_value" { |
| 93 |
t.Errorf("Expected main_value, got %v", val) |
| 94 |
} |
| 95 |
|
| 96 |
if cache.Len() != 3 { |
| 97 |
t.Errorf("Expected length 3, got %d", cache.Len()) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestWithLock(t *testing.T) { |
| 102 |
cache, _ := NewSync[string, string](100) |
| 103 |
|
| 104 |
|
| 105 |
cache.WithLock(func(innerCache *SieveCache[string, string]) { |
| 106 |
innerCache.Insert("key1", "value1") |
| 107 |
innerCache.Insert("key2", "value2") |
| 108 |
innerCache.Insert("key3", "value3") |
| 109 |
|
| 110 |
|
| 111 |
if innerCache.Len() != 3 { |
| 112 |
t.Errorf("Expected length 3, got %d", innerCache.Len()) |
| 113 |
} |
| 114 |
}) |
| 115 |
|
| 116 |
if cache.Len() != 3 { |
| 117 |
t.Errorf("Expected length 3, got %d", cache.Len()) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestGetMut(t *testing.T) { |
| 122 |
cache, _ := NewSync[string, string](100) |
| 123 |
cache.Insert("key", "value") |
| 124 |
|
| 125 |
|
| 126 |
modified := cache.GetMut("key", func(value *string) { |
| 127 |
*value = "new_value" |
| 128 |
}) |
| 129 |
|
| 130 |
if !modified { |
| 131 |
t.Error("Expected GetMut to return true for existing key") |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
val, _ := cache.Get("key") |
| 136 |
if val != "new_value" { |
| 137 |
t.Errorf("Expected new_value, got %v", val) |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
modified = cache.GetMut("missing", func(_ *string) { |
| 142 |
t.Error("This should not be called") |
| 143 |
}) |
| 144 |
|
| 145 |
if modified { |
| 146 |
t.Error("Expected GetMut to return false for missing key") |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func TestForEachMethods(t *testing.T) { |
| 151 |
cache, _ := NewSync[string, string](10) |
| 152 |
cache.Insert("key1", "value1") |
| 153 |
cache.Insert("key2", "value2") |
| 154 |
|
| 155 |
|
| 156 |
cache.ForEachValue(func(value *string) { |
| 157 |
*value = *value + "_updated" |
| 158 |
}) |
| 159 |
|
| 160 |
val, _ := cache.Get("key1") |
| 161 |
if val != "value1_updated" { |
| 162 |
t.Errorf("Expected value1_updated, got %v", val) |
| 163 |
} |
| 164 |
|
| 165 |
val, _ = cache.Get("key2") |
| 166 |
if val != "value2_updated" { |
| 167 |
t.Errorf("Expected value2_updated, got %v", val) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
cache.ForEachEntry(func(key string, value *string) { |
| 172 |
if key == "key1" { |
| 173 |
*value = *value + "_special" |
| 174 |
} |
| 175 |
}) |
| 176 |
|
| 177 |
val, _ = cache.Get("key1") |
| 178 |
if val != "value1_updated_special" { |
| 179 |
t.Errorf("Expected value1_updated_special, got %v", val) |
| 180 |
} |
| 181 |
|
| 182 |
val, _ = cache.Get("key2") |
| 183 |
if val != "value2_updated" { |
| 184 |
t.Errorf("Expected value2_updated, got %v", val) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func TestDeadlockPrevention(t *testing.T) { |
| 189 |
cache, _ := NewSync[string, int](100) |
| 190 |
|
| 191 |
|
| 192 |
cache.Insert("key1", 1) |
| 193 |
cache.Insert("key2", 2) |
| 194 |
|
| 195 |
var wg sync.WaitGroup |
| 196 |
wg.Add(2) |
| 197 |
|
| 198 |
|
| 199 |
go func() { |
| 200 |
defer wg.Done() |
| 201 |
|
| 202 |
cache.GetMut("key1", func(value *int) { |
| 203 |
|
| 204 |
|
| 205 |
val, found := cache.Get("key2") |
| 206 |
if !found || val != 2 { |
| 207 |
t.Errorf("Expected 2, got %v", val) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
cache.Insert("key3", 3) |
| 212 |
|
| 213 |
*value += 10 |
| 214 |
}) |
| 215 |
}() |
| 216 |
|
| 217 |
|
| 218 |
go func() { |
| 219 |
defer wg.Done() |
| 220 |
|
| 221 |
|
| 222 |
time.Sleep(10 * time.Millisecond) |
| 223 |
|
| 224 |
|
| 225 |
cache.Insert("key4", 4) |
| 226 |
val, found := cache.Get("key2") |
| 227 |
if !found || val != 2 { |
| 228 |
t.Errorf("Expected 2, got %v", val) |
| 229 |
} |
| 230 |
}() |
| 231 |
|
| 232 |
|
| 233 |
wg.Wait() |
| 234 |
|
| 235 |
|
| 236 |
val, _ := cache.Get("key1") |
| 237 |
if val != 11 { |
| 238 |
t.Errorf("Expected 11, got %v", val) |
| 239 |
} |
| 240 |
|
| 241 |
val, found := cache.Get("key3") |
| 242 |
if !found || val != 3 { |
| 243 |
t.Errorf("Expected 3, got %v", val) |
| 244 |
} |
| 245 |
|
| 246 |
val, found = cache.Get("key4") |
| 247 |
if !found || val != 4 { |
| 248 |
t.Errorf("Expected 4, got %v", val) |
| 249 |
} |
| 250 |
} |