~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lru_cache.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006, 2008, 2009 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
46
46
        self.failUnless('foo' in cache)
47
47
        self.failIf('bar' in cache)
48
48
 
 
49
    def test_map_None(self):
 
50
        # Make sure that we can properly map None as a key.
 
51
        cache = lru_cache.LRUCache(max_cache=10)
 
52
        self.failIf(None in cache)
 
53
        cache[None] = 1
 
54
        self.assertEqual(1, cache[None])
 
55
        cache[None] = 2
 
56
        self.assertEqual(2, cache[None])
 
57
        # Test the various code paths of __getitem__, to make sure that we can
 
58
        # handle when None is the key for the LRU and the MRU
 
59
        cache[1] = 3
 
60
        cache[None] = 1
 
61
        cache[None]
 
62
        cache[1]
 
63
        cache[None]
 
64
        self.assertEqual([None, 1], [n.key for n in cache._walk_lru()])
 
65
 
 
66
    def test_add__null_key(self):
 
67
        cache = lru_cache.LRUCache(max_cache=10)
 
68
        self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
 
69
 
49
70
    def test_overflow(self):
50
71
        """Adding extra entries will pop out old ones."""
51
72
        cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
111
132
        cache[2] = 26
112
133
        self.assertEqual([(2, 20), (2, 25)], cleanup_called)
113
134
 
 
135
    def test_cleanup_error_maintains_linked_list(self):
 
136
        cleanup_called = []
 
137
        def cleanup_func(key, val):
 
138
            cleanup_called.append((key, val))
 
139
            raise ValueError('failure during cleanup')
 
140
 
 
141
        cache = lru_cache.LRUCache(max_cache=10)
 
142
        for i in xrange(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)
 
147
 
 
148
        self.assertEqual([(i, i) for i in xrange(10)], cleanup_called)
 
149
 
 
150
        self.assertEqual(range(19, 9, -1), [n.key for n in cache._walk_lru()])
 
151
 
 
152
    def test_cleanup_during_replace_still_replaces(self):
 
153
        cleanup_called = []
 
154
        def cleanup_func(key, val):
 
155
            cleanup_called.append((key, val))
 
156
            raise ValueError('failure during cleanup')
 
157
 
 
158
        cache = lru_cache.LRUCache(max_cache=10)
 
159
        for i in xrange(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])
 
167
 
 
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()])
 
171
 
114
172
    def test_len(self):
115
173
        cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
116
174
 
138
196
 
139
197
        # We hit the max
140
198
        self.assertEqual(10, len(cache))
 
199
        self.assertEqual([11, 10, 9, 1, 8, 7, 6, 5, 4, 3],
 
200
                         [n.key for n in cache._walk_lru()])
141
201
 
142
202
    def test_cleanup_shrinks_to_after_clean_count(self):
143
203
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
222
282
        cache[6] = 7
223
283
        self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
224
284
 
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
285
    def test_resize_smaller(self):
233
286
        cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
234
287
        cache[1] = 2
277
330
        self.assertEqual(int(cache._max_size*0.8), cache._after_cleanup_size)
278
331
        self.assertEqual(0, cache._value_size)
279
332
 
 
333
    def test_add__null_key(self):
 
334
        cache = lru_cache.LRUSizeCache()
 
335
        self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
 
336
 
280
337
    def test_add_tracks_size(self):
281
338
        cache = lru_cache.LRUSizeCache()
282
339
        self.assertEqual(0, cache._value_size)