Debian Codegraph @

util/cacheutil/lrucache_test.go

raw
1 /*
2 Copyright The containerd 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 cacheutil
18
19 import (
20 "fmt"
21 "testing"
22 )
23
24 // TestLRUAdd tests Add API
25 func TestLRUAdd(t *testing.T) {
26 c := NewLRUCache(10)
27
28 key, value := "key1", "abcd"
29 v, _, added := c.Add(key, value)
30 if !added {
31 t.Fatalf("failed to add %q", key)
32 } else if v.(string) != value {
33 t.Fatalf("returned different object for %q; want %q; got %q", key, value, v.(string))
34 }
35
36 key, newvalue := "key1", "dummy"
37 v, _, added = c.Add(key, newvalue)
38 if added || v.(string) != value {
39 t.Fatalf("%q must be originally stored one; want %q; got %q (added:%v)",
40 key, value, v.(string), added)
41 }
42 }
43
44 // TestLRUGet tests Get API
45 func TestLRUGet(t *testing.T) {
46 c := NewLRUCache(10)
47
48 key, value := "key1", "abcd"
49 v, _, added := c.Add(key, value)
50 if !added {
51 t.Fatalf("failed to add %q", key)
52 } else if v.(string) != value {
53 t.Fatalf("returned different object for %q; want %q; got %q", key, value, v.(string))
54 }
55
56 v, _, ok := c.Get(key)
57 if !ok {
58 t.Fatalf("failed to get obj %q (%q)", key, value)
59 } else if v.(string) != value {
60 t.Fatalf("unexpected object for %q; want %q; got %q", key, value, v.(string))
61 }
62 }
63
64 // TestLRURemoe tests Remove API
65 func TestLRURemove(t *testing.T) {
66 var evicted []string
67 c := NewLRUCache(2)
68 c.OnEvicted = func(key string, value interface{}) {
69 evicted = append(evicted, key)
70 }
71 key1, value1 := "key1", "abcd1"
72 _, done1, _ := c.Add(key1, value1)
73 _, done12, _ := c.Get(key1)
74
75 c.Remove(key1)
76 if len(evicted) != 0 {
77 t.Fatalf("no content must be evicted after remove")
78 }
79
80 done1()
81 if len(evicted) != 0 {
82 t.Fatalf("no content must be evicted until all reference are discarded")
83 }
84
85 done12()
86 if len(evicted) != 1 {
87 t.Fatalf("content must be evicted")
88 }
89 if evicted[0] != key1 {
90 t.Fatalf("1st content %q must be evicted but got %q", key1, evicted[0])
91 }
92 }
93
94 // TestLRUEviction tests that eviction occurs when the overflow happens.
95 func TestLRUEviction(t *testing.T) {
96 var evicted []string
97 c := NewLRUCache(2)
98 c.OnEvicted = func(key string, value interface{}) {
99 evicted = append(evicted, key)
100 }
101 key1, value1 := "key1", "abcd1"
102 key2, value2 := "key2", "abcd2"
103 _, done1, _ := c.Add(key1, value1)
104 _, done2, _ := c.Add(key2, value2)
105 _, done22, _ := c.Get(key2)
106
107 if len(evicted) != 0 {
108 t.Fatalf("no content must be evicted after addition")
109 }
110 for i := 0; i < 2; i++ {
111 c.Add(fmt.Sprintf("key-add-%d", i), fmt.Sprintf("abcd-add-%d", i))
112 }
113 if len(evicted) != 0 {
114 t.Fatalf("no content must be evicted after overflow")
115 }
116
117 done1()
118 if len(evicted) != 1 {
119 t.Fatalf("1 content must be evicted")
120 }
121 if evicted[0] != key1 {
122 t.Fatalf("1st content %q must be evicted but got %q", key1, evicted[0])
123 }
124
125 done2() // effective
126 done2() // ignored
127 done2() // ignored
128 if len(evicted) != 1 {
129 t.Fatalf("only 1 content must be evicted")
130 }
131
132 done22()
133 if len(evicted) != 2 {
134 t.Fatalf("2 contents must be evicted")
135 }
136 if evicted[1] != key2 {
137 t.Fatalf("2nd content %q must be evicted but got %q", key2, evicted[1])
138 }
139 }