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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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())
38
28
def test_missing(self):
39
29
cache = lru_cache.LRUCache(max_cache=10)
41
self.assertFalse('foo' in cache)
31
self.failIf('foo' in cache)
42
32
self.assertRaises(KeyError, cache.__getitem__, 'foo')
44
34
cache['foo'] = 'bar'
45
35
self.assertEqual('bar', cache['foo'])
46
self.assertTrue('foo' in cache)
47
self.assertFalse('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.assertFalse(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)
36
self.failUnless('foo' in cache)
37
self.failIf('bar' in cache)
70
39
def test_overflow(self):
71
40
"""Adding extra entries will pop out old ones."""
72
cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
41
cache = lru_cache.LRUCache(max_cache=1)
74
43
cache['foo'] = 'bar'
75
44
# With a max cache of 1, adding 'baz' should pop out 'foo'
76
45
cache['baz'] = 'biz'
78
self.assertFalse('foo' in cache)
79
self.assertTrue('baz' in cache)
47
self.failIf('foo' in cache)
48
self.failUnless('baz' in cache)
81
50
self.assertEqual('biz', cache['baz'])
92
61
# This must kick out 'foo' because it was the last accessed
93
62
cache['nub'] = 'in'
95
self.assertFalse('foo' in cache)
64
self.failIf('foo' in cache)
66
def test_queue_stays_bounded(self):
67
"""Lots of accesses does not cause the queue to grow without bound."""
68
cache = lru_cache.LRUCache(max_cache=10)
73
for i in xrange(1000):
76
self.failUnless(len(cache._queue) < 40)
97
78
def test_cleanup(self):
98
79
"""Test that we can use a cleanup function."""
111
92
# 'foo' is now most recent, so final cleanup will call it last
114
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
95
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')], cleanup_called)
117
97
def test_cleanup_on_replace(self):
118
98
"""Replacing an object should cleanup the old value."""
128
108
self.assertEqual([(2, 20)], cleanup_called)
129
109
self.assertEqual(25, cache[2])
131
111
# Even __setitem__ should make sure cleanup() is called
133
113
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()])
172
115
def test_len(self):
173
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
116
cache = lru_cache.LRUCache(max_cache=10)
198
141
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()])
202
def test_cleanup_shrinks_to_after_clean_count(self):
203
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
143
def test_cleanup_shrinks_to_after_clean_size(self):
144
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=3)
215
156
self.assertEqual(3, len(cache))
217
158
def test_after_cleanup_larger_than_max(self):
218
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
219
self.assertEqual(5, cache._after_cleanup_count)
159
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=10)
160
self.assertEqual(5, cache._after_cleanup_size)
221
162
def test_after_cleanup_none(self):
222
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None)
223
# By default _after_cleanup_size is 80% of the normal size
224
self.assertEqual(4, cache._after_cleanup_count)
163
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=None)
164
self.assertEqual(5, cache._after_cleanup_size)
226
166
def test_cleanup(self):
227
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
167
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=2)
229
169
# Add these in order
251
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
191
self.assertEqual([1, 2, 3, 4, 5], list(cache._queue))
253
193
# Now access some randomly
258
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
198
self.assertEqual([1, 2, 3, 4, 5, 2, 5, 3, 2], list(cache._queue))
199
self.assertEqual({1:1, 2:3, 3:2, 4:1, 5:2}, cache._refcount)
201
# Compacting should save the last position
202
cache._compact_queue()
203
self.assertEqual([1, 4, 5, 3, 2], list(cache._queue))
204
self.assertEqual({1:1, 2:1, 3:1, 4:1, 5:1}, cache._refcount)
260
206
def test_get(self):
261
207
cache = lru_cache.LRUCache(max_cache=5)
266
212
self.assertIs(None, cache.get(3))
268
214
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()])
274
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
279
self.assertEqual([1, 2, 3], sorted(cache.keys()))
283
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
285
def test_resize_smaller(self):
286
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
292
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
294
self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
295
# Now resize to something smaller, which triggers a cleanup
296
cache.resize(max_cache=3, after_cleanup_count=2)
297
self.assertEqual([5, 6], sorted(cache.keys()))
298
# Adding something will use the new size
300
self.assertEqual([5, 6, 7], sorted(cache.keys()))
302
self.assertEqual([7, 8], sorted(cache.keys()))
304
def test_resize_larger(self):
305
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
311
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
313
self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
314
cache.resize(max_cache=8, after_cleanup_count=6)
315
self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
320
self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys()))
321
cache[11] = 12 # triggers cleanup back to new after_cleanup_count
322
self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys()))
325
217
class TestLRUSizeCache(tests.TestCase):
327
219
def test_basic_init(self):
328
220
cache = lru_cache.LRUSizeCache()
329
221
self.assertEqual(2048, cache._max_cache)
330
self.assertEqual(int(cache._max_size*0.8), cache._after_cleanup_size)
222
self.assertEqual(4*2048, cache._compact_queue_length)
223
self.assertEqual(cache._max_size, cache._after_cleanup_size)
331
224
self.assertEqual(0, cache._value_size)
333
def test_add__null_key(self):
334
cache = lru_cache.LRUSizeCache()
335
self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
337
226
def test_add_tracks_size(self):
338
227
cache = lru_cache.LRUSizeCache()
339
228
self.assertEqual(0, cache._value_size)
345
234
self.assertEqual(0, cache._value_size)
346
235
cache.add('my key', 'my value text')
347
236
self.assertEqual(13, cache._value_size)
348
node = cache._cache['my key']
349
cache._remove_node(node)
237
cache._remove('my key')
350
238
self.assertEqual(0, cache._value_size)
352
240
def test_no_add_over_size(self):
353
241
"""Adding a large value may not be cached at all."""
354
242
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
355
243
self.assertEqual(0, cache._value_size)
356
self.assertEqual({}, cache.items())
244
self.assertEqual({}, cache._cache)
357
245
cache.add('test', 'key')
358
246
self.assertEqual(3, cache._value_size)
359
self.assertEqual({'test': 'key'}, cache.items())
247
self.assertEqual({'test':'key'}, cache._cache)
360
248
cache.add('test2', 'key that is too big')
361
249
self.assertEqual(3, cache._value_size)
362
self.assertEqual({'test':'key'}, cache.items())
250
self.assertEqual({'test':'key'}, cache._cache)
363
251
# If we would add a key, only to cleanup and remove all cached entries,
364
252
# then obviously that value should not be stored
365
253
cache.add('test3', 'bigkey')
366
254
self.assertEqual(3, cache._value_size)
367
self.assertEqual({'test':'key'}, cache.items())
255
self.assertEqual({'test':'key'}, cache._cache)
369
257
cache.add('test4', 'bikey')
370
258
self.assertEqual(3, cache._value_size)
371
self.assertEqual({'test':'key'}, cache.items())
373
def test_no_add_over_size_cleanup(self):
374
"""If a large value is not cached, we will call cleanup right away."""
376
def cleanup(key, value):
377
cleanup_calls.append((key, value))
379
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
380
self.assertEqual(0, cache._value_size)
381
self.assertEqual({}, cache.items())
382
cache.add('test', 'key that is too big', cleanup=cleanup)
384
self.assertEqual(0, cache._value_size)
385
self.assertEqual({}, cache.items())
386
# and cleanup was called
387
self.assertEqual([('test', 'key that is too big')], cleanup_calls)
259
self.assertEqual({'test':'key'}, cache._cache)
389
261
def test_adding_clears_cache_based_on_size(self):
390
262
"""The cache is cleared in LRU order until small enough"""
410
282
cache.add('key4', 'value234') # 8 chars, over limit
411
283
# We have to remove 3 keys to get back under limit
412
284
self.assertEqual(8, cache._value_size)
413
self.assertEqual({'key4':'value234'}, cache.items())
285
self.assertEqual({'key4':'value234'}, cache._cache)
415
287
def test_custom_sizes(self):
416
288
def size_of_list(lst):
426
298
cache.add('key4', ['value', '234']) # 8 chars, over limit
427
299
# We have to remove 3 keys to get back under limit
428
300
self.assertEqual(8, cache._value_size)
429
self.assertEqual({'key4':['value', '234']}, cache.items())
301
self.assertEqual({'key4':['value', '234']}, cache._cache)
431
303
def test_cleanup(self):
432
304
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
441
313
# Only the most recent fits after cleaning up
442
314
self.assertEqual(7, cache._value_size)
445
cache = lru_cache.LRUSizeCache(max_size=10)
450
self.assertEqual([1, 2, 3], sorted(cache.keys()))
452
def test_resize_smaller(self):
453
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
459
self.assertEqual([2, 3, 4], sorted(cache.keys()))
460
# Resize should also cleanup again
461
cache.resize(max_size=6, after_cleanup_size=4)
462
self.assertEqual([4], sorted(cache.keys()))
463
# Adding should use the new max size
465
self.assertEqual([4, 5], sorted(cache.keys()))
467
self.assertEqual([6], sorted(cache.keys()))
469
def test_resize_larger(self):
470
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
476
self.assertEqual([2, 3, 4], sorted(cache.keys()))
477
cache.resize(max_size=15, after_cleanup_size=12)
478
self.assertEqual([2, 3, 4], sorted(cache.keys()))
481
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
483
self.assertEqual([4, 5, 6, 7], sorted(cache.keys()))