|
1 | 1 | package test
|
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/pnguyen215/cachify" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// Test cache creation and basic functionality |
| 12 | +func TestLRU_SetAndGet(t *testing.T) { |
| 13 | + cache := cachify.NewLRU(2) |
| 14 | + |
| 15 | + cache.Set("a", "alpha") |
| 16 | + cache.Set("b", "beta") |
| 17 | + |
| 18 | + // Verify the values |
| 19 | + val, ok := cache.Get("a") |
| 20 | + assert.True(t, ok) |
| 21 | + assert.Equal(t, "alpha", val) |
| 22 | + |
| 23 | + val, ok = cache.Get("b") |
| 24 | + assert.True(t, ok) |
| 25 | + assert.Equal(t, "beta", val) |
| 26 | + |
| 27 | + // Add a new entry to exceed capacity and evict the least recently used |
| 28 | + cache.Set("c", "gamma") |
| 29 | + _, ok = cache.Get("a") |
| 30 | + assert.False(t, ok) // "a" should be evicted |
| 31 | +} |
| 32 | + |
| 33 | +// Test eviction callback |
| 34 | +func TestLRU_Callback(t *testing.T) { |
| 35 | + evicted := make(map[string]interface{}) |
| 36 | + callback := func(key string, value interface{}) { |
| 37 | + evicted[key] = value |
| 38 | + } |
| 39 | + |
| 40 | + cache := cachify.NewLRUCallback(2, callback) |
| 41 | + |
| 42 | + cache.Set("x", "X-ray") |
| 43 | + cache.Set("y", "Yankee") |
| 44 | + cache.Set("z", "Zulu") |
| 45 | + |
| 46 | + // Verify eviction |
| 47 | + assert.Len(t, evicted, 1) |
| 48 | + assert.Equal(t, "X-ray", evicted["x"]) |
| 49 | +} |
| 50 | + |
| 51 | +// Test expiration functionality |
| 52 | +func TestLRU_Expiration(t *testing.T) { |
| 53 | + cache := cachify.NewLRUExpires(2, 12*time.Second) |
| 54 | + |
| 55 | + cache.Set("key", "value") |
| 56 | + time.Sleep(1 * time.Second) |
| 57 | + |
| 58 | + // Before expiration |
| 59 | + val, ok := cache.Get("key") |
| 60 | + assert.True(t, ok) |
| 61 | + assert.Equal(t, "value", val) |
| 62 | + |
| 63 | + // After expiration |
| 64 | + time.Sleep(1 * time.Second) |
| 65 | + _, ok = cache.Get("key") |
| 66 | + assert.True(t, ok) |
| 67 | +} |
| 68 | + |
| 69 | +// Test dynamic capacity adjustment |
| 70 | +func TestLRU_SetCapacity(t *testing.T) { |
| 71 | + cache := cachify.NewLRU(2) |
| 72 | + |
| 73 | + cache.Set("a", "alpha") |
| 74 | + cache.Set("b", "beta") |
| 75 | + |
| 76 | + // Increase capacity |
| 77 | + cache.SetCapacity(3) |
| 78 | + cache.Set("c", "gamma") |
| 79 | + |
| 80 | + assert.Equal(t, 3, cache.Len()) |
| 81 | + assert.Contains(t, cache.GetAll(), "c") |
| 82 | + |
| 83 | + // Decrease capacity |
| 84 | + cache.SetCapacity(2) |
| 85 | + _, ok := cache.Get("a") // "a" should be evicted |
| 86 | + assert.False(t, ok) |
| 87 | +} |
| 88 | + |
| 89 | +// Test IsMostRecentlyUsed and GetMostRecentlyUsed |
| 90 | +func TestLRU_MostRecentlyUsed(t *testing.T) { |
| 91 | + cache := cachify.NewLRU(3) |
| 92 | + |
| 93 | + cache.Set("a", "alpha") |
| 94 | + cache.Set("b", "beta") |
| 95 | + cache.Set("c", "gamma") |
| 96 | + |
| 97 | + assert.True(t, cache.IsMostRecentlyUsed("c")) |
| 98 | + |
| 99 | + state, ok := cache.GetMostRecentlyUsed() |
| 100 | + assert.True(t, ok) |
| 101 | + assert.Equal(t, "c", state.Key()) |
| 102 | + assert.Equal(t, "gamma", state.Value()) |
| 103 | +} |
| 104 | + |
| 105 | +// Test Clear and IsEmpty |
| 106 | +func TestLRU_Clear(t *testing.T) { |
| 107 | + cache := cachify.NewLRU(2) |
| 108 | + |
| 109 | + cache.Set("a", "alpha") |
| 110 | + cache.Set("b", "beta") |
| 111 | + |
| 112 | + cache.Clear() |
| 113 | + assert.Equal(t, 0, cache.Len()) |
| 114 | + assert.True(t, cache.IsEmpty()) |
| 115 | +} |
| 116 | + |
| 117 | +// Test ExpandExpiry |
| 118 | +func TestLRU_ExpandExpiry(t *testing.T) { |
| 119 | + cache := cachify.NewLRUExpires(2, 12*time.Second) |
| 120 | + |
| 121 | + cache.Set("key", "value") |
| 122 | + cache.ExpandExpiry("key", 3*time.Second) |
| 123 | + |
| 124 | + time.Sleep(1 * time.Second) |
| 125 | + val, ok := cache.Get("key") |
| 126 | + assert.True(t, ok) |
| 127 | + assert.Equal(t, "value", val) |
| 128 | +} |
| 129 | + |
| 130 | +// Test PersistExpiry |
| 131 | +func TestLRU_PersistExpiry(t *testing.T) { |
| 132 | + cache := cachify.NewLRUExpires(2, 5*time.Second) |
| 133 | + |
| 134 | + cache.Set("key", "value") |
| 135 | + remain, ok := cache.PersistExpiry("key") |
| 136 | + assert.True(t, ok) |
| 137 | + assert.LessOrEqual(t, remain, 5*time.Second) |
| 138 | +} |
| 139 | + |
| 140 | +// Test Update |
| 141 | +func TestLRU_Update(t *testing.T) { |
| 142 | + cache := cachify.NewLRU(2) |
| 143 | + |
| 144 | + cache.Set("key", "old_value") |
| 145 | + cache.Update("key", "new_value") |
| 146 | + |
| 147 | + val, ok := cache.Get("key") |
| 148 | + assert.True(t, ok) |
| 149 | + assert.Equal(t, "new_value", val) |
| 150 | +} |
| 151 | + |
| 152 | +// Test Remove |
| 153 | +func TestLRU_Remove(t *testing.T) { |
| 154 | + cache := cachify.NewLRU(2) |
| 155 | + |
| 156 | + cache.Set("a", "alpha") |
| 157 | + cache.Remove("a") |
| 158 | + |
| 159 | + _, ok := cache.Get("a") |
| 160 | + assert.False(t, ok) |
| 161 | +} |
0 commit comments