Spaces:
Build error
Build error
File size: 6,833 Bytes
4b1daed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | package cache_test
import (
"context"
"testing"
"time"
"github.com/AmaniQuery/amaniquery/internal/cache"
)
// TestLRUCache_SetGet verifies basic set/get operations
func TestLRUCache_SetGet(t *testing.T) {
lru := cache.NewLRUCache(100)
// Test set and get
lru.Set("key1", []byte("value1"))
value, found := lru.Get("key1")
if !found {
t.Fatal("Expected to find key1")
}
if string(value) != "value1" {
t.Errorf("Expected 'value1', got '%s'", string(value))
}
// Test missing key
_, found = lru.Get("nonexistent")
if found {
t.Error("Expected not to find nonexistent key")
}
}
// TestLRUCache_Update verifies updating existing keys
func TestLRUCache_Update(t *testing.T) {
lru := cache.NewLRUCache(100)
lru.Set("key1", []byte("original"))
lru.Set("key1", []byte("updated"))
value, found := lru.Get("key1")
if !found {
t.Fatal("Expected to find key1")
}
if string(value) != "updated" {
t.Errorf("Expected 'updated', got '%s'", string(value))
}
}
// TestLRUCache_Eviction verifies LRU eviction when over capacity
func TestLRUCache_Eviction(t *testing.T) {
lru := cache.NewLRUCache(3)
// Fill cache to capacity
lru.Set("key1", []byte("value1"))
lru.Set("key2", []byte("value2"))
lru.Set("key3", []byte("value3"))
// Access key1 to make it recently used
lru.Get("key1")
// Add new key, should evict key2 (least recently used)
lru.Set("key4", []byte("value4"))
// key2 should be evicted
_, found := lru.Get("key2")
if found {
t.Error("Expected key2 to be evicted")
}
// key1 should still exist (was accessed recently)
_, found = lru.Get("key1")
if !found {
t.Error("Expected key1 to still exist")
}
// key3 and key4 should exist
_, found = lru.Get("key3")
if !found {
t.Error("Expected key3 to exist")
}
_, found = lru.Get("key4")
if !found {
t.Error("Expected key4 to exist")
}
}
// TestLRUCache_Delete verifies deletion operations
func TestLRUCache_Delete(t *testing.T) {
lru := cache.NewLRUCache(100)
lru.Set("key1", []byte("value1"))
lru.Set("key2", []byte("value2"))
lru.Delete("key1")
_, found := lru.Get("key1")
if found {
t.Error("Expected key1 to be deleted")
}
// key2 should still exist
_, found = lru.Get("key2")
if !found {
t.Error("Expected key2 to still exist")
}
}
// TestLRUCache_DeleteNonexistent verifies deleting nonexistent keys doesn't panic
func TestLRUCache_DeleteNonexistent(t *testing.T) {
lru := cache.NewLRUCache(100)
// Should not panic
lru.Delete("nonexistent")
}
// TestCacheMissError tests the error type
func TestCacheMissError(t *testing.T) {
err := cache.ErrCacheMiss
if err.Error() != "cache miss" {
t.Errorf("Expected 'cache miss', got '%s'", err.Error())
}
}
// TestMultiTierCache_LocalOnly tests cache without Redis connection
func TestMultiTierCache_LocalOnly(t *testing.T) {
// Create cache with invalid Redis URL to ensure Redis is not used
cfg := cache.Config{
RedisURL: "redis://invalid:6379", // Will fail connection
LocalSize: 100,
TTL: time.Hour,
MaxRetries: 1,
PoolSize: 1,
}
c, err := cache.New(cfg)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
defer c.Close()
ctx := context.Background()
// Set and get should work with local cache
err = c.Set(ctx, "key1", []byte("value1"), 0)
if err != nil {
t.Fatalf("Set failed: %v", err)
}
value, err := c.Get(ctx, "key1")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if string(value) != "value1" {
t.Errorf("Expected 'value1', got '%s'", string(value))
}
}
// TestMultiTierCache_CacheMiss tests cache miss behavior
func TestMultiTierCache_CacheMiss(t *testing.T) {
cfg := cache.Config{
RedisURL: "redis://invalid:6379",
LocalSize: 100,
TTL: time.Hour,
MaxRetries: 1,
PoolSize: 1,
}
c, err := cache.New(cfg)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
defer c.Close()
ctx := context.Background()
_, err = c.Get(ctx, "nonexistent")
if err == nil {
t.Error("Expected cache miss error")
}
}
// TestMultiTierCache_Delete tests deletion
func TestMultiTierCache_Delete(t *testing.T) {
cfg := cache.Config{
RedisURL: "redis://invalid:6379",
LocalSize: 100,
TTL: time.Hour,
MaxRetries: 1,
PoolSize: 1,
}
c, err := cache.New(cfg)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
defer c.Close()
ctx := context.Background()
// Set then delete
c.Set(ctx, "key1", []byte("value1"), 0)
err = c.Delete(ctx, "key1")
if err != nil {
t.Fatalf("Delete failed: %v", err)
}
// Should be gone
_, err = c.Get(ctx, "key1")
if err == nil {
t.Error("Expected cache miss after delete")
}
}
// TestMultiTierCache_JSON tests JSON operations
func TestMultiTierCache_JSON(t *testing.T) {
cfg := cache.Config{
RedisURL: "redis://invalid:6379",
LocalSize: 100,
TTL: time.Hour,
MaxRetries: 1,
PoolSize: 1,
}
c, err := cache.New(cfg)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
defer c.Close()
ctx := context.Background()
type testData struct {
Name string `json:"name"`
Value int `json:"value"`
}
original := testData{Name: "test", Value: 42}
err = c.SetJSON(ctx, "json-key", original, 0)
if err != nil {
t.Fatalf("SetJSON failed: %v", err)
}
var result testData
err = c.GetJSON(ctx, "json-key", &result)
if err != nil {
t.Fatalf("GetJSON failed: %v", err)
}
if result.Name != original.Name || result.Value != original.Value {
t.Errorf("JSON mismatch: expected %+v, got %+v", original, result)
}
}
// TestMultiTierCache_Metrics tests metrics tracking
func TestMultiTierCache_Metrics(t *testing.T) {
cfg := cache.Config{
RedisURL: "redis://invalid:6379",
LocalSize: 100,
TTL: time.Hour,
MaxRetries: 1,
PoolSize: 1,
}
c, err := cache.New(cfg)
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
defer c.Close()
ctx := context.Background()
// Set and get to generate metrics
c.Set(ctx, "key1", []byte("value1"), 0)
c.Get(ctx, "key1") // Hit
c.Get(ctx, "key2") // Miss
metrics := c.GetMetrics()
if metrics.LocalHits != 1 {
t.Errorf("Expected 1 local hit, got %d", metrics.LocalHits)
}
if metrics.LocalMisses != 1 {
t.Errorf("Expected 1 local miss, got %d", metrics.LocalMisses)
}
}
// BenchmarkLRUCache_Set benchmarks LRU set operations
func BenchmarkLRUCache_Set(b *testing.B) {
lru := cache.NewLRUCache(10000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
lru.Set("key"+string(rune(i%1000)), []byte("value"))
}
}
// BenchmarkLRUCache_Get benchmarks LRU get operations
func BenchmarkLRUCache_Get(b *testing.B) {
lru := cache.NewLRUCache(10000)
// Pre-populate
for i := 0; i < 1000; i++ {
lru.Set("key"+string(rune(i)), []byte("value"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
lru.Get("key" + string(rune(i%1000)))
}
}
|