github.com/igoogolx/clash@v1.19.8/common/cache/lrucache_test.go (about) 1 package cache 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 var entries = []struct { 11 key string 12 value string 13 }{ 14 {"1", "one"}, 15 {"2", "two"}, 16 {"3", "three"}, 17 {"4", "four"}, 18 {"5", "five"}, 19 } 20 21 func TestLRUCache(t *testing.T) { 22 c := New() 23 24 for _, e := range entries { 25 c.Set(e.key, e.value) 26 } 27 28 c.Delete("missing") 29 _, ok := c.Get("missing") 30 assert.False(t, ok) 31 32 for _, e := range entries { 33 value, ok := c.Get(e.key) 34 if assert.True(t, ok) { 35 assert.Equal(t, e.value, value.(string)) 36 } 37 } 38 39 for _, e := range entries { 40 c.Delete(e.key) 41 42 _, ok := c.Get(e.key) 43 assert.False(t, ok) 44 } 45 } 46 47 func TestLRUMaxAge(t *testing.T) { 48 c := New(WithAge(86400)) 49 50 now := time.Now().Unix() 51 expected := now + 86400 52 53 // Add one expired entry 54 c.Set("foo", "bar") 55 c.lru.Back().Value.(*entry).expires = now 56 57 // Reset 58 c.Set("foo", "bar") 59 e := c.lru.Back().Value.(*entry) 60 assert.True(t, e.expires >= now) 61 c.lru.Back().Value.(*entry).expires = now 62 63 // Set a few and verify expiration times 64 for _, s := range entries { 65 c.Set(s.key, s.value) 66 e := c.lru.Back().Value.(*entry) 67 assert.True(t, e.expires >= expected && e.expires <= expected+10) 68 } 69 70 // Make sure we can get them all 71 for _, s := range entries { 72 _, ok := c.Get(s.key) 73 assert.True(t, ok) 74 } 75 76 // Expire all entries 77 for _, s := range entries { 78 le, ok := c.cache[s.key] 79 if assert.True(t, ok) { 80 le.Value.(*entry).expires = now 81 } 82 } 83 84 // Get one expired entry, which should clear all expired entries 85 _, ok := c.Get("3") 86 assert.False(t, ok) 87 assert.Equal(t, c.lru.Len(), 0) 88 } 89 90 func TestLRUpdateOnGet(t *testing.T) { 91 c := New(WithAge(86400), WithUpdateAgeOnGet()) 92 93 now := time.Now().Unix() 94 expires := now + 86400/2 95 96 // Add one expired entry 97 c.Set("foo", "bar") 98 c.lru.Back().Value.(*entry).expires = expires 99 100 _, ok := c.Get("foo") 101 assert.True(t, ok) 102 assert.True(t, c.lru.Back().Value.(*entry).expires > expires) 103 } 104 105 func TestMaxSize(t *testing.T) { 106 c := New(WithSize(2)) 107 // Add one expired entry 108 c.Set("foo", "bar") 109 _, ok := c.Get("foo") 110 assert.True(t, ok) 111 112 c.Set("bar", "foo") 113 c.Set("baz", "foo") 114 115 _, ok = c.Get("foo") 116 assert.False(t, ok) 117 } 118 119 func TestExist(t *testing.T) { 120 c := New(WithSize(1)) 121 c.Set(1, 2) 122 assert.True(t, c.Exist(1)) 123 c.Set(2, 3) 124 assert.False(t, c.Exist(1)) 125 } 126 127 func TestEvict(t *testing.T) { 128 temp := 0 129 evict := func(key any, value any) { 130 temp = key.(int) + value.(int) 131 } 132 133 c := New(WithEvict(evict), WithSize(1)) 134 c.Set(1, 2) 135 c.Set(2, 3) 136 137 assert.Equal(t, temp, 3) 138 } 139 140 func TestSetWithExpire(t *testing.T) { 141 c := New(WithAge(1)) 142 now := time.Now().Unix() 143 144 tenSecBefore := time.Unix(now-10, 0) 145 c.SetWithExpire(1, 2, tenSecBefore) 146 147 // res is expected not to exist, and expires should be empty time.Time 148 res, expires, exist := c.GetWithExpire(1) 149 assert.Equal(t, nil, res) 150 assert.Equal(t, time.Time{}, expires) 151 assert.Equal(t, false, exist) 152 } 153 154 func TestStale(t *testing.T) { 155 c := New(WithAge(1), WithStale(true)) 156 now := time.Now().Unix() 157 158 tenSecBefore := time.Unix(now-10, 0) 159 c.SetWithExpire(1, 2, tenSecBefore) 160 161 res, expires, exist := c.GetWithExpire(1) 162 assert.Equal(t, 2, res) 163 assert.Equal(t, tenSecBefore, expires) 164 assert.Equal(t, true, exist) 165 } 166 167 func TestCloneTo(t *testing.T) { 168 o := New(WithSize(10)) 169 o.Set("1", 1) 170 o.Set("2", 2) 171 172 n := New(WithSize(2)) 173 n.Set("3", 3) 174 n.Set("4", 4) 175 176 o.CloneTo(n) 177 178 assert.False(t, n.Exist("3")) 179 assert.True(t, n.Exist("1")) 180 181 n.Set("5", 5) 182 assert.False(t, n.Exist("1")) 183 }