-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest_lru_cache.py
More file actions
211 lines (160 loc) · 6.78 KB
/
test_lru_cache.py
File metadata and controls
211 lines (160 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright 2022, Optimizely
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import time
from unittest import TestCase
from optimizely.odp.lru_cache import LRUCache, OptimizelySegmentsCache
class LRUCacheTest(TestCase):
def test_min_config(self):
cache = LRUCache(1000, 2000)
self.assertEqual(1000, cache.capacity)
self.assertEqual(2000, cache.timeout)
cache = LRUCache(0, 0)
self.assertEqual(0, cache.capacity)
self.assertEqual(0, cache.timeout)
def test_save_and_lookup(self):
max_size = 2
cache = LRUCache(max_size, 1000)
self.assertIsNone(cache.peek(1))
cache.save(1, 100) # [1]
cache.save(2, 200) # [1, 2]
cache.save(3, 300) # [2, 3]
self.assertIsNone(cache.peek(1))
self.assertEqual(200, cache.peek(2))
self.assertEqual(300, cache.peek(3))
cache.save(2, 201) # [3, 2]
cache.save(1, 101) # [2, 1]
self.assertEqual(101, cache.peek(1))
self.assertEqual(201, cache.peek(2))
self.assertIsNone(cache.peek(3))
self.assertIsNone(cache.lookup(3)) # [2, 1]
self.assertEqual(201, cache.lookup(2)) # [1, 2]
cache.save(3, 302) # [2, 3]
self.assertIsNone(cache.peek(1))
self.assertEqual(201, cache.peek(2))
self.assertEqual(302, cache.peek(3))
self.assertEqual(302, cache.lookup(3)) # [2, 3]
cache.save(1, 103) # [3, 1]
self.assertEqual(103, cache.peek(1))
self.assertIsNone(cache.peek(2))
self.assertEqual(302, cache.peek(3))
self.assertEqual(len(cache.map), max_size)
self.assertEqual(len(cache.map), cache.capacity)
def test_size_zero(self):
cache = LRUCache(0, 1000)
self.assertIsNone(cache.lookup(1))
cache.save(1, 100) # [1]
self.assertIsNone(cache.lookup(1))
def test_size_less_than_zero(self):
cache = LRUCache(-2, 1000)
self.assertIsNone(cache.lookup(1))
cache.save(1, 100) # [1]
self.assertIsNone(cache.lookup(1))
def test_timeout(self):
max_timeout = .5
cache = LRUCache(1000, max_timeout)
cache.save(1, 100) # [1]
cache.save(2, 200) # [1, 2]
cache.save(3, 300) # [1, 2, 3]
time.sleep(1.1) # wait to expire
cache.save(4, 400) # [1, 2, 3, 4]
cache.save(1, 101) # [2, 3, 4, 1]
self.assertEqual(101, cache.lookup(1)) # [4, 1]
self.assertIsNone(cache.lookup(2))
self.assertIsNone(cache.lookup(3))
self.assertEqual(400, cache.lookup(4))
def test_timeout_zero(self):
max_timeout = 0
cache = LRUCache(1000, max_timeout)
cache.save(1, 100) # [1]
cache.save(2, 200) # [1, 2]
time.sleep(1) # wait to expire
self.assertEqual(100, cache.lookup(1), "should not expire when timeout is 0")
self.assertEqual(200, cache.lookup(2))
def test_timeout_less_than_zero(self):
max_timeout = -2
cache = LRUCache(1000, max_timeout)
cache.save(1, 100) # [1]
cache.save(2, 200) # [1, 2]
time.sleep(1) # wait to expire
self.assertEqual(100, cache.lookup(1), "should not expire when timeout is less than 0")
self.assertEqual(200, cache.lookup(2))
def test_reset(self):
cache = LRUCache(1000, 600)
cache.save('wow', 'great')
cache.save('tow', 'freight')
self.assertEqual(cache.lookup('wow'), 'great')
self.assertEqual(len(cache.map), 2)
cache.reset()
self.assertEqual(cache.lookup('wow'), None)
self.assertEqual(len(cache.map), 0)
cache.save('cow', 'crate')
self.assertEqual(cache.lookup('cow'), 'crate')
def test_remove_non_existent_key(self):
cache = LRUCache(3, 1000)
cache.save("1", 100)
cache.save("2", 200)
cache.remove("3") # Doesn't exist
self.assertEqual(cache.lookup("1"), 100)
self.assertEqual(cache.lookup("2"), 200)
def test_remove_existing_key(self):
cache = LRUCache(3, 1000)
cache.save("1", 100)
cache.save("2", 200)
cache.save("3", 300)
self.assertEqual(cache.lookup("1"), 100)
self.assertEqual(cache.lookup("2"), 200)
self.assertEqual(cache.lookup("3"), 300)
cache.remove("2")
self.assertEqual(cache.lookup("1"), 100)
self.assertIsNone(cache.lookup("2"))
self.assertEqual(cache.lookup("3"), 300)
def test_remove_from_zero_sized_cache(self):
cache = LRUCache(0, 1000)
cache.save("1", 100)
cache.remove("1")
self.assertIsNone(cache.lookup("1"))
def test_remove_and_add_back(self):
cache = LRUCache(3, 1000)
cache.save("1", 100)
cache.save("2", 200)
cache.save("3", 300)
cache.remove("2")
cache.save("2", 201)
self.assertEqual(cache.lookup("1"), 100)
self.assertEqual(cache.lookup("2"), 201)
self.assertEqual(cache.lookup("3"), 300)
def test_thread_safety(self):
import threading
max_size = 100
cache = LRUCache(max_size, 1000)
for i in range(1, max_size + 1):
cache.save(str(i), i * 100)
def remove_key(k):
cache.remove(str(k))
threads = []
for i in range(1, (max_size // 2) + 1):
thread = threading.Thread(target=remove_key, args=(i,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
for i in range(1, max_size + 1):
if i <= max_size // 2:
self.assertIsNone(cache.lookup(str(i)))
else:
self.assertEqual(cache.lookup(str(i)), i * 100)
self.assertEqual(len(cache.map), max_size // 2)
# type checker test
# confirm that LRUCache matches OptimizelySegmentsCache protocol
_: OptimizelySegmentsCache = LRUCache(0, 0)