/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
1
# Copyright (C) 2006, 2008 Canonical Ltd
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
16
17
"""Tests for the lru_cache module."""
18
19
from bzrlib import (
20
    lru_cache,
21
    tests,
22
    )
23
24
25
class TestLRUCache(tests.TestCase):
26
    """Test that LRU cache properly keeps track of entries."""
27
4178.3.2 by John Arbash Meinel
Add tests for LRUCache.cache_size()
28
    def test_cache_size(self):
29
        cache = lru_cache.LRUCache(max_cache=10)
30
        self.assertEqual(10, cache.cache_size())
31
32
        cache = lru_cache.LRUCache(max_cache=256)
33
        self.assertEqual(256, cache.cache_size())
34
35
        cache.resize(512)
36
        self.assertEqual(512, cache.cache_size())
37
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
38
    def test_missing(self):
39
        cache = lru_cache.LRUCache(max_cache=10)
40
41
        self.failIf('foo' in cache)
42
        self.assertRaises(KeyError, cache.__getitem__, 'foo')
43
44
        cache['foo'] = 'bar'
45
        self.assertEqual('bar', cache['foo'])
46
        self.failUnless('foo' in cache)
47
        self.failIf('bar' in cache)
48
49
    def test_overflow(self):
50
        """Adding extra entries will pop out old ones."""
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
51
        cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
52
53
        cache['foo'] = 'bar'
54
        # With a max cache of 1, adding 'baz' should pop out 'foo'
55
        cache['baz'] = 'biz'
56
57
        self.failIf('foo' in cache)
58
        self.failUnless('baz' in cache)
59
60
        self.assertEqual('biz', cache['baz'])
61
62
    def test_by_usage(self):
63
        """Accessing entries bumps them up in priority."""
64
        cache = lru_cache.LRUCache(max_cache=2)
65
66
        cache['baz'] = 'biz'
67
        cache['foo'] = 'bar'
68
69
        self.assertEqual('biz', cache['baz'])
70
71
        # This must kick out 'foo' because it was the last accessed
72
        cache['nub'] = 'in'
73
74
        self.failIf('foo' in cache)
75
76
    def test_cleanup(self):
77
        """Test that we can use a cleanup function."""
78
        cleanup_called = []
79
        def cleanup_func(key, val):
80
            cleanup_called.append((key, val))
81
82
        cache = lru_cache.LRUCache(max_cache=2)
83
84
        cache.add('baz', '1', cleanup=cleanup_func)
85
        cache.add('foo', '2', cleanup=cleanup_func)
86
        cache.add('biz', '3', cleanup=cleanup_func)
87
88
        self.assertEqual([('baz', '1')], cleanup_called)
89
90
        # 'foo' is now most recent, so final cleanup will call it last
91
        cache['foo']
92
        cache.clear()
4178.3.7 by John Arbash Meinel
Review tweaks from Ian.
93
        self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')],
94
                         cleanup_called)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
95
96
    def test_cleanup_on_replace(self):
97
        """Replacing an object should cleanup the old value."""
98
        cleanup_called = []
99
        def cleanup_func(key, val):
100
            cleanup_called.append((key, val))
101
102
        cache = lru_cache.LRUCache(max_cache=2)
103
        cache.add(1, 10, cleanup=cleanup_func)
104
        cache.add(2, 20, cleanup=cleanup_func)
105
        cache.add(2, 25, cleanup=cleanup_func)
106
107
        self.assertEqual([(2, 20)], cleanup_called)
108
        self.assertEqual(25, cache[2])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
109
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
110
        # Even __setitem__ should make sure cleanup() is called
111
        cache[2] = 26
112
        self.assertEqual([(2, 20), (2, 25)], cleanup_called)
113
114
    def test_len(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
115
        cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
116
117
        cache[1] = 10
118
        cache[2] = 20
119
        cache[3] = 30
120
        cache[4] = 40
121
122
        self.assertEqual(4, len(cache))
123
124
        cache[5] = 50
125
        cache[6] = 60
126
        cache[7] = 70
127
        cache[8] = 80
128
129
        self.assertEqual(8, len(cache))
130
131
        cache[1] = 15 # replacement
132
133
        self.assertEqual(8, len(cache))
134
135
        cache[9] = 90
136
        cache[10] = 100
137
        cache[11] = 110
138
139
        # We hit the max
140
        self.assertEqual(10, len(cache))
141
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
142
    def test_cleanup_shrinks_to_after_clean_count(self):
143
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
144
145
        cache.add(1, 10)
146
        cache.add(2, 20)
147
        cache.add(3, 25)
148
        cache.add(4, 30)
149
        cache.add(5, 35)
150
151
        self.assertEqual(5, len(cache))
152
        # This will bump us over the max, which causes us to shrink down to
153
        # after_cleanup_cache size
154
        cache.add(6, 40)
155
        self.assertEqual(3, len(cache))
156
157
    def test_after_cleanup_larger_than_max(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
158
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
159
        self.assertEqual(5, cache._after_cleanup_count)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
160
161
    def test_after_cleanup_none(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
162
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None)
163
        # By default _after_cleanup_size is 80% of the normal size
164
        self.assertEqual(4, cache._after_cleanup_count)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
165
166
    def test_cleanup(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
167
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
168
169
        # Add these in order
170
        cache.add(1, 10)
171
        cache.add(2, 20)
172
        cache.add(3, 25)
173
        cache.add(4, 30)
174
        cache.add(5, 35)
175
176
        self.assertEqual(5, len(cache))
177
        # Force a compaction
178
        cache.cleanup()
179
        self.assertEqual(2, len(cache))
180
4178.3.7 by John Arbash Meinel
Review tweaks from Ian.
181
    def test_preserve_last_access_order(self):
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
182
        cache = lru_cache.LRUCache(max_cache=5)
183
184
        # Add these in order
185
        cache.add(1, 10)
186
        cache.add(2, 20)
187
        cache.add(3, 25)
188
        cache.add(4, 30)
189
        cache.add(5, 35)
190
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
191
        self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
192
193
        # Now access some randomly
194
        cache[2]
195
        cache[5]
196
        cache[3]
197
        cache[2]
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
198
        self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
199
2998.2.1 by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get()
200
    def test_get(self):
201
        cache = lru_cache.LRUCache(max_cache=5)
202
203
        cache.add(1, 10)
204
        cache.add(2, 20)
205
        self.assertEqual(20, cache.get(2))
206
        self.assertIs(None, cache.get(3))
207
        obj = object()
208
        self.assertIs(obj, cache.get(3, obj))
4178.3.5 by John Arbash Meinel
Add tests that LRUCache.get() properly tracks accesses.
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()])
2998.2.1 by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get()
212
3763.8.10 by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache.
213
    def test_keys(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
214
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
3763.8.10 by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache.
215
216
        cache[1] = 2
217
        cache[2] = 3
218
        cache[3] = 4
219
        self.assertEqual([1, 2, 3], sorted(cache.keys()))
220
        cache[4] = 5
221
        cache[5] = 6
222
        cache[6] = 7
223
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
224
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
225
    def test_after_cleanup_size_deprecated(self):
226
        obj = self.callDeprecated([
227
            'LRUCache.__init__(after_cleanup_size) was deprecated in 1.11.'
228
            ' Use after_cleanup_count instead.'],
229
            lru_cache.LRUCache, 50, after_cleanup_size=25)
230
        self.assertEqual(obj._after_cleanup_count, 25)
231
232
    def test_resize_smaller(self):
233
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
234
        cache[1] = 2
235
        cache[2] = 3
236
        cache[3] = 4
237
        cache[4] = 5
238
        cache[5] = 6
239
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
240
        cache[6] = 7
241
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
242
        # Now resize to something smaller, which triggers a cleanup
243
        cache.resize(max_cache=3, after_cleanup_count=2)
244
        self.assertEqual([5, 6], sorted(cache.keys()))
245
        # Adding something will use the new size
246
        cache[7] = 8
247
        self.assertEqual([5, 6, 7], sorted(cache.keys()))
248
        cache[8] = 9
249
        self.assertEqual([7, 8], sorted(cache.keys()))
250
251
    def test_resize_larger(self):
252
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
253
        cache[1] = 2
254
        cache[2] = 3
255
        cache[3] = 4
256
        cache[4] = 5
257
        cache[5] = 6
258
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
259
        cache[6] = 7
260
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
261
        cache.resize(max_cache=8, after_cleanup_count=6)
262
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
263
        cache[7] = 8
264
        cache[8] = 9
265
        cache[9] = 10
266
        cache[10] = 11
267
        self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys()))
268
        cache[11] = 12 # triggers cleanup back to new after_cleanup_count
269
        self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys()))
270
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
271
272
class TestLRUSizeCache(tests.TestCase):
273
274
    def test_basic_init(self):
275
        cache = lru_cache.LRUSizeCache()
276
        self.assertEqual(2048, cache._max_cache)
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
277
        self.assertEqual(int(cache._max_size*0.8), cache._after_cleanup_size)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
278
        self.assertEqual(0, cache._value_size)
279
280
    def test_add_tracks_size(self):
281
        cache = lru_cache.LRUSizeCache()
282
        self.assertEqual(0, cache._value_size)
283
        cache.add('my key', 'my value text')
284
        self.assertEqual(13, cache._value_size)
285
286
    def test_remove_tracks_size(self):
287
        cache = lru_cache.LRUSizeCache()
288
        self.assertEqual(0, cache._value_size)
289
        cache.add('my key', 'my value text')
290
        self.assertEqual(13, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
291
        node = cache._cache['my key']
292
        cache._remove_node(node)
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
293
        self.assertEqual(0, cache._value_size)
294
295
    def test_no_add_over_size(self):
296
        """Adding a large value may not be cached at all."""
297
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
298
        self.assertEqual(0, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
299
        self.assertEqual({}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
300
        cache.add('test', 'key')
301
        self.assertEqual(3, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
302
        self.assertEqual({'test': 'key'}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
303
        cache.add('test2', 'key that is too big')
304
        self.assertEqual(3, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
305
        self.assertEqual({'test':'key'}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
306
        # If we would add a key, only to cleanup and remove all cached entries,
307
        # then obviously that value should not be stored
308
        cache.add('test3', 'bigkey')
309
        self.assertEqual(3, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
310
        self.assertEqual({'test':'key'}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
311
312
        cache.add('test4', 'bikey')
313
        self.assertEqual(3, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
314
        self.assertEqual({'test':'key'}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
315
4178.3.7 by John Arbash Meinel
Review tweaks from Ian.
316
    def test_no_add_over_size_cleanup(self):
317
        """If a large value is not cached, we will call cleanup right away."""
318
        cleanup_calls = []
319
        def cleanup(key, value):
320
            cleanup_calls.append((key, value))
321
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)
326
        # key was not added
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)
331
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
332
    def test_adding_clears_cache_based_on_size(self):
333
        """The cache is cleared in LRU order until small enough"""
334
        cache = lru_cache.LRUSizeCache(max_size=20)
335
        cache.add('key1', 'value') # 5 chars
336
        cache.add('key2', 'value2') # 6 chars
337
        cache.add('key3', 'value23') # 7 chars
338
        self.assertEqual(5+6+7, cache._value_size)
339
        cache['key2'] # reference key2 so it gets a newer reference time
340
        cache.add('key4', 'value234') # 8 chars, over limit
341
        # We have to remove 2 keys to get back under limit
342
        self.assertEqual(6+8, cache._value_size)
343
        self.assertEqual({'key2':'value2', 'key4':'value234'},
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
344
                         cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
345
346
    def test_adding_clears_to_after_cleanup_size(self):
347
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
348
        cache.add('key1', 'value') # 5 chars
349
        cache.add('key2', 'value2') # 6 chars
350
        cache.add('key3', 'value23') # 7 chars
351
        self.assertEqual(5+6+7, cache._value_size)
352
        cache['key2'] # reference key2 so it gets a newer reference time
353
        cache.add('key4', 'value234') # 8 chars, over limit
354
        # We have to remove 3 keys to get back under limit
355
        self.assertEqual(8, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
356
        self.assertEqual({'key4':'value234'}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
357
358
    def test_custom_sizes(self):
359
        def size_of_list(lst):
360
            return sum(len(x) for x in lst)
361
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10,
362
                                       compute_size=size_of_list)
363
364
        cache.add('key1', ['val', 'ue']) # 5 chars
365
        cache.add('key2', ['val', 'ue2']) # 6 chars
366
        cache.add('key3', ['val', 'ue23']) # 7 chars
367
        self.assertEqual(5+6+7, cache._value_size)
368
        cache['key2'] # reference key2 so it gets a newer reference time
369
        cache.add('key4', ['value', '234']) # 8 chars, over limit
370
        # We have to remove 3 keys to get back under limit
371
        self.assertEqual(8, cache._value_size)
4178.3.3 by John Arbash Meinel
LRUCache is now implemented with a dict to a linked list,
372
        self.assertEqual({'key4':['value', '234']}, cache.items())
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
373
374
    def test_cleanup(self):
375
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
376
377
        # Add these in order
378
        cache.add('key1', 'value') # 5 chars
379
        cache.add('key2', 'value2') # 6 chars
380
        cache.add('key3', 'value23') # 7 chars
381
        self.assertEqual(5+6+7, cache._value_size)
382
383
        cache.cleanup()
384
        # Only the most recent fits after cleaning up
385
        self.assertEqual(7, cache._value_size)
3763.8.10 by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache.
386
387
    def test_keys(self):
388
        cache = lru_cache.LRUSizeCache(max_size=10)
389
390
        cache[1] = 'a'
391
        cache[2] = 'b'
392
        cache[3] = 'cdef'
393
        self.assertEqual([1, 2, 3], sorted(cache.keys()))
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
394
395
    def test_resize_smaller(self):
396
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
397
        cache[1] = 'abc'
398
        cache[2] = 'def'
399
        cache[3] = 'ghi'
400
        cache[4] = 'jkl'
401
        # Triggers a cleanup
402
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
403
        # Resize should also cleanup again
404
        cache.resize(max_size=6, after_cleanup_size=4)
405
        self.assertEqual([4], sorted(cache.keys()))
406
        # Adding should use the new max size
407
        cache[5] = 'mno'
408
        self.assertEqual([4, 5], sorted(cache.keys()))
409
        cache[6] = 'pqr'
410
        self.assertEqual([6], sorted(cache.keys()))
411
412
    def test_resize_larger(self):
413
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
414
        cache[1] = 'abc'
415
        cache[2] = 'def'
416
        cache[3] = 'ghi'
417
        cache[4] = 'jkl'
418
        # Triggers a cleanup
419
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
420
        cache.resize(max_size=15, after_cleanup_size=12)
421
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
422
        cache[5] = 'mno'
423
        cache[6] = 'pqr'
424
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
425
        cache[7] = 'stu'
426
        self.assertEqual([4, 5, 6, 7], sorted(cache.keys()))
427