github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/cache/lruexpirecache_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 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 17 package cache 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/google/go-cmp/cmp" 24 testingclock "k8s.io/utils/clock/testing" 25 ) 26 27 func expectEntry(t *testing.T, c *LRUExpireCache, key interface{}, value interface{}) { 28 t.Helper() 29 result, ok := c.Get(key) 30 if !ok || result != value { 31 t.Errorf("Expected cache[%v]: %v, got %v", key, value, result) 32 } 33 } 34 35 func expectNotEntry(t *testing.T, c *LRUExpireCache, key interface{}) { 36 t.Helper() 37 if result, ok := c.Get(key); ok { 38 t.Errorf("Expected cache[%v] to be empty, got %v", key, result) 39 } 40 } 41 42 // Note: Check keys before checking individual entries, because Get() changes 43 // the eviction list. 44 func assertKeys(t *testing.T, gotKeys, wantKeys []interface{}) { 45 t.Helper() 46 if diff := cmp.Diff(gotKeys, wantKeys); diff != "" { 47 t.Errorf("Wrong result for keys: diff (-got +want):\n%s", diff) 48 } 49 } 50 51 func TestSimpleGet(t *testing.T) { 52 c := NewLRUExpireCache(10) 53 c.Add("long-lived", "12345", 10*time.Hour) 54 55 assertKeys(t, c.Keys(), []interface{}{"long-lived"}) 56 57 expectEntry(t, c, "long-lived", "12345") 58 } 59 60 func TestSimpleRemove(t *testing.T) { 61 c := NewLRUExpireCache(10) 62 c.Add("long-lived", "12345", 10*time.Hour) 63 c.Remove("long-lived") 64 65 assertKeys(t, c.Keys(), []interface{}{}) 66 67 expectNotEntry(t, c, "long-lived") 68 } 69 70 func TestExpiredGet(t *testing.T) { 71 fakeClock := testingclock.NewFakeClock(time.Now()) 72 c := NewLRUExpireCacheWithClock(10, fakeClock) 73 c.Add("short-lived", "12345", 1*time.Millisecond) 74 // ensure the entry expired 75 fakeClock.Step(2 * time.Millisecond) 76 77 // Keys() should not return expired keys. 78 assertKeys(t, c.Keys(), []interface{}{}) 79 80 expectNotEntry(t, c, "short-lived") 81 } 82 83 func TestLRUOverflow(t *testing.T) { 84 c := NewLRUExpireCache(4) 85 c.Add("elem1", "1", 10*time.Hour) 86 c.Add("elem2", "2", 10*time.Hour) 87 c.Add("elem3", "3", 10*time.Hour) 88 c.Add("elem4", "4", 10*time.Hour) 89 c.Add("elem5", "5", 10*time.Hour) 90 91 assertKeys(t, c.Keys(), []interface{}{"elem2", "elem3", "elem4", "elem5"}) 92 93 expectNotEntry(t, c, "elem1") 94 expectEntry(t, c, "elem2", "2") 95 expectEntry(t, c, "elem3", "3") 96 expectEntry(t, c, "elem4", "4") 97 expectEntry(t, c, "elem5", "5") 98 } 99 100 func TestAddBringsToFront(t *testing.T) { 101 c := NewLRUExpireCache(4) 102 c.Add("elem1", "1", 10*time.Hour) 103 c.Add("elem2", "2", 10*time.Hour) 104 c.Add("elem3", "3", 10*time.Hour) 105 c.Add("elem4", "4", 10*time.Hour) 106 107 c.Add("elem1", "1-new", 10*time.Hour) 108 109 c.Add("elem5", "5", 10*time.Hour) 110 111 assertKeys(t, c.Keys(), []interface{}{"elem3", "elem4", "elem1", "elem5"}) 112 113 expectNotEntry(t, c, "elem2") 114 expectEntry(t, c, "elem1", "1-new") 115 expectEntry(t, c, "elem3", "3") 116 expectEntry(t, c, "elem4", "4") 117 expectEntry(t, c, "elem5", "5") 118 } 119 120 func TestGetBringsToFront(t *testing.T) { 121 c := NewLRUExpireCache(4) 122 c.Add("elem1", "1", 10*time.Hour) 123 c.Add("elem2", "2", 10*time.Hour) 124 c.Add("elem3", "3", 10*time.Hour) 125 c.Add("elem4", "4", 10*time.Hour) 126 127 c.Get("elem1") 128 129 c.Add("elem5", "5", 10*time.Hour) 130 131 assertKeys(t, c.Keys(), []interface{}{"elem3", "elem4", "elem1", "elem5"}) 132 133 expectNotEntry(t, c, "elem2") 134 expectEntry(t, c, "elem1", "1") 135 expectEntry(t, c, "elem3", "3") 136 expectEntry(t, c, "elem4", "4") 137 expectEntry(t, c, "elem5", "5") 138 } 139 140 func TestLRUKeys(t *testing.T) { 141 c := NewLRUExpireCache(4) 142 c.Add("elem1", "1", 10*time.Hour) 143 c.Add("elem2", "2", 10*time.Hour) 144 c.Add("elem3", "3", 10*time.Hour) 145 c.Add("elem4", "4", 10*time.Hour) 146 147 assertKeys(t, c.Keys(), []interface{}{"elem1", "elem2", "elem3", "elem4"}) 148 }