Debian Codegraph @

util/cacheutil/ttlcache_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 "sync"
21 "testing"
22 "time"
23 )
24
25 // TestTTLAdd tests Add API
26 func TestTTLAdd(t *testing.T) {
27 c := NewTTLCache(time.Hour)
28
29 key, value := "key1", "abcd"
30 v, _, added := c.Add(key, value)
31 if !added {
32 t.Fatalf("failed to add %q", key)
33 } else if v.(string) != value {
34 t.Fatalf("returned different object for %q; want %q; got %q", key, value, v.(string))
35 }
36
37 key, newvalue := "key1", "dummy"
38 v, _, added = c.Add(key, newvalue)
39 if added || v.(string) != value {
40 t.Fatalf("%q must be originally stored one; want %q; got %q (added:%v)",
41 key, value, v.(string), added)
42 }
43 }
44
45 // TestTTLGet tests Get API
46 func TestTTLGet(t *testing.T) {
47 c := NewTTLCache(time.Hour)
48
49 key, value := "key1", "abcd"
50 v, _, added := c.Add(key, value)
51 if !added {
52 t.Fatalf("failed to add %q", key)
53 } else if v.(string) != value {
54 t.Fatalf("returned different object for %q; want %q; got %q", key, value, v.(string))
55 }
56
57 v, _, ok := c.Get(key)
58 if !ok {
59 t.Fatalf("failed to get obj %q (%q)", key, value)
60 } else if v.(string) != value {
61 t.Fatalf("unexpected object for %q; want %q; got %q", key, value, v.(string))
62 }
63 }
64
65 // TestTTLRemove tests Remove API
66 func TestTTLRemove(t *testing.T) {
67 var evicted []string
68 c := NewTTLCache(time.Hour)
69 c.OnEvicted = func(key string, value interface{}) {
70 evicted = append(evicted, key)
71 }
72 key1, value1 := "key1", "abcd1"
73 _, done1, _ := c.Add(key1, value1)
74 _, done12, _ := c.Get(key1)
75
76 c.Remove(key1)
77 if len(evicted) != 0 {
78 t.Fatalf("no content must be evicted after remove")
79 }
80
81 done1()
82 if len(evicted) != 0 {
83 t.Fatalf("no content must be evicted until all reference are discarded")
84 }
85
86 done12()
87 if len(evicted) != 1 {
88 t.Fatalf("content must be evicted")
89 }
90 if evicted[0] != key1 {
91 t.Fatalf("1st content %q must be evicted but got %q", key1, evicted[0])
92 }
93 }
94
95 // TestTTLRemoveOverwritten tests old gc doesn't affect overwritten content
96 func TestTTLRemoveOverwritten(t *testing.T) {
97 var evicted []string
98 c := NewTTLCache(3 * time.Second)
99 c.OnEvicted = func(key string, value interface{}) {
100 evicted = append(evicted, key)
101 }
102 key1, value1 := "key1", "abcd1"
103 _, done1, _ := c.Add(key1, value1)
104 done1()
105 c.Remove(key1) // remove key1 as soon as possible
106
107 // add another content with a new key
108 time.Sleep(2 * time.Second)
109 value12 := value1 + "!"
110 _, done12, _ := c.Add(key1, value12)
111 time.Sleep(2 * time.Second)
112 // spent 4 sec (larger than ttl) since the previous key1 was added.
113 // but the *newly-added* key1 hasn't been expierd yet so key1 must remain.
114 v1, done122, getOK := c.Get(key1)
115 if !getOK {
116 t.Fatalf("unexpected eviction")
117 }
118 if s1, ok := v1.(string); !ok || s1 != value12 {
119 t.Fatalf("unexpected content %q(%v) != %q", s1, ok, value12)
120 }
121
122 time.Sleep(2 * time.Second)
123 done122()
124 done12()
125 // spent 4 sec since the new key1 was added. This should be expierd.
126 if _, _, ok := c.Get(key1); ok {
127 t.Fatalf("%q must be expierd but remaining", key1)
128 }
129 }
130
131 // TestTTLEviction tests contents are evicted after TTL witout remaining reference.
132 func TestTTLEviction(t *testing.T) {
133 var (
134 evicted []string
135 evictedMu sync.Mutex
136 )
137 c := NewTTLCache(time.Second)
138 c.OnEvicted = func(key string, value interface{}) {
139 evictedMu.Lock()
140 evicted = append(evicted, key)
141 evictedMu.Unlock()
142 }
143 key1, value1 := "key1", "abcd1"
144 key2, value2 := "key2", "abcd2"
145 _, done1, _ := c.Add(key1, value1)
146 done1() // evict key1 on expiering ttl
147 _, done2, _ := c.Add(key2, value2)
148 _, done22, _ := c.Get(key2) // hold reference of key2 to prevent eviction
149 time.Sleep(3 * time.Second) // wait until elements reach ttl
150
151 evictedMu.Lock()
152 if len(evicted) != 1 {
153 t.Fatalf("1 content must be removed")
154 }
155 if evicted[0] != key1 {
156 t.Fatalf("1st content %q must be evicted but got %q", key1, evicted[0])
157 }
158 evictedMu.Unlock()
159
160 done2() // effective
161 done2() // ignored
162 done2() // ignored
163 evictedMu.Lock()
164 if len(evicted) != 1 {
165 t.Fatalf("only 1 content must be evicted")
166 }
167 evictedMu.Unlock()
168
169 done22()
170 evictedMu.Lock()
171 if len(evicted) != 2 {
172 t.Fatalf("2 contents must be evicted")
173 }
174 if evicted[1] != key2 {
175 t.Fatalf("2nd content %q must be evicted but got %q", key2, evicted[1])
176 }
177 evictedMu.Unlock()
178 }