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())
38
28
def test_missing(self):
39
29
cache = lru_cache.LRUCache(max_cache=10)
90
92
# 'foo' is now most recent, so final cleanup will call it last
93
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
95
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')], cleanup_called)
96
97
def test_cleanup_on_replace(self):
97
98
"""Replacing an object should cleanup the old value."""
191
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
192
self.assertEqual([1, 2, 3, 4, 5], list(cache._queue))
193
194
# Now access some randomly
198
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
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)
200
207
def test_get(self):
201
208
cache = lru_cache.LRUCache(max_cache=5)
206
213
self.assertIs(None, cache.get(3))
208
215
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()])
213
217
def test_keys(self):
214
218
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
288
293
self.assertEqual(0, cache._value_size)
289
294
cache.add('my key', 'my value text')
290
295
self.assertEqual(13, cache._value_size)
291
node = cache._cache['my key']
292
cache._remove_node(node)
296
cache._remove('my key')
293
297
self.assertEqual(0, cache._value_size)
295
299
def test_no_add_over_size(self):
296
300
"""Adding a large value may not be cached at all."""
297
301
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
298
302
self.assertEqual(0, cache._value_size)
299
self.assertEqual({}, cache.items())
303
self.assertEqual({}, cache._cache)
300
304
cache.add('test', 'key')
301
305
self.assertEqual(3, cache._value_size)
302
self.assertEqual({'test': 'key'}, cache.items())
306
self.assertEqual({'test':'key'}, cache._cache)
303
307
cache.add('test2', 'key that is too big')
304
308
self.assertEqual(3, cache._value_size)
305
self.assertEqual({'test':'key'}, cache.items())
309
self.assertEqual({'test':'key'}, cache._cache)
306
310
# If we would add a key, only to cleanup and remove all cached entries,
307
311
# then obviously that value should not be stored
308
312
cache.add('test3', 'bigkey')
309
313
self.assertEqual(3, cache._value_size)
310
self.assertEqual({'test':'key'}, cache.items())
314
self.assertEqual({'test':'key'}, cache._cache)
312
316
cache.add('test4', 'bikey')
313
317
self.assertEqual(3, cache._value_size)
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)
318
self.assertEqual({'test':'key'}, cache._cache)
332
320
def test_adding_clears_cache_based_on_size(self):
333
321
"""The cache is cleared in LRU order until small enough"""
353
341
cache.add('key4', 'value234') # 8 chars, over limit
354
342
# We have to remove 3 keys to get back under limit
355
343
self.assertEqual(8, cache._value_size)
356
self.assertEqual({'key4':'value234'}, cache.items())
344
self.assertEqual({'key4':'value234'}, cache._cache)
358
346
def test_custom_sizes(self):
359
347
def size_of_list(lst):
369
357
cache.add('key4', ['value', '234']) # 8 chars, over limit
370
358
# We have to remove 3 keys to get back under limit
371
359
self.assertEqual(8, cache._value_size)
372
self.assertEqual({'key4':['value', '234']}, cache.items())
360
self.assertEqual({'key4':['value', '234']}, cache._cache)
374
362
def test_cleanup(self):
375
363
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)