156
156
self.assertEqual(3, len(cache))
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)
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)
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)
169
170
# Add these in order
226
227
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
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)
236
def test_resize_smaller(self):
237
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
243
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
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
251
self.assertEqual([5, 6, 7], sorted(cache.keys()))
253
self.assertEqual([7, 8], sorted(cache.keys()))
255
def test_resize_larger(self):
256
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
262
self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
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()))
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()))
229
276
class TestLRUSizeCache(tests.TestCase):
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)
238
285
def test_add_tracks_size(self):
333
380
cache[3] = 'cdef'
334
381
self.assertEqual([1, 2, 3], sorted(cache.keys()))
383
def test_resize_smaller(self):
384
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
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
396
self.assertEqual([4, 5], sorted(cache.keys()))
398
self.assertEqual([6], sorted(cache.keys()))
400
def test_resize_larger(self):
401
cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
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()))
412
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
414
self.assertEqual([4, 5, 6, 7], sorted(cache.keys()))