github.com/mailgun/holster/v4@v4.20.0/collections/lru_cache_test.go (about) 1 /* 2 Copyright 2017 Mailgun Technologies Inc 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 16 This work is derived from github.com/golang/groupcache/lru 17 */ 18 package collections_test 19 20 import ( 21 "fmt" 22 "testing" 23 24 "github.com/mailgun/holster/v4/clock" 25 "github.com/mailgun/holster/v4/collections" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestLRUCache(t *testing.T) { 31 cache := collections.NewLRUCache(5) 32 33 // Confirm non existent key 34 value, ok := cache.Get("key") 35 assert.Nil(t, value) 36 assert.Equal(t, false, ok) 37 38 // Confirm add new value 39 cache.Add("key", "value") 40 value, ok = cache.Get("key") 41 assert.Equal(t, "value", value) 42 assert.Equal(t, true, ok) 43 44 // Confirm overwrite current value correctly 45 cache.Add("key", "new") 46 value, ok = cache.Get("key") 47 assert.Equal(t, "new", value) 48 assert.Equal(t, true, ok) 49 50 // Confirm removal works 51 cache.Remove("key") 52 value, ok = cache.Get("key") 53 assert.Nil(t, value) 54 assert.Equal(t, false, ok) 55 56 // Stats should be correct 57 stats := cache.Stats() 58 assert.Equal(t, int64(2), stats.Hit) 59 assert.Equal(t, int64(2), stats.Miss) 60 assert.Equal(t, int64(0), stats.Size) 61 } 62 63 func TestLRUCacheWithTTL(t *testing.T) { 64 cache := collections.NewLRUCache(5) 65 start := clock.Now() 66 clock.Freeze(start) 67 68 cache.AddWithTTL("key", "value", 10*clock.Nanosecond) 69 70 clock.Advance(10 * clock.Nanosecond) 71 value, ok := cache.Get("key") 72 assert.Equal(t, "value", value) 73 assert.Equal(t, true, ok) 74 75 clock.Advance(clock.Nanosecond) 76 value, ok = cache.Get("key") 77 assert.Nil(t, value) 78 assert.Equal(t, false, ok) 79 } 80 81 func TestLRUCacheEach(t *testing.T) { 82 cache := collections.NewLRUCache(5) 83 84 cache.Add("1", 1) 85 cache.Add("2", 2) 86 cache.Add("3", 3) 87 cache.Add("4", 4) 88 cache.Add("5", 5) 89 90 var count int 91 // concurrency of 0, means no concurrency (This test will not develop a race condition) 92 errs := cache.Each(0, func(key interface{}, value interface{}) error { 93 count++ 94 return nil 95 }) 96 assert.Nil(t, errs) 97 assert.Equal(t, 5, count) 98 99 stats := cache.Stats() 100 assert.Equal(t, int64(0), stats.Hit) 101 assert.Equal(t, int64(0), stats.Miss) 102 assert.Equal(t, int64(5), stats.Size) 103 } 104 105 func TestLRUCacheMap(t *testing.T) { 106 cache := collections.NewLRUCache(5) 107 108 cache.Add("1", 1) 109 cache.Add("2", 2) 110 cache.Add("3", 3) 111 cache.Add("4", 4) 112 cache.Add("5", 5) 113 114 var count int 115 cache.Map(func(item *collections.CacheItem) bool { 116 count++ 117 if v, ok := item.Value.(int); ok { 118 // Remove value 3 119 if v == 3 { 120 return false 121 } 122 } 123 return true 124 }) 125 assert.Equal(t, 5, count) 126 assert.Equal(t, 4, cache.Size()) 127 128 for _, item := range []int{1, 2, 4, 5} { 129 v, ok := cache.Get(fmt.Sprintf("%d", item)) 130 require.True(t, ok) 131 assert.Equal(t, item, v.(int)) 132 } 133 }