~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lru_cache.py

  • Committer: Mark Hammond
  • Date: 2008-12-28 05:21:23 UTC
  • mfrom: (3920 +trunk)
  • mto: (3932.1.1 prepare-1.11)
  • mto: This revision was merged to the branch mainline in revision 3937.
  • Revision ID: mhammond@skippinet.com.au-20081228052123-f78xs5sbdkotshwf
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
38
38
 
39
39
    def test_overflow(self):
40
40
        """Adding extra entries will pop out old ones."""
41
 
        cache = lru_cache.LRUCache(max_cache=1)
 
41
        cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
42
42
 
43
43
        cache['foo'] = 'bar'
44
44
        # With a max cache of 1, adding 'baz' should pop out 'foo'
113
113
        self.assertEqual([(2, 20), (2, 25)], cleanup_called)
114
114
 
115
115
    def test_len(self):
116
 
        cache = lru_cache.LRUCache(max_cache=10)
 
116
        cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
117
117
 
118
118
        cache[1] = 10
119
119
        cache[2] = 20
140
140
        # We hit the max
141
141
        self.assertEqual(10, len(cache))
142
142
 
143
 
    def test_cleanup_shrinks_to_after_clean_size(self):
144
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=3)
 
143
    def test_cleanup_shrinks_to_after_clean_count(self):
 
144
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
145
145
 
146
146
        cache.add(1, 10)
147
147
        cache.add(2, 20)
156
156
        self.assertEqual(3, len(cache))
157
157
 
158
158
    def test_after_cleanup_larger_than_max(self):
159
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=10)
160
 
        self.assertEqual(5, cache._after_cleanup_size)
 
159
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
 
160
        self.assertEqual(5, cache._after_cleanup_count)
161
161
 
162
162
    def test_after_cleanup_none(self):
163
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=None)
164
 
        self.assertEqual(5, cache._after_cleanup_size)
 
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)
165
166
 
166
167
    def test_cleanup(self):
167
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_size=2)
 
168
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
168
169
 
169
170
        # Add these in order
170
171
        cache.add(1, 10)
214
215
        self.assertIs(obj, cache.get(3, obj))
215
216
 
216
217
    def test_keys(self):
217
 
        cache = lru_cache.LRUCache(max_cache=5)
 
218
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
218
219
 
219
220
        cache[1] = 2
220
221
        cache[2] = 3
225
226
        cache[6] = 7
226
227
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
227
228
 
 
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
 
228
275
 
229
276
class TestLRUSizeCache(tests.TestCase):
230
277
 
232
279
        cache = lru_cache.LRUSizeCache()
233
280
        self.assertEqual(2048, cache._max_cache)
234
281
        self.assertEqual(4*2048, cache._compact_queue_length)
235
 
        self.assertEqual(cache._max_size, cache._after_cleanup_size)
 
282
        self.assertEqual(int(cache._max_size*0.8), cache._after_cleanup_size)
236
283
        self.assertEqual(0, cache._value_size)
237
284
 
238
285
    def test_add_tracks_size(self):
332
379
        cache[2] = 'b'
333
380
        cache[3] = 'cdef'
334
381
        self.assertEqual([1, 2, 3], sorted(cache.keys()))
 
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