13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the lru_cache module."""
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)
36
46
self.failUnless('foo' in cache)
37
47
self.failIf('bar' in cache)
49
def test_map_None(self):
50
# Make sure that we can properly map None as a key.
51
cache = lru_cache.LRUCache(max_cache=10)
52
self.failIf(None in cache)
54
self.assertEqual(1, cache[None])
56
self.assertEqual(2, cache[None])
57
# Test the various code paths of __getitem__, to make sure that we can
58
# handle when None is the key for the LRU and the MRU
64
self.assertEqual([None, 1], [n.key for n in cache._walk_lru()])
66
def test_add__null_key(self):
67
cache = lru_cache.LRUCache(max_cache=10)
68
self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
39
70
def test_overflow(self):
40
71
"""Adding extra entries will pop out old ones."""
41
72
cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
92
111
# 'foo' is now most recent, so final cleanup will call it last
95
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')], cleanup_called)
114
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
97
117
def test_cleanup_on_replace(self):
98
118
"""Replacing an object should cleanup the old value."""
108
128
self.assertEqual([(2, 20)], cleanup_called)
109
129
self.assertEqual(25, cache[2])
111
131
# Even __setitem__ should make sure cleanup() is called
113
133
self.assertEqual([(2, 20), (2, 25)], cleanup_called)
135
def test_cleanup_error_maintains_linked_list(self):
137
def cleanup_func(key, val):
138
cleanup_called.append((key, val))
139
raise ValueError('failure during cleanup')
141
cache = lru_cache.LRUCache(max_cache=10)
143
cache.add(i, i, cleanup=cleanup_func)
144
for i in xrange(10, 20):
145
self.assertRaises(ValueError,
146
cache.add, i, i, cleanup=cleanup_func)
148
self.assertEqual([(i, i) for i in xrange(10)], cleanup_called)
150
self.assertEqual(range(19, 9, -1), [n.key for n in cache._walk_lru()])
152
def test_cleanup_during_replace_still_replaces(self):
154
def cleanup_func(key, val):
155
cleanup_called.append((key, val))
156
raise ValueError('failure during cleanup')
158
cache = lru_cache.LRUCache(max_cache=10)
160
cache.add(i, i, cleanup=cleanup_func)
161
self.assertRaises(ValueError,
162
cache.add, 1, 20, cleanup=cleanup_func)
163
# We also still update the recent access to this node
164
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
165
[n.key for n in cache._walk_lru()])
166
self.assertEqual(20, cache[1])
168
self.assertEqual([(1, 1)], cleanup_called)
169
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
170
[n.key for n in cache._walk_lru()])
115
172
def test_len(self):
116
173
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
141
198
self.assertEqual(10, len(cache))
199
self.assertEqual([11, 10, 9, 1, 8, 7, 6, 5, 4, 3],
200
[n.key for n in cache._walk_lru()])
143
202
def test_cleanup_shrinks_to_after_clean_count(self):
144
203
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
192
self.assertEqual([1, 2, 3, 4, 5], list(cache._queue))
251
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
194
253
# 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)
258
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
207
260
def test_get(self):
208
261
cache = lru_cache.LRUCache(max_cache=5)
213
266
self.assertIs(None, cache.get(3))
215
268
self.assertIs(obj, cache.get(3, obj))
269
self.assertEqual([2, 1], [n.key for n in cache._walk_lru()])
270
self.assertEqual(10, cache.get(1))
271
self.assertEqual([1, 2], [n.key for n in cache._walk_lru()])
217
273
def test_keys(self):
218
274
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
278
334
def test_basic_init(self):
279
335
cache = lru_cache.LRUSizeCache()
280
336
self.assertEqual(2048, cache._max_cache)
281
self.assertEqual(4*2048, cache._compact_queue_length)
282
337
self.assertEqual(int(cache._max_size*0.8), cache._after_cleanup_size)
283
338
self.assertEqual(0, cache._value_size)
340
def test_add__null_key(self):
341
cache = lru_cache.LRUSizeCache()
342
self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
285
344
def test_add_tracks_size(self):
286
345
cache = lru_cache.LRUSizeCache()
287
346
self.assertEqual(0, cache._value_size)
293
352
self.assertEqual(0, cache._value_size)
294
353
cache.add('my key', 'my value text')
295
354
self.assertEqual(13, cache._value_size)
296
cache._remove('my key')
355
node = cache._cache['my key']
356
cache._remove_node(node)
297
357
self.assertEqual(0, cache._value_size)
299
359
def test_no_add_over_size(self):
300
360
"""Adding a large value may not be cached at all."""
301
361
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
302
362
self.assertEqual(0, cache._value_size)
303
self.assertEqual({}, cache._cache)
363
self.assertEqual({}, cache.items())
304
364
cache.add('test', 'key')
305
365
self.assertEqual(3, cache._value_size)
306
self.assertEqual({'test':'key'}, cache._cache)
366
self.assertEqual({'test': 'key'}, cache.items())
307
367
cache.add('test2', 'key that is too big')
308
368
self.assertEqual(3, cache._value_size)
309
self.assertEqual({'test':'key'}, cache._cache)
369
self.assertEqual({'test':'key'}, cache.items())
310
370
# If we would add a key, only to cleanup and remove all cached entries,
311
371
# then obviously that value should not be stored
312
372
cache.add('test3', 'bigkey')
313
373
self.assertEqual(3, cache._value_size)
314
self.assertEqual({'test':'key'}, cache._cache)
374
self.assertEqual({'test':'key'}, cache.items())
316
376
cache.add('test4', 'bikey')
317
377
self.assertEqual(3, cache._value_size)
318
self.assertEqual({'test':'key'}, cache._cache)
378
self.assertEqual({'test':'key'}, cache.items())
380
def test_no_add_over_size_cleanup(self):
381
"""If a large value is not cached, we will call cleanup right away."""
383
def cleanup(key, value):
384
cleanup_calls.append((key, value))
386
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
387
self.assertEqual(0, cache._value_size)
388
self.assertEqual({}, cache.items())
389
cache.add('test', 'key that is too big', cleanup=cleanup)
391
self.assertEqual(0, cache._value_size)
392
self.assertEqual({}, cache.items())
393
# and cleanup was called
394
self.assertEqual([('test', 'key that is too big')], cleanup_calls)
320
396
def test_adding_clears_cache_based_on_size(self):
321
397
"""The cache is cleared in LRU order until small enough"""
341
417
cache.add('key4', 'value234') # 8 chars, over limit
342
418
# We have to remove 3 keys to get back under limit
343
419
self.assertEqual(8, cache._value_size)
344
self.assertEqual({'key4':'value234'}, cache._cache)
420
self.assertEqual({'key4':'value234'}, cache.items())
346
422
def test_custom_sizes(self):
347
423
def size_of_list(lst):
357
433
cache.add('key4', ['value', '234']) # 8 chars, over limit
358
434
# We have to remove 3 keys to get back under limit
359
435
self.assertEqual(8, cache._value_size)
360
self.assertEqual({'key4':['value', '234']}, cache._cache)
436
self.assertEqual({'key4':['value', '234']}, cache.items())
362
438
def test_cleanup(self):
363
439
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)