127
129
self.assertFalse('foo' in cache)
129
def test_cleanup(self):
130
"""Test that we can use a cleanup function."""
132
def cleanup_func(key, val):
133
cleanup_called.append((key, val))
135
cache = lru_cache.LRUCache(max_cache=2)
137
cache.add('baz', '1', cleanup=cleanup_func)
138
cache.add('foo', '2', cleanup=cleanup_func)
139
cache.add('biz', '3', cleanup=cleanup_func)
141
self.assertEqual([('baz', '1')], cleanup_called)
143
# 'foo' is now most recent, so final cleanup will call it last
146
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
149
def test_cleanup_on_replace(self):
150
"""Replacing an object should cleanup the old value."""
152
def cleanup_func(key, val):
153
cleanup_called.append((key, val))
155
cache = lru_cache.LRUCache(max_cache=2)
156
cache.add(1, 10, cleanup=cleanup_func)
157
cache.add(2, 20, cleanup=cleanup_func)
158
cache.add(2, 25, cleanup=cleanup_func)
160
self.assertEqual([(2, 20)], cleanup_called)
161
self.assertEqual(25, cache[2])
163
# Even __setitem__ should make sure cleanup() is called
165
self.assertEqual([(2, 20), (2, 25)], cleanup_called)
167
def test_cleanup_error_maintains_linked_list(self):
169
def cleanup_func(key, val):
170
cleanup_called.append((key, val))
171
raise ValueError('failure during cleanup')
173
cache = lru_cache.LRUCache(max_cache=10)
175
cache.add(i, i, cleanup=cleanup_func)
176
for i in xrange(10, 20):
177
self.assertRaises(ValueError,
178
cache.add, i, i, cleanup=cleanup_func)
180
self.assertEqual([(i, i) for i in xrange(10)], cleanup_called)
182
self.assertEqual(range(19, 9, -1), [n.key for n in walk_lru(cache)])
184
def test_cleanup_during_replace_still_replaces(self):
186
def cleanup_func(key, val):
187
cleanup_called.append((key, val))
188
raise ValueError('failure during cleanup')
190
cache = lru_cache.LRUCache(max_cache=10)
192
cache.add(i, i, cleanup=cleanup_func)
193
self.assertRaises(ValueError,
194
cache.add, 1, 20, cleanup=cleanup_func)
195
# We also still update the recent access to this node
196
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
197
[n.key for n in walk_lru(cache)])
198
self.assertEqual(20, cache[1])
200
self.assertEqual([(1, 1)], cleanup_called)
201
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
202
[n.key for n in walk_lru(cache)])
131
def test_cleanup_function_deprecated(self):
132
"""Test that per-node cleanup functions are no longer allowed"""
133
cache = lru_cache.LRUCache()
134
self.assertRaises(ValueError, self.applyDeprecated,
135
symbol_versioning.deprecated_in((2, 5, 0)),
136
cache.add, "key", 1, cleanup=lambda: None)
204
138
def test_len(self):
205
139
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
365
299
def test_add__null_key(self):
366
300
cache = lru_cache.LRUSizeCache()
367
self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
301
self.assertRaises(ValueError,
302
cache.__setitem__, lru_cache._null_key, 1)
369
304
def test_add_tracks_size(self):
370
305
cache = lru_cache.LRUSizeCache()
371
306
self.assertEqual(0, cache._value_size)
372
cache.add('my key', 'my value text')
307
cache['my key'] = 'my value text'
373
308
self.assertEqual(13, cache._value_size)
375
310
def test_remove_tracks_size(self):
376
311
cache = lru_cache.LRUSizeCache()
377
312
self.assertEqual(0, cache._value_size)
378
cache.add('my key', 'my value text')
313
cache['my key'] = 'my value text'
379
314
self.assertEqual(13, cache._value_size)
380
315
node = cache._cache['my key']
381
316
cache._remove_node(node)
386
321
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
387
322
self.assertEqual(0, cache._value_size)
388
323
self.assertEqual({}, cache.as_dict())
389
cache.add('test', 'key')
324
cache['test'] = 'key'
390
325
self.assertEqual(3, cache._value_size)
391
326
self.assertEqual({'test': 'key'}, cache.as_dict())
392
cache.add('test2', 'key that is too big')
327
cache['test2'] = 'key that is too big'
393
328
self.assertEqual(3, cache._value_size)
394
329
self.assertEqual({'test':'key'}, cache.as_dict())
395
330
# If we would add a key, only to cleanup and remove all cached entries,
396
331
# then obviously that value should not be stored
397
cache.add('test3', 'bigkey')
398
self.assertEqual(3, cache._value_size)
399
self.assertEqual({'test':'key'}, cache.as_dict())
401
cache.add('test4', 'bikey')
402
self.assertEqual(3, cache._value_size)
403
self.assertEqual({'test':'key'}, cache.as_dict())
405
def test_no_add_over_size_cleanup(self):
406
"""If a large value is not cached, we will call cleanup right away."""
408
def cleanup(key, value):
409
cleanup_calls.append((key, value))
411
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
412
self.assertEqual(0, cache._value_size)
413
self.assertEqual({}, cache.as_dict())
414
cache.add('test', 'key that is too big', cleanup=cleanup)
416
self.assertEqual(0, cache._value_size)
417
self.assertEqual({}, cache.as_dict())
418
# and cleanup was called
419
self.assertEqual([('test', 'key that is too big')], cleanup_calls)
332
cache['test3'] = 'bigkey'
333
self.assertEqual(3, cache._value_size)
334
self.assertEqual({'test':'key'}, cache.as_dict())
336
cache['test4'] = 'bikey'
337
self.assertEqual(3, cache._value_size)
338
self.assertEqual({'test':'key'}, cache.as_dict())
421
340
def test_adding_clears_cache_based_on_size(self):
422
341
"""The cache is cleared in LRU order until small enough"""
423
342
cache = lru_cache.LRUSizeCache(max_size=20)
424
cache.add('key1', 'value') # 5 chars
425
cache.add('key2', 'value2') # 6 chars
426
cache.add('key3', 'value23') # 7 chars
343
cache['key1'] = 'value' # 5 chars
344
cache['key2'] = 'value2' # 6 chars
345
cache['key3'] = 'value23' # 7 chars
427
346
self.assertEqual(5+6+7, cache._value_size)
428
347
cache['key2'] # reference key2 so it gets a newer reference time
429
cache.add('key4', 'value234') # 8 chars, over limit
348
cache['key4'] = 'value234' # 8 chars, over limit
430
349
# We have to remove 2 keys to get back under limit
431
350
self.assertEqual(6+8, cache._value_size)
432
351
self.assertEqual({'key2':'value2', 'key4':'value234'},
435
354
def test_adding_clears_to_after_cleanup_size(self):
436
355
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
437
cache.add('key1', 'value') # 5 chars
438
cache.add('key2', 'value2') # 6 chars
439
cache.add('key3', 'value23') # 7 chars
356
cache['key1'] = 'value' # 5 chars
357
cache['key2'] = 'value2' # 6 chars
358
cache['key3'] = 'value23' # 7 chars
440
359
self.assertEqual(5+6+7, cache._value_size)
441
360
cache['key2'] # reference key2 so it gets a newer reference time
442
cache.add('key4', 'value234') # 8 chars, over limit
361
cache['key4'] = 'value234' # 8 chars, over limit
443
362
# We have to remove 3 keys to get back under limit
444
363
self.assertEqual(8, cache._value_size)
445
364
self.assertEqual({'key4':'value234'}, cache.as_dict())
450
369
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10,
451
370
compute_size=size_of_list)
453
cache.add('key1', ['val', 'ue']) # 5 chars
454
cache.add('key2', ['val', 'ue2']) # 6 chars
455
cache.add('key3', ['val', 'ue23']) # 7 chars
372
cache['key1'] = ['val', 'ue'] # 5 chars
373
cache['key2'] = ['val', 'ue2'] # 6 chars
374
cache['key3'] = ['val', 'ue23'] # 7 chars
456
375
self.assertEqual(5+6+7, cache._value_size)
457
376
cache['key2'] # reference key2 so it gets a newer reference time
458
cache.add('key4', ['value', '234']) # 8 chars, over limit
377
cache['key4'] = ['value', '234'] # 8 chars, over limit
459
378
# We have to remove 3 keys to get back under limit
460
379
self.assertEqual(8, cache._value_size)
461
380
self.assertEqual({'key4':['value', '234']}, cache.as_dict())