25
25
class TestLRUCache(tests.TestCase):
26
26
"""Test that LRU cache properly keeps track of entries."""
28
def test_cache_size(self):
29
cache = lru_cache.LRUCache(max_cache=10)
30
self.assertEqual(10, cache.cache_size())
32
cache = lru_cache.LRUCache(max_cache=256)
33
self.assertEqual(256, cache.cache_size())
36
self.assertEqual(512, cache.cache_size())
28
38
def test_missing(self):
29
39
cache = lru_cache.LRUCache(max_cache=10)
92
90
# 'foo' is now most recent, so final cleanup will call it last
95
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')], cleanup_called)
93
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
97
96
def test_cleanup_on_replace(self):
98
97
"""Replacing an object should cleanup the old value."""
192
self.assertEqual([1, 2, 3, 4, 5], list(cache._queue))
191
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
194
193
# Now access some randomly
199
self.assertEqual([1, 2, 3, 4, 5, 2, 5, 3, 2], list(cache._queue))
200
self.assertEqual({1:1, 2:3, 3:2, 4:1, 5:2}, cache._refcount)
202
# Compacting should save the last position
203
cache._compact_queue()
204
self.assertEqual([1, 4, 5, 3, 2], list(cache._queue))
205
self.assertEqual({1:1, 2:1, 3:1, 4:1, 5:1}, cache._refcount)
198
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
207
200
def test_get(self):
208
201
cache = lru_cache.LRUCache(max_cache=5)
213
206
self.assertIs(None, cache.get(3))
215
208
self.assertIs(obj, cache.get(3, obj))
209
self.assertEqual([2, 1], [n.key for n in cache._walk_lru()])
210
self.assertEqual(10, cache.get(1))
211
self.assertEqual([1, 2], [n.key for n in cache._walk_lru()])
217
213
def test_keys(self):
218
214
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
293
288
self.assertEqual(0, cache._value_size)
294
289
cache.add('my key', 'my value text')
295
290
self.assertEqual(13, cache._value_size)
296
cache._remove('my key')
291
node = cache._cache['my key']
292
cache._remove_node(node)
297
293
self.assertEqual(0, cache._value_size)
299
295
def test_no_add_over_size(self):
300
296
"""Adding a large value may not be cached at all."""
301
297
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
302
298
self.assertEqual(0, cache._value_size)
303
self.assertEqual({}, cache._cache)
299
self.assertEqual({}, cache.items())
304
300
cache.add('test', 'key')
305
301
self.assertEqual(3, cache._value_size)
306
self.assertEqual({'test':'key'}, cache._cache)
302
self.assertEqual({'test': 'key'}, cache.items())
307
303
cache.add('test2', 'key that is too big')
308
304
self.assertEqual(3, cache._value_size)
309
self.assertEqual({'test':'key'}, cache._cache)
305
self.assertEqual({'test':'key'}, cache.items())
310
306
# If we would add a key, only to cleanup and remove all cached entries,
311
307
# then obviously that value should not be stored
312
308
cache.add('test3', 'bigkey')
313
309
self.assertEqual(3, cache._value_size)
314
self.assertEqual({'test':'key'}, cache._cache)
310
self.assertEqual({'test':'key'}, cache.items())
316
312
cache.add('test4', 'bikey')
317
313
self.assertEqual(3, cache._value_size)
318
self.assertEqual({'test':'key'}, cache._cache)
314
self.assertEqual({'test':'key'}, cache.items())
316
def test_no_add_over_size_cleanup(self):
317
"""If a large value is not cached, we will call cleanup right away."""
319
def cleanup(key, value):
320
cleanup_calls.append((key, value))
322
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
323
self.assertEqual(0, cache._value_size)
324
self.assertEqual({}, cache.items())
325
cache.add('test', 'key that is too big', cleanup=cleanup)
327
self.assertEqual(0, cache._value_size)
328
self.assertEqual({}, cache.items())
329
# and cleanup was called
330
self.assertEqual([('test', 'key that is too big')], cleanup_calls)
320
332
def test_adding_clears_cache_based_on_size(self):
321
333
"""The cache is cleared in LRU order until small enough"""
341
353
cache.add('key4', 'value234') # 8 chars, over limit
342
354
# We have to remove 3 keys to get back under limit
343
355
self.assertEqual(8, cache._value_size)
344
self.assertEqual({'key4':'value234'}, cache._cache)
356
self.assertEqual({'key4':'value234'}, cache.items())
346
358
def test_custom_sizes(self):
347
359
def size_of_list(lst):
357
369
cache.add('key4', ['value', '234']) # 8 chars, over limit
358
370
# We have to remove 3 keys to get back under limit
359
371
self.assertEqual(8, cache._value_size)
360
self.assertEqual({'key4':['value', '234']}, cache._cache)
372
self.assertEqual({'key4':['value', '234']}, cache.items())
362
374
def test_cleanup(self):
363
375
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)