19
19
from bzrlib import (
27
"""Test helper to walk the LRU list and assert its consistency"""
28
node = lru._most_recently_used
30
if node.prev is not None:
31
raise AssertionError('the _most_recently_used entry is not'
32
' supposed to have a previous entry'
34
while node is not None:
35
if node.next_key is lru_cache._null_key:
36
if node is not lru._least_recently_used:
37
raise AssertionError('only the last node should have'
38
' no next value: %s' % (node,))
41
node_next = lru._cache[node.next_key]
42
if node_next.prev is not node:
43
raise AssertionError('inconsistency found, node.next.prev'
44
' != node: %s' % (node,))
46
if node is not lru._most_recently_used:
47
raise AssertionError('only the _most_recently_used should'
48
' not have a previous node: %s'
51
if node.prev.next_key != node.key:
52
raise AssertionError('inconsistency found, node.prev.next'
53
' != node: %s' % (node,))
25
58
class TestLRUCache(tests.TestCase):
26
59
"""Test that LRU cache properly keeps track of entries."""
95
129
self.assertFalse('foo' in cache)
97
def test_cleanup(self):
98
"""Test that we can use a cleanup function."""
100
def cleanup_func(key, val):
101
cleanup_called.append((key, val))
103
cache = lru_cache.LRUCache(max_cache=2)
105
cache.add('baz', '1', cleanup=cleanup_func)
106
cache.add('foo', '2', cleanup=cleanup_func)
107
cache.add('biz', '3', cleanup=cleanup_func)
109
self.assertEqual([('baz', '1')], cleanup_called)
111
# 'foo' is now most recent, so final cleanup will call it last
114
self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
117
def test_cleanup_on_replace(self):
118
"""Replacing an object should cleanup the old value."""
120
def cleanup_func(key, val):
121
cleanup_called.append((key, val))
123
cache = lru_cache.LRUCache(max_cache=2)
124
cache.add(1, 10, cleanup=cleanup_func)
125
cache.add(2, 20, cleanup=cleanup_func)
126
cache.add(2, 25, cleanup=cleanup_func)
128
self.assertEqual([(2, 20)], cleanup_called)
129
self.assertEqual(25, cache[2])
131
# Even __setitem__ should make sure cleanup() is called
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()])
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)
172
138
def test_len(self):
173
139
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
198
164
self.assertEqual(10, len(cache))
199
165
self.assertEqual([11, 10, 9, 1, 8, 7, 6, 5, 4, 3],
200
[n.key for n in cache._walk_lru()])
166
[n.key for n in walk_lru(cache)])
202
168
def test_cleanup_shrinks_to_after_clean_count(self):
203
169
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
211
177
self.assertEqual(5, len(cache))
212
178
# This will bump us over the max, which causes us to shrink down to
213
179
# after_cleanup_cache size
215
181
self.assertEqual(3, len(cache))
217
183
def test_after_cleanup_larger_than_max(self):
242
208
cache = lru_cache.LRUCache(max_cache=5)
244
210
# Add these in order
251
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
217
self.assertEqual([5, 4, 3, 2, 1], [n.key for n in walk_lru(cache)])
253
219
# Now access some randomly
258
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
224
self.assertEqual([2, 3, 5, 4, 1], [n.key for n in walk_lru(cache)])
260
226
def test_get(self):
261
227
cache = lru_cache.LRUCache(max_cache=5)
265
231
self.assertEqual(20, cache.get(2))
266
232
self.assertIs(None, cache.get(3))
268
234
self.assertIs(obj, cache.get(3, obj))
269
self.assertEqual([2, 1], [n.key for n in cache._walk_lru()])
235
self.assertEqual([2, 1], [n.key for n in walk_lru(cache)])
270
236
self.assertEqual(10, cache.get(1))
271
self.assertEqual([1, 2], [n.key for n in cache._walk_lru()])
237
self.assertEqual([1, 2], [n.key for n in walk_lru(cache)])
273
239
def test_keys(self):
274
240
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
333
299
def test_add__null_key(self):
334
300
cache = lru_cache.LRUSizeCache()
335
self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
301
self.assertRaises(ValueError,
302
cache.__setitem__, lru_cache._null_key, 1)
337
304
def test_add_tracks_size(self):
338
305
cache = lru_cache.LRUSizeCache()
339
306
self.assertEqual(0, cache._value_size)
340
cache.add('my key', 'my value text')
307
cache['my key'] = 'my value text'
341
308
self.assertEqual(13, cache._value_size)
343
310
def test_remove_tracks_size(self):
344
311
cache = lru_cache.LRUSizeCache()
345
312
self.assertEqual(0, cache._value_size)
346
cache.add('my key', 'my value text')
313
cache['my key'] = 'my value text'
347
314
self.assertEqual(13, cache._value_size)
348
315
node = cache._cache['my key']
349
316
cache._remove_node(node)
353
320
"""Adding a large value may not be cached at all."""
354
321
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
355
322
self.assertEqual(0, cache._value_size)
356
self.assertEqual({}, cache.items())
357
cache.add('test', 'key')
358
self.assertEqual(3, cache._value_size)
359
self.assertEqual({'test': 'key'}, cache.items())
360
cache.add('test2', 'key that is too big')
361
self.assertEqual(3, cache._value_size)
362
self.assertEqual({'test':'key'}, cache.items())
323
self.assertEqual({}, cache.as_dict())
324
cache['test'] = 'key'
325
self.assertEqual(3, cache._value_size)
326
self.assertEqual({'test': 'key'}, cache.as_dict())
327
cache['test2'] = 'key that is too big'
328
self.assertEqual(3, cache._value_size)
329
self.assertEqual({'test':'key'}, cache.as_dict())
363
330
# If we would add a key, only to cleanup and remove all cached entries,
364
331
# then obviously that value should not be stored
365
cache.add('test3', 'bigkey')
366
self.assertEqual(3, cache._value_size)
367
self.assertEqual({'test':'key'}, cache.items())
369
cache.add('test4', 'bikey')
370
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)
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())
389
340
def test_adding_clears_cache_based_on_size(self):
390
341
"""The cache is cleared in LRU order until small enough"""
391
342
cache = lru_cache.LRUSizeCache(max_size=20)
392
cache.add('key1', 'value') # 5 chars
393
cache.add('key2', 'value2') # 6 chars
394
cache.add('key3', 'value23') # 7 chars
343
cache['key1'] = 'value' # 5 chars
344
cache['key2'] = 'value2' # 6 chars
345
cache['key3'] = 'value23' # 7 chars
395
346
self.assertEqual(5+6+7, cache._value_size)
396
347
cache['key2'] # reference key2 so it gets a newer reference time
397
cache.add('key4', 'value234') # 8 chars, over limit
348
cache['key4'] = 'value234' # 8 chars, over limit
398
349
# We have to remove 2 keys to get back under limit
399
350
self.assertEqual(6+8, cache._value_size)
400
351
self.assertEqual({'key2':'value2', 'key4':'value234'},
403
354
def test_adding_clears_to_after_cleanup_size(self):
404
355
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
405
cache.add('key1', 'value') # 5 chars
406
cache.add('key2', 'value2') # 6 chars
407
cache.add('key3', 'value23') # 7 chars
356
cache['key1'] = 'value' # 5 chars
357
cache['key2'] = 'value2' # 6 chars
358
cache['key3'] = 'value23' # 7 chars
408
359
self.assertEqual(5+6+7, cache._value_size)
409
360
cache['key2'] # reference key2 so it gets a newer reference time
410
cache.add('key4', 'value234') # 8 chars, over limit
361
cache['key4'] = 'value234' # 8 chars, over limit
411
362
# We have to remove 3 keys to get back under limit
412
363
self.assertEqual(8, cache._value_size)
413
self.assertEqual({'key4':'value234'}, cache.items())
364
self.assertEqual({'key4':'value234'}, cache.as_dict())
415
366
def test_custom_sizes(self):
416
367
def size_of_list(lst):
418
369
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10,
419
370
compute_size=size_of_list)
421
cache.add('key1', ['val', 'ue']) # 5 chars
422
cache.add('key2', ['val', 'ue2']) # 6 chars
423
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
424
375
self.assertEqual(5+6+7, cache._value_size)
425
376
cache['key2'] # reference key2 so it gets a newer reference time
426
cache.add('key4', ['value', '234']) # 8 chars, over limit
377
cache['key4'] = ['value', '234'] # 8 chars, over limit
427
378
# We have to remove 3 keys to get back under limit
428
379
self.assertEqual(8, cache._value_size)
429
self.assertEqual({'key4':['value', '234']}, cache.items())
380
self.assertEqual({'key4':['value', '234']}, cache.as_dict())
431
382
def test_cleanup(self):
432
383
cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
434
385
# Add these in order
435
cache.add('key1', 'value') # 5 chars
436
cache.add('key2', 'value2') # 6 chars
437
cache.add('key3', 'value23') # 7 chars
386
cache['key1'] = 'value' # 5 chars
387
cache['key2'] = 'value2' # 6 chars
388
cache['key3'] = 'value23' # 7 chars
438
389
self.assertEqual(5+6+7, cache._value_size)