/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_lru_cache.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009 Canonical Ltd
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for the lru_cache module."""
18
 
 
19
 
from .. import (
20
 
    lru_cache,
21
 
    tests,
22
 
    )
23
 
 
24
 
 
25
 
def walk_lru(lru):
26
 
    """Test helper to walk the LRU list and assert its consistency"""
27
 
    node = lru._most_recently_used
28
 
    if node is not None:
29
 
        if node.prev is not None:
30
 
            raise AssertionError('the _most_recently_used entry is not'
31
 
                                 ' supposed to have a previous entry'
32
 
                                 ' %s' % (node,))
33
 
    while node is not None:
34
 
        if node.next_key is lru_cache._null_key:
35
 
            if node is not lru._least_recently_used:
36
 
                raise AssertionError('only the last node should have'
37
 
                                     ' no next value: %s' % (node,))
38
 
            node_next = None
39
 
        else:
40
 
            node_next = lru._cache[node.next_key]
41
 
            if node_next.prev is not node:
42
 
                raise AssertionError('inconsistency found, node.next.prev'
43
 
                                     ' != node: %s' % (node,))
44
 
        if node.prev is None:
45
 
            if node is not lru._most_recently_used:
46
 
                raise AssertionError('only the _most_recently_used should'
47
 
                                     ' not have a previous node: %s'
48
 
                                     % (node,))
49
 
        else:
50
 
            if node.prev.next_key != node.key:
51
 
                raise AssertionError('inconsistency found, node.prev.next'
52
 
                                     ' != node: %s' % (node,))
53
 
        yield node
54
 
        node = node_next
55
 
 
56
 
 
57
 
class TestLRUCache(tests.TestCase):
58
 
    """Test that LRU cache properly keeps track of entries."""
59
 
 
60
 
    def test_cache_size(self):
61
 
        cache = lru_cache.LRUCache(max_cache=10)
62
 
        self.assertEqual(10, cache.cache_size())
63
 
 
64
 
        cache = lru_cache.LRUCache(max_cache=256)
65
 
        self.assertEqual(256, cache.cache_size())
66
 
 
67
 
        cache.resize(512)
68
 
        self.assertEqual(512, cache.cache_size())
69
 
 
70
 
    def test_missing(self):
71
 
        cache = lru_cache.LRUCache(max_cache=10)
72
 
 
73
 
        self.assertFalse('foo' in cache)
74
 
        self.assertRaises(KeyError, cache.__getitem__, 'foo')
75
 
 
76
 
        cache['foo'] = 'bar'
77
 
        self.assertEqual('bar', cache['foo'])
78
 
        self.assertTrue('foo' in cache)
79
 
        self.assertFalse('bar' in cache)
80
 
 
81
 
    def test_map_None(self):
82
 
        # Make sure that we can properly map None as a key.
83
 
        cache = lru_cache.LRUCache(max_cache=10)
84
 
        self.assertFalse(None in cache)
85
 
        cache[None] = 1
86
 
        self.assertEqual(1, cache[None])
87
 
        cache[None] = 2
88
 
        self.assertEqual(2, cache[None])
89
 
        # Test the various code paths of __getitem__, to make sure that we can
90
 
        # handle when None is the key for the LRU and the MRU
91
 
        cache[1] = 3
92
 
        cache[None] = 1
93
 
        cache[None]
94
 
        cache[1]
95
 
        cache[None]
96
 
        self.assertEqual([None, 1], [n.key for n in walk_lru(cache)])
97
 
 
98
 
    def test_add__null_key(self):
99
 
        cache = lru_cache.LRUCache(max_cache=10)
100
 
        self.assertRaises(ValueError,
101
 
                          cache.__setitem__, lru_cache._null_key, 1)
102
 
 
103
 
    def test_overflow(self):
104
 
        """Adding extra entries will pop out old ones."""
105
 
        cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
106
 
 
107
 
        cache['foo'] = 'bar'
108
 
        # With a max cache of 1, adding 'baz' should pop out 'foo'
109
 
        cache['baz'] = 'biz'
110
 
 
111
 
        self.assertFalse('foo' in cache)
112
 
        self.assertTrue('baz' in cache)
113
 
 
114
 
        self.assertEqual('biz', cache['baz'])
115
 
 
116
 
    def test_by_usage(self):
117
 
        """Accessing entries bumps them up in priority."""
118
 
        cache = lru_cache.LRUCache(max_cache=2)
119
 
 
120
 
        cache['baz'] = 'biz'
121
 
        cache['foo'] = 'bar'
122
 
 
123
 
        self.assertEqual('biz', cache['baz'])
124
 
 
125
 
        # This must kick out 'foo' because it was the last accessed
126
 
        cache['nub'] = 'in'
127
 
 
128
 
        self.assertFalse('foo' in cache)
129
 
 
130
 
    def test_len(self):
131
 
        cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
132
 
 
133
 
        cache[1] = 10
134
 
        cache[2] = 20
135
 
        cache[3] = 30
136
 
        cache[4] = 40
137
 
 
138
 
        self.assertEqual(4, len(cache))
139
 
 
140
 
        cache[5] = 50
141
 
        cache[6] = 60
142
 
        cache[7] = 70
143
 
        cache[8] = 80
144
 
 
145
 
        self.assertEqual(8, len(cache))
146
 
 
147
 
        cache[1] = 15  # replacement
148
 
 
149
 
        self.assertEqual(8, len(cache))
150
 
 
151
 
        cache[9] = 90
152
 
        cache[10] = 100
153
 
        cache[11] = 110
154
 
 
155
 
        # We hit the max
156
 
        self.assertEqual(10, len(cache))
157
 
        self.assertEqual([11, 10, 9, 1, 8, 7, 6, 5, 4, 3],
158
 
                         [n.key for n in walk_lru(cache)])
159
 
 
160
 
    def test_cleanup_shrinks_to_after_clean_count(self):
161
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
162
 
 
163
 
        cache[1] = 10
164
 
        cache[2] = 20
165
 
        cache[3] = 25
166
 
        cache[4] = 30
167
 
        cache[5] = 35
168
 
 
169
 
        self.assertEqual(5, len(cache))
170
 
        # This will bump us over the max, which causes us to shrink down to
171
 
        # after_cleanup_cache size
172
 
        cache[6] = 40
173
 
        self.assertEqual(3, len(cache))
174
 
 
175
 
    def test_after_cleanup_larger_than_max(self):
176
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
177
 
        self.assertEqual(5, cache._after_cleanup_count)
178
 
 
179
 
    def test_after_cleanup_none(self):
180
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None)
181
 
        # By default _after_cleanup_size is 80% of the normal size
182
 
        self.assertEqual(4, cache._after_cleanup_count)
183
 
 
184
 
    def test_cleanup(self):
185
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
186
 
 
187
 
        # Add these in order
188
 
        cache[1] = 10
189
 
        cache[2] = 20
190
 
        cache[3] = 25
191
 
        cache[4] = 30
192
 
        cache[5] = 35
193
 
 
194
 
        self.assertEqual(5, len(cache))
195
 
        # Force a compaction
196
 
        cache.cleanup()
197
 
        self.assertEqual(2, len(cache))
198
 
 
199
 
    def test_preserve_last_access_order(self):
200
 
        cache = lru_cache.LRUCache(max_cache=5)
201
 
 
202
 
        # Add these in order
203
 
        cache[1] = 10
204
 
        cache[2] = 20
205
 
        cache[3] = 25
206
 
        cache[4] = 30
207
 
        cache[5] = 35
208
 
 
209
 
        self.assertEqual([5, 4, 3, 2, 1], [n.key for n in walk_lru(cache)])
210
 
 
211
 
        # Now access some randomly
212
 
        cache[2]
213
 
        cache[5]
214
 
        cache[3]
215
 
        cache[2]
216
 
        self.assertEqual([2, 3, 5, 4, 1], [n.key for n in walk_lru(cache)])
217
 
 
218
 
    def test_get(self):
219
 
        cache = lru_cache.LRUCache(max_cache=5)
220
 
 
221
 
        cache[1] = 10
222
 
        cache[2] = 20
223
 
        self.assertEqual(20, cache.get(2))
224
 
        self.assertIs(None, cache.get(3))
225
 
        obj = object()
226
 
        self.assertIs(obj, cache.get(3, obj))
227
 
        self.assertEqual([2, 1], [n.key for n in walk_lru(cache)])
228
 
        self.assertEqual(10, cache.get(1))
229
 
        self.assertEqual([1, 2], [n.key for n in walk_lru(cache)])
230
 
 
231
 
    def test_keys(self):
232
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
233
 
 
234
 
        cache[1] = 2
235
 
        cache[2] = 3
236
 
        cache[3] = 4
237
 
        self.assertEqual([1, 2, 3], sorted(cache.keys()))
238
 
        cache[4] = 5
239
 
        cache[5] = 6
240
 
        cache[6] = 7
241
 
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
242
 
 
243
 
    def test_resize_smaller(self):
244
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
245
 
        cache[1] = 2
246
 
        cache[2] = 3
247
 
        cache[3] = 4
248
 
        cache[4] = 5
249
 
        cache[5] = 6
250
 
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
251
 
        cache[6] = 7
252
 
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
253
 
        # Now resize to something smaller, which triggers a cleanup
254
 
        cache.resize(max_cache=3, after_cleanup_count=2)
255
 
        self.assertEqual([5, 6], sorted(cache.keys()))
256
 
        # Adding something will use the new size
257
 
        cache[7] = 8
258
 
        self.assertEqual([5, 6, 7], sorted(cache.keys()))
259
 
        cache[8] = 9
260
 
        self.assertEqual([7, 8], sorted(cache.keys()))
261
 
 
262
 
    def test_resize_larger(self):
263
 
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
264
 
        cache[1] = 2
265
 
        cache[2] = 3
266
 
        cache[3] = 4
267
 
        cache[4] = 5
268
 
        cache[5] = 6
269
 
        self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
270
 
        cache[6] = 7
271
 
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
272
 
        cache.resize(max_cache=8, after_cleanup_count=6)
273
 
        self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
274
 
        cache[7] = 8
275
 
        cache[8] = 9
276
 
        cache[9] = 10
277
 
        cache[10] = 11
278
 
        self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys()))
279
 
        cache[11] = 12  # triggers cleanup back to new after_cleanup_count
280
 
        self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys()))
281
 
 
282
 
 
283
 
class TestLRUSizeCache(tests.TestCase):
284
 
 
285
 
    def test_basic_init(self):
286
 
        cache = lru_cache.LRUSizeCache()
287
 
        self.assertEqual(2048, cache._max_cache)
288
 
        self.assertEqual(int(cache._max_size * 0.8), cache._after_cleanup_size)
289
 
        self.assertEqual(0, cache._value_size)
290
 
 
291
 
    def test_add__null_key(self):
292
 
        cache = lru_cache.LRUSizeCache()
293
 
        self.assertRaises(ValueError,
294
 
                          cache.__setitem__, lru_cache._null_key, 1)
295
 
 
296
 
    def test_add_tracks_size(self):
297
 
        cache = lru_cache.LRUSizeCache()
298
 
        self.assertEqual(0, cache._value_size)
299
 
        cache['my key'] = 'my value text'
300
 
        self.assertEqual(13, cache._value_size)
301
 
 
302
 
    def test_remove_tracks_size(self):
303
 
        cache = lru_cache.LRUSizeCache()
304
 
        self.assertEqual(0, cache._value_size)
305
 
        cache['my key'] = 'my value text'
306
 
        self.assertEqual(13, cache._value_size)
307
 
        node = cache._cache['my key']
308
 
        cache._remove_node(node)
309
 
        self.assertEqual(0, cache._value_size)
310
 
 
311
 
    def test_no_add_over_size(self):
312
 
        """Adding a large value may not be cached at all."""
313
 
        cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
314
 
        self.assertEqual(0, cache._value_size)
315
 
        self.assertEqual({}, cache.as_dict())
316
 
        cache['test'] = 'key'
317
 
        self.assertEqual(3, cache._value_size)
318
 
        self.assertEqual({'test': 'key'}, cache.as_dict())
319
 
        cache['test2'] = 'key that is too big'
320
 
        self.assertEqual(3, cache._value_size)
321
 
        self.assertEqual({'test': 'key'}, cache.as_dict())
322
 
        # If we would add a key, only to cleanup and remove all cached entries,
323
 
        # then obviously that value should not be stored
324
 
        cache['test3'] = 'bigkey'
325
 
        self.assertEqual(3, cache._value_size)
326
 
        self.assertEqual({'test': 'key'}, cache.as_dict())
327
 
 
328
 
        cache['test4'] = 'bikey'
329
 
        self.assertEqual(3, cache._value_size)
330
 
        self.assertEqual({'test': 'key'}, cache.as_dict())
331
 
 
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['key1'] = 'value'  # 5 chars
336
 
        cache['key2'] = 'value2'  # 6 chars
337
 
        cache['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['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'},
344
 
                         cache.as_dict())
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['key1'] = 'value'  # 5 chars
349
 
        cache['key2'] = 'value2'  # 6 chars
350
 
        cache['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['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)
356
 
        self.assertEqual({'key4': 'value234'}, cache.as_dict())
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['key1'] = ['val', 'ue']  # 5 chars
365
 
        cache['key2'] = ['val', 'ue2']  # 6 chars
366
 
        cache['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['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)
372
 
        self.assertEqual({'key4': ['value', '234']}, cache.as_dict())
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['key1'] = 'value'  # 5 chars
379
 
        cache['key2'] = 'value2'  # 6 chars
380
 
        cache['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)
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()))
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()))