Something went wrong. Try again.
notes and lists
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247package cache
import ( "fmt" "sync" "testing" "time")
func TestBasicSetGet(t *testing.T) { c := NewTTLCache(10, time.Minute)
c.Set("key1", "value1")
val, found := c.Get("key1") if !found { t.Error("expected to find key1") } if val != "value1" { t.Errorf("expected value1, got %v", val) }}
func TestGetNonExistent(t *testing.T) { c := NewTTLCache(10, time.Minute)
val, found := c.Get("nonexistent") if found { t.Error("expected not to find nonexistent key") } if val != nil { t.Errorf("expected nil, got %v", val) }}
func TestTTLExpiration(t *testing.T) { c := NewTTLCache(10, 50*time.Millisecond)
c.Set("key1", "value1")
// Should exist immediately _, found := c.Get("key1") if !found { t.Error("expected key to exist before expiration") }
// Wait for expiration time.Sleep(100 * time.Millisecond)
// Should be expired now _, found = c.Get("key1") if found { t.Error("expected key to be expired") }
// Size should be 0 if c.Size() != 0 { t.Errorf("expected size 0 after expiration, got %d", c.Size()) }}
func TestLRUEviction(t *testing.T) { c := NewTTLCache(3, time.Minute)
// Add 3 items c.Set("key1", "value1") c.Set("key2", "value2") c.Set("key3", "value3")
if c.Size() != 3 { t.Errorf("expected size 3, got %d", c.Size()) }
// Access key1 to make it most recently used c.Get("key1")
// Add 4th item - should evict key2 (least recently used) c.Set("key4", "value4")
if c.Size() != 3 { t.Errorf("expected size 3 after eviction, got %d", c.Size()) }
// key1 should exist (was accessed) _, found := c.Get("key1") if !found { t.Error("expected key1 to exist (was recently accessed)") }
// key2 should be evicted _, found = c.Get("key2") if found { t.Error("expected key2 to be evicted (was least recently used)") }
// key3 and key4 should exist _, found = c.Get("key3") if !found { t.Error("expected key3 to exist") } _, found = c.Get("key4") if !found { t.Error("expected key4 to exist") }}
func TestUpdateExistingKey(t *testing.T) { c := NewTTLCache(3, time.Minute)
c.Set("key1", "value1") c.Set("key2", "value2") c.Set("key3", "value3")
// Update key1 - should not trigger eviction c.Set("key1", "updated")
if c.Size() != 3 { t.Errorf("expected size 3 after update, got %d", c.Size()) }
val, found := c.Get("key1") if !found { t.Error("expected key1 to exist") } if val != "updated" { t.Errorf("expected 'updated', got %v", val) }}
func TestDelete(t *testing.T) { c := NewTTLCache(10, time.Minute)
c.Set("key1", "value1") c.Delete("key1")
_, found := c.Get("key1") if found { t.Error("expected key1 to be deleted") }
if c.Size() != 0 { t.Errorf("expected size 0, got %d", c.Size()) }}
func TestClear(t *testing.T) { c := NewTTLCache(10, time.Minute)
c.Set("key1", "value1") c.Set("key2", "value2") c.Clear()
if c.Size() != 0 { t.Errorf("expected size 0 after clear, got %d", c.Size()) }
_, found := c.Get("key1") if found { t.Error("expected key1 to be cleared") }}
func TestConcurrentAccess(t *testing.T) { c := NewTTLCache(10000, time.Minute)
var wg sync.WaitGroup numGoroutines := 100 numOperations := 100
// Concurrent writes for i := 0; i < numGoroutines; i++ { wg.Add(1) go func(id int) { defer wg.Done() for j := 0; j < numOperations; j++ { c.Set(fmt.Sprintf("key-%d-%d", id, j), "value") } }(i) }
// Concurrent reads for i := 0; i < numGoroutines; i++ { wg.Add(1) go func(id int) { defer wg.Done() for j := 0; j < numOperations; j++ { c.Get(fmt.Sprintf("key-%d-%d", id, j)) } }(i) }
wg.Wait()
// All items should be in cache (10,000 items, capacity is 10,000) if c.Size() != numGoroutines*numOperations { t.Errorf("expected size %d, got %d", numGoroutines*numOperations, c.Size()) }}
func TestConcurrentWithEviction(t *testing.T) { c := NewTTLCache(50, time.Minute)
var wg sync.WaitGroup numGoroutines := 10 numOperations := 20
// More writes than capacity to trigger evictions for i := 0; i < numGoroutines; i++ { wg.Add(1) go func(id int) { defer wg.Done() for j := 0; j < numOperations; j++ { c.Set(fmt.Sprintf("key-%d-%d", id, j), "value") } }(i) }
wg.Wait()
// Size should be at most maxSize if c.Size() > 50 { t.Errorf("expected size <= 50, got %d", c.Size()) }}
func TestSetWithTTL(t *testing.T) { c := NewTTLCache(10, time.Minute)
// Set with short TTL c.SetWithTTL("key1", "value1", 50*time.Millisecond)
// Should exist _, found := c.Get("key1") if !found { t.Error("expected key to exist immediately") }
// Wait for custom TTL to expire time.Sleep(100 * time.Millisecond)
// Should be expired _, found = c.Get("key1") if found { t.Error("expected key to be expired after custom TTL") }}