/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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
28
    def test_missing(self):
29
        cache = lru_cache.LRUCache(max_cache=10)
30
31
        self.failIf('foo' in cache)
32
        self.assertRaises(KeyError, cache.__getitem__, 'foo')
33
34
        cache['foo'] = 'bar'
35
        self.assertEqual('bar', cache['foo'])
36
        self.failUnless('foo' in cache)
37
        self.failIf('bar' in cache)
38
39
    def test_overflow(self):
40
        """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.
41
        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
42
43
        cache['foo'] = 'bar'
44
        # With a max cache of 1, adding 'baz' should pop out 'foo'
45
        cache['baz'] = 'biz'
46
47
        self.failIf('foo' in cache)
48
        self.failUnless('baz' in cache)
49
50
        self.assertEqual('biz', cache['baz'])
51
52
    def test_by_usage(self):
53
        """Accessing entries bumps them up in priority."""
54
        cache = lru_cache.LRUCache(max_cache=2)
55
56
        cache['baz'] = 'biz'
57
        cache['foo'] = 'bar'
58
59
        self.assertEqual('biz', cache['baz'])
60
61
        # This must kick out 'foo' because it was the last accessed
62
        cache['nub'] = 'in'
63
64
        self.failIf('foo' in cache)
65
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)
69
70
        cache['baz'] = 'biz'
71
        cache['foo'] = 'bar'
72
73
        for i in xrange(1000):
74
            cache['baz']
75
76
        self.failUnless(len(cache._queue) < 40)
77
78
    def test_cleanup(self):
79
        """Test that we can use a cleanup function."""
80
        cleanup_called = []
81
        def cleanup_func(key, val):
82
            cleanup_called.append((key, val))
83
84
        cache = lru_cache.LRUCache(max_cache=2)
85
86
        cache.add('baz', '1', cleanup=cleanup_func)
87
        cache.add('foo', '2', cleanup=cleanup_func)
88
        cache.add('biz', '3', cleanup=cleanup_func)
89
90
        self.assertEqual([('baz', '1')], cleanup_called)
91
92
        # 'foo' is now most recent, so final cleanup will call it last
93
        cache['foo']
94
        cache.clear()
95
        self.assertEqual([('baz', '1'), ('biz', '3'), ('foo', '2')], cleanup_called)
96
97
    def test_cleanup_on_replace(self):
98
        """Replacing an object should cleanup the old value."""
99
        cleanup_called = []
100
        def cleanup_func(key, val):
101
            cleanup_called.append((key, val))
102
103
        cache = lru_cache.LRUCache(max_cache=2)
104
        cache.add(1, 10, cleanup=cleanup_func)
105
        cache.add(2, 20, cleanup=cleanup_func)
106
        cache.add(2, 25, cleanup=cleanup_func)
107
108
        self.assertEqual([(2, 20)], cleanup_called)
109
        self.assertEqual(25, cache[2])
110
        
111
        # Even __setitem__ should make sure cleanup() is called
112
        cache[2] = 26
113
        self.assertEqual([(2, 20), (2, 25)], cleanup_called)
114
115
    def test_len(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
116
        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
117
118
        cache[1] = 10
119
        cache[2] = 20
120
        cache[3] = 30
121
        cache[4] = 40
122
123
        self.assertEqual(4, len(cache))
124
125
        cache[5] = 50
126
        cache[6] = 60
127
        cache[7] = 70
128
        cache[8] = 80
129
130
        self.assertEqual(8, len(cache))
131
132
        cache[1] = 15 # replacement
133
134
        self.assertEqual(8, len(cache))
135
136
        cache[9] = 90
137
        cache[10] = 100
138
        cache[11] = 110
139
140
        # We hit the max
141
        self.assertEqual(10, len(cache))
142
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
143
    def test_cleanup_shrinks_to_after_clean_count(self):
144
        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
145
146
        cache.add(1, 10)
147
        cache.add(2, 20)
148
        cache.add(3, 25)
149
        cache.add(4, 30)
150
        cache.add(5, 35)
151
152
        self.assertEqual(5, len(cache))
153
        # This will bump us over the max, which causes us to shrink down to
154
        # after_cleanup_cache size
155
        cache.add(6, 40)
156
        self.assertEqual(3, len(cache))
157
158
    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.
159
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
160
        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
161
162
    def test_after_cleanup_none(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
163
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None)
164
        # By default _after_cleanup_size is 80% of the normal size
165
        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
166
167
    def test_cleanup(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
168
        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
169
170
        # Add these in order
171
        cache.add(1, 10)
172
        cache.add(2, 20)
173
        cache.add(3, 25)
174
        cache.add(4, 30)
175
        cache.add(5, 35)
176
177
        self.assertEqual(5, len(cache))
178
        # Force a compaction
179
        cache.cleanup()
180
        self.assertEqual(2, len(cache))
181
182
    def test_compact_preserves_last_access_order(self):
183
        cache = lru_cache.LRUCache(max_cache=5)
184
185
        # Add these in order
186
        cache.add(1, 10)
187
        cache.add(2, 20)
188
        cache.add(3, 25)
189
        cache.add(4, 30)
190
        cache.add(5, 35)
191
192
        self.assertEqual([1, 2, 3, 4, 5], list(cache._queue))
193
194
        # Now access some randomly
195
        cache[2]
196
        cache[5]
197
        cache[3]
198
        cache[2]
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)
201
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)
206
2998.2.1 by John Arbash Meinel
Implement LRUCache.get() which acts like dict.get()
207
    def test_get(self):
208
        cache = lru_cache.LRUCache(max_cache=5)
209
210
        cache.add(1, 10)
211
        cache.add(2, 20)
212
        self.assertEqual(20, cache.get(2))
213
        self.assertIs(None, cache.get(3))
214
        obj = object()
215
        self.assertIs(obj, cache.get(3, obj))
216
3763.8.10 by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache.
217
    def test_keys(self):
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
218
        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.
219
220
        cache[1] = 2
221
        cache[2] = 3
222
        cache[3] = 4
223
        self.assertEqual([1, 2, 3], sorted(cache.keys()))
224
        cache[4] = 5
225
        cache[5] = 6
226
        cache[6] = 7
227
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
228
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
229
    def test_after_cleanup_size_deprecated(self):
230
        obj = self.callDeprecated([
231
            'LRUCache.__init__(after_cleanup_size) was deprecated in 1.11.'
232
            ' Use after_cleanup_count instead.'],
233
            lru_cache.LRUCache, 50, after_cleanup_size=25)
234
        self.assertEqual(obj._after_cleanup_count, 25)
235
236
    def test_resize_smaller(self):
237
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
238
        cache[1] = 2
239
        cache[2] = 3
240
        cache[3] = 4
241
        cache[4] = 5
242
        cache[5] = 6
243
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
244
        cache[6] = 7
245
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
246
        # Now resize to something smaller, which triggers a cleanup
247
        cache.resize(max_cache=3, after_cleanup_count=2)
248
        self.assertEqual([5, 6], sorted(cache.keys()))
249
        # Adding something will use the new size
250
        cache[7] = 8
251
        self.assertEqual([5, 6, 7], sorted(cache.keys()))
252
        cache[8] = 9
253
        self.assertEqual([7, 8], sorted(cache.keys()))
254
255
    def test_resize_larger(self):
256
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
257
        cache[1] = 2
258
        cache[2] = 3
259
        cache[3] = 4
260
        cache[4] = 5
261
        cache[5] = 6
262
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
263
        cache[6] = 7
264
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
265
        cache.resize(max_cache=8, after_cleanup_count=6)
266
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
267
        cache[7] = 8
268
        cache[8] = 9
269
        cache[9] = 10
270
        cache[10] = 11
271
        self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys()))
272
        cache[11] = 12 # triggers cleanup back to new after_cleanup_count
273
        self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys()))
274
2993.1.1 by Robert Collins
* New module ``lru_cache`` providing a cache for use by tasks that need
275
276
class TestLRUSizeCache(tests.TestCase):
277
278
    def test_basic_init(self):
279
        cache = lru_cache.LRUSizeCache()
280
        self.assertEqual(2048, cache._max_cache)
281
        self.assertEqual(4*2048, cache._compact_queue_length)
3882.3.1 by John Arbash Meinel
Add LRUCache.resize(), and change the init arguments for LRUCache.
282
        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
283
        self.assertEqual(0, cache._value_size)
284
285
    def test_add_tracks_size(self):
286
        cache = lru_cache.LRUSizeCache()
287
        self.assertEqual(0, cache._value_size)
288
        cache.add('my key', 'my value text')
289
        self.assertEqual(13, cache._value_size)
290
291
    def test_remove_tracks_size(self):
292
        cache = lru_cache.LRUSizeCache()
293
        self.assertEqual(0, cache._value_size)
294
        cache.add('my key', 'my value text')
295
        self.assertEqual(13, cache._value_size)
296
        cache._remove('my key')
297
        self.assertEqual(0, cache._value_size)
298
299
    def test_no_add_over_size(self):
300
        """Adding a large value may not be cached at all."""
301
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
302
        self.assertEqual(0, cache._value_size)
303
        self.assertEqual({}, cache._cache)
304
        cache.add('test', 'key')
305
        self.assertEqual(3, cache._value_size)
306
        self.assertEqual({'test':'key'}, cache._cache)
307
        cache.add('test2', 'key that is too big')
308
        self.assertEqual(3, cache._value_size)
309
        self.assertEqual({'test':'key'}, cache._cache)
310
        # If we would add a key, only to cleanup and remove all cached entries,
311
        # then obviously that value should not be stored
312
        cache.add('test3', 'bigkey')
313
        self.assertEqual(3, cache._value_size)
314
        self.assertEqual({'test':'key'}, cache._cache)
315
316
        cache.add('test4', 'bikey')
317
        self.assertEqual(3, cache._value_size)
318
        self.assertEqual({'test':'key'}, cache._cache)
319
320
    def test_adding_clears_cache_based_on_size(self):
321
        """The cache is cleared in LRU order until small enough"""
322
        cache = lru_cache.LRUSizeCache(max_size=20)
323
        cache.add('key1', 'value') # 5 chars
324
        cache.add('key2', 'value2') # 6 chars
325
        cache.add('key3', 'value23') # 7 chars
326
        self.assertEqual(5+6+7, cache._value_size)
327
        cache['key2'] # reference key2 so it gets a newer reference time
328
        cache.add('key4', 'value234') # 8 chars, over limit
329
        # We have to remove 2 keys to get back under limit
330
        self.assertEqual(6+8, cache._value_size)
331
        self.assertEqual({'key2':'value2', 'key4':'value234'},
332
                         cache._cache)
333
334
    def test_adding_clears_to_after_cleanup_size(self):
335
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
336
        cache.add('key1', 'value') # 5 chars
337
        cache.add('key2', 'value2') # 6 chars
338
        cache.add('key3', 'value23') # 7 chars
339
        self.assertEqual(5+6+7, cache._value_size)
340
        cache['key2'] # reference key2 so it gets a newer reference time
341
        cache.add('key4', 'value234') # 8 chars, over limit
342
        # We have to remove 3 keys to get back under limit
343
        self.assertEqual(8, cache._value_size)
344
        self.assertEqual({'key4':'value234'}, cache._cache)
345
346
    def test_custom_sizes(self):
347
        def size_of_list(lst):
348
            return sum(len(x) for x in lst)
349
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10,
350
                                       compute_size=size_of_list)
351
352
        cache.add('key1', ['val', 'ue']) # 5 chars
353
        cache.add('key2', ['val', 'ue2']) # 6 chars
354
        cache.add('key3', ['val', 'ue23']) # 7 chars
355
        self.assertEqual(5+6+7, cache._value_size)
356
        cache['key2'] # reference key2 so it gets a newer reference time
357
        cache.add('key4', ['value', '234']) # 8 chars, over limit
358
        # We have to remove 3 keys to get back under limit
359
        self.assertEqual(8, cache._value_size)
360
        self.assertEqual({'key4':['value', '234']}, cache._cache)
361
362
    def test_cleanup(self):
363
        cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
364
365
        # Add these in order
366
        cache.add('key1', 'value') # 5 chars
367
        cache.add('key2', 'value2') # 6 chars
368
        cache.add('key3', 'value23') # 7 chars
369
        self.assertEqual(5+6+7, cache._value_size)
370
371
        cache.cleanup()
372
        # Only the most recent fits after cleaning up
373
        self.assertEqual(7, cache._value_size)
3763.8.10 by John Arbash Meinel
Add a .keys() member to LRUCache and LRUSizeCache.
374
375
    def test_keys(self):
376
        cache = lru_cache.LRUSizeCache(max_size=10)
377
378
        cache[1] = 'a'
379
        cache[2] = 'b'
380
        cache[3] = 'cdef'
381
        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.
382
383
    def test_resize_smaller(self):
384
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
385
        cache[1] = 'abc'
386
        cache[2] = 'def'
387
        cache[3] = 'ghi'
388
        cache[4] = 'jkl'
389
        # Triggers a cleanup
390
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
391
        # Resize should also cleanup again
392
        cache.resize(max_size=6, after_cleanup_size=4)
393
        self.assertEqual([4], sorted(cache.keys()))
394
        # Adding should use the new max size
395
        cache[5] = 'mno'
396
        self.assertEqual([4, 5], sorted(cache.keys()))
397
        cache[6] = 'pqr'
398
        self.assertEqual([6], sorted(cache.keys()))
399
400
    def test_resize_larger(self):
401
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
402
        cache[1] = 'abc'
403
        cache[2] = 'def'
404
        cache[3] = 'ghi'
405
        cache[4] = 'jkl'
406
        # Triggers a cleanup
407
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
408
        cache.resize(max_size=15, after_cleanup_size=12)
409
        self.assertEqual([2, 3, 4], sorted(cache.keys()))
410
        cache[5] = 'mno'
411
        cache[6] = 'pqr'
412
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
413
        cache[7] = 'stu'
414
        self.assertEqual([4, 5, 6, 7], sorted(cache.keys()))
415