| 1 |
/* |
| 2 |
Copyright 2021 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 |
Copyright 2013 Google Inc. |
| 18 |
|
| 19 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 |
you may not use this file except in compliance with the License. |
| 21 |
You may obtain a copy of the License at |
| 22 |
|
| 23 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 24 |
|
| 25 |
Unless required by applicable law or agreed to in writing, software |
| 26 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 27 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 |
See the License for the specific language governing permissions and |
| 29 |
limitations under the License. |
| 30 |
*/ |
| 31 |
|
| 32 |
package lru |
| 33 |
|
| 34 |
import ( |
| 35 |
"testing" |
| 36 |
"time" |
| 37 |
) |
| 38 |
|
| 39 |
type simpleStruct struct { |
| 40 |
int |
| 41 |
string |
| 42 |
} |
| 43 |
|
| 44 |
type complexStruct struct { |
| 45 |
int |
| 46 |
simpleStruct |
| 47 |
} |
| 48 |
|
| 49 |
var getTests = []struct { |
| 50 |
name string |
| 51 |
keyToAdd any |
| 52 |
keyToGet any |
| 53 |
expectedOk bool |
| 54 |
}{ |
| 55 |
{"string_hit", "myKey", "myKey", true}, |
| 56 |
{"string_miss", "myKey", "nonsense", false}, |
| 57 |
{"simple_struct_hit", simpleStruct{1, "two"}, simpleStruct{1, "two"}, true}, |
| 58 |
{"simple_struct_miss", simpleStruct{1, "two"}, simpleStruct{0, "noway"}, false}, |
| 59 |
{"complex_struct_hit", complexStruct{1, simpleStruct{2, "three"}}, |
| 60 |
complexStruct{1, simpleStruct{2, "three"}}, true}, |
| 61 |
} |
| 62 |
|
| 63 |
func TestGet(t *testing.T) { |
| 64 |
for _, tt := range getTests { |
| 65 |
lru := New(0) |
| 66 |
lru.Add(tt.keyToAdd, 1234) |
| 67 |
val, ok := lru.Get(tt.keyToGet) |
| 68 |
if ok != tt.expectedOk { |
| 69 |
t.Fatalf("%s: cache hit = %v; want %v", tt.name, ok, !ok) |
| 70 |
} else if ok && val != 1234 { |
| 71 |
t.Fatalf("%s expected get to return 1234 but got %v", tt.name, val) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestRemove(t *testing.T) { |
| 77 |
lru := New(0) |
| 78 |
lru.Add("myKey", 1234) |
| 79 |
if val, ok := lru.Get("myKey"); !ok { |
| 80 |
t.Fatal("TestRemove returned no match") |
| 81 |
} else if val != 1234 { |
| 82 |
t.Fatalf("TestRemove failed. Expected %d, got %v", 1234, val) |
| 83 |
} |
| 84 |
|
| 85 |
lru.Remove("myKey") |
| 86 |
if _, ok := lru.Get("myKey"); ok { |
| 87 |
t.Fatal("TestRemove returned a removed entry") |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestGetRace(_ *testing.T) { |
| 92 |
// size to force eviction and exercise next,curr,prev list behavior |
| 93 |
lru := New(25) |
| 94 |
|
| 95 |
stop := make(chan struct{}) |
| 96 |
defer close(stop) |
| 97 |
|
| 98 |
// set up parallel getters/writers on 2x len keys |
| 99 |
for key := range 50 { |
| 100 |
go func(key int) { |
| 101 |
for { |
| 102 |
select { |
| 103 |
case <-stop: |
| 104 |
return |
| 105 |
default: |
| 106 |
lru.Get(key) |
| 107 |
lru.Add(key, 1) |
| 108 |
lru.Get(key) |
| 109 |
} |
| 110 |
} |
| 111 |
}(key) |
| 112 |
} |
| 113 |
// let them run |
| 114 |
time.Sleep(5 * time.Second) |
| 115 |
} |
| 116 |
|
| 117 |
func TestEviction(t *testing.T) { |
| 118 |
var seenKey Key |
| 119 |
var seenVal any |
| 120 |
|
| 121 |
lru := NewWithEvictionFunc(1, func(key Key, value any) { |
| 122 |
seenKey = key |
| 123 |
seenVal = value |
| 124 |
}) |
| 125 |
|
| 126 |
lru.Add(1, 2) |
| 127 |
lru.Add(3, 4) |
| 128 |
|
| 129 |
if seenKey != 1 || seenVal != 2 { |
| 130 |
t.Errorf("unexpected eviction data: key=%v val=%v", seenKey, seenVal) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestSetEviction(t *testing.T) { |
| 135 |
var seenKey Key |
| 136 |
var seenVal any |
| 137 |
|
| 138 |
lru := New(1) |
| 139 |
|
| 140 |
err := lru.SetEvictionFunc(func(key Key, value any) { |
| 141 |
seenKey = key |
| 142 |
seenVal = value |
| 143 |
}) |
| 144 |
|
| 145 |
if err != nil { |
| 146 |
t.Errorf("unexpected error setting eviction function: %v", err) |
| 147 |
} |
| 148 |
|
| 149 |
lru.Add(1, 2) |
| 150 |
lru.Add(3, 4) |
| 151 |
|
| 152 |
if seenKey != 1 || seenVal != 2 { |
| 153 |
t.Errorf("unexpected eviction data: key=%v val=%v", seenKey, seenVal) |
| 154 |
} |
| 155 |
|
| 156 |
err = lru.SetEvictionFunc(func(_ Key, _ any) {}) |
| 157 |
if err == nil { |
| 158 |
t.Errorf("expected error but got none") |
| 159 |
} |
| 160 |
} |