2
0

test_lru_cache.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. # Copyright (C) 2006, 2008 Canonical Ltd
  2. #
  3. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for the lru_cache module."""
  21. from dulwich import lru_cache
  22. from . import TestCase
  23. class TestLRUCache(TestCase):
  24. """Test that LRU cache properly keeps track of entries."""
  25. def test_cache_size(self) -> None:
  26. cache = lru_cache.LRUCache(max_cache=10)
  27. self.assertEqual(10, cache.cache_size())
  28. cache = lru_cache.LRUCache(max_cache=256)
  29. self.assertEqual(256, cache.cache_size())
  30. cache.resize(512)
  31. self.assertEqual(512, cache.cache_size())
  32. def test_missing(self) -> None:
  33. cache = lru_cache.LRUCache(max_cache=10)
  34. self.assertNotIn("foo", cache)
  35. self.assertRaises(KeyError, cache.__getitem__, "foo")
  36. cache["foo"] = "bar"
  37. self.assertEqual("bar", cache["foo"])
  38. self.assertIn("foo", cache)
  39. self.assertNotIn("bar", cache)
  40. def test_map_None(self) -> None:
  41. # Make sure that we can properly map None as a key.
  42. cache = lru_cache.LRUCache(max_cache=10)
  43. self.assertNotIn(None, cache)
  44. cache[None] = 1
  45. self.assertEqual(1, cache[None])
  46. cache[None] = 2
  47. self.assertEqual(2, cache[None])
  48. # Test the various code paths of __getitem__, to make sure that we can
  49. # handle when None is the key for the LRU and the MRU
  50. cache[1] = 3
  51. cache[None] = 1
  52. cache[None]
  53. cache[1]
  54. cache[None]
  55. self.assertEqual([None, 1], [n.key for n in cache._walk_lru()])
  56. def test_add__null_key(self) -> None:
  57. cache = lru_cache.LRUCache(max_cache=10)
  58. self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
  59. def test_overflow(self) -> None:
  60. """Adding extra entries will pop out old ones."""
  61. cache = lru_cache.LRUCache(max_cache=1, after_cleanup_count=1)
  62. cache["foo"] = "bar"
  63. # With a max cache of 1, adding 'baz' should pop out 'foo'
  64. cache["baz"] = "biz"
  65. self.assertNotIn("foo", cache)
  66. self.assertIn("baz", cache)
  67. self.assertEqual("biz", cache["baz"])
  68. def test_by_usage(self) -> None:
  69. """Accessing entries bumps them up in priority."""
  70. cache = lru_cache.LRUCache(max_cache=2)
  71. cache["baz"] = "biz"
  72. cache["foo"] = "bar"
  73. self.assertEqual("biz", cache["baz"])
  74. # This must kick out 'foo' because it was the last accessed
  75. cache["nub"] = "in"
  76. self.assertNotIn("foo", cache)
  77. def test_cleanup(self) -> None:
  78. """Test that we can use a cleanup function."""
  79. cleanup_called = []
  80. def cleanup_func(key, val) -> None:
  81. cleanup_called.append((key, val))
  82. cache = lru_cache.LRUCache(max_cache=2, after_cleanup_count=2)
  83. cache.add("baz", "1", cleanup=cleanup_func)
  84. cache.add("foo", "2", cleanup=cleanup_func)
  85. cache.add("biz", "3", cleanup=cleanup_func)
  86. self.assertEqual([("baz", "1")], cleanup_called)
  87. # 'foo' is now most recent, so final cleanup will call it last
  88. cache["foo"]
  89. cache.clear()
  90. self.assertEqual([("baz", "1"), ("biz", "3"), ("foo", "2")], cleanup_called)
  91. def test_cleanup_on_replace(self) -> None:
  92. """Replacing an object should cleanup the old value."""
  93. cleanup_called = []
  94. def cleanup_func(key, val) -> None:
  95. cleanup_called.append((key, val))
  96. cache = lru_cache.LRUCache(max_cache=2)
  97. cache.add(1, 10, cleanup=cleanup_func)
  98. cache.add(2, 20, cleanup=cleanup_func)
  99. cache.add(2, 25, cleanup=cleanup_func)
  100. self.assertEqual([(2, 20)], cleanup_called)
  101. self.assertEqual(25, cache[2])
  102. # Even __setitem__ should make sure cleanup() is called
  103. cache[2] = 26
  104. self.assertEqual([(2, 20), (2, 25)], cleanup_called)
  105. def test_len(self) -> None:
  106. cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
  107. cache[1] = 10
  108. cache[2] = 20
  109. cache[3] = 30
  110. cache[4] = 40
  111. self.assertEqual(4, len(cache))
  112. cache[5] = 50
  113. cache[6] = 60
  114. cache[7] = 70
  115. cache[8] = 80
  116. self.assertEqual(8, len(cache))
  117. cache[1] = 15 # replacement
  118. self.assertEqual(8, len(cache))
  119. cache[9] = 90
  120. cache[10] = 100
  121. cache[11] = 110
  122. # We hit the max
  123. self.assertEqual(10, len(cache))
  124. self.assertEqual(
  125. [11, 10, 9, 1, 8, 7, 6, 5, 4, 3],
  126. [n.key for n in cache._walk_lru()],
  127. )
  128. def test_cleanup_shrinks_to_after_clean_count(self) -> None:
  129. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=3)
  130. cache.add(1, 10)
  131. cache.add(2, 20)
  132. cache.add(3, 25)
  133. cache.add(4, 30)
  134. cache.add(5, 35)
  135. self.assertEqual(5, len(cache))
  136. # This will bump us over the max, which causes us to shrink down to
  137. # after_cleanup_cache size
  138. cache.add(6, 40)
  139. self.assertEqual(3, len(cache))
  140. def test_after_cleanup_larger_than_max(self) -> None:
  141. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=10)
  142. self.assertEqual(5, cache._after_cleanup_count)
  143. def test_after_cleanup_none(self) -> None:
  144. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=None)
  145. # By default _after_cleanup_size is 80% of the normal size
  146. self.assertEqual(4, cache._after_cleanup_count)
  147. def test_cleanup_2(self) -> None:
  148. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
  149. # Add these in order
  150. cache.add(1, 10)
  151. cache.add(2, 20)
  152. cache.add(3, 25)
  153. cache.add(4, 30)
  154. cache.add(5, 35)
  155. self.assertEqual(5, len(cache))
  156. # Force a compaction
  157. cache.cleanup()
  158. self.assertEqual(2, len(cache))
  159. def test_preserve_last_access_order(self) -> None:
  160. cache = lru_cache.LRUCache(max_cache=5)
  161. # Add these in order
  162. cache.add(1, 10)
  163. cache.add(2, 20)
  164. cache.add(3, 25)
  165. cache.add(4, 30)
  166. cache.add(5, 35)
  167. self.assertEqual([5, 4, 3, 2, 1], [n.key for n in cache._walk_lru()])
  168. # Now access some randomly
  169. cache[2]
  170. cache[5]
  171. cache[3]
  172. cache[2]
  173. self.assertEqual([2, 3, 5, 4, 1], [n.key for n in cache._walk_lru()])
  174. def test_get(self) -> None:
  175. cache = lru_cache.LRUCache(max_cache=5)
  176. cache.add(1, 10)
  177. cache.add(2, 20)
  178. self.assertEqual(20, cache.get(2))
  179. self.assertEqual(None, cache.get(3))
  180. obj = object()
  181. self.assertIs(obj, cache.get(3, obj))
  182. self.assertEqual([2, 1], [n.key for n in cache._walk_lru()])
  183. self.assertEqual(10, cache.get(1))
  184. self.assertEqual([1, 2], [n.key for n in cache._walk_lru()])
  185. def test_keys(self) -> None:
  186. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=5)
  187. cache[1] = 2
  188. cache[2] = 3
  189. cache[3] = 4
  190. self.assertEqual([1, 2, 3], sorted(cache.keys()))
  191. cache[4] = 5
  192. cache[5] = 6
  193. cache[6] = 7
  194. self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
  195. def test_resize_smaller(self) -> None:
  196. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
  197. cache[1] = 2
  198. cache[2] = 3
  199. cache[3] = 4
  200. cache[4] = 5
  201. cache[5] = 6
  202. self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
  203. cache[6] = 7
  204. self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
  205. # Now resize to something smaller, which triggers a cleanup
  206. cache.resize(max_cache=3, after_cleanup_count=2)
  207. self.assertEqual([5, 6], sorted(cache.keys()))
  208. # Adding something will use the new size
  209. cache[7] = 8
  210. self.assertEqual([5, 6, 7], sorted(cache.keys()))
  211. cache[8] = 9
  212. self.assertEqual([7, 8], sorted(cache.keys()))
  213. def test_resize_larger(self) -> None:
  214. cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)
  215. cache[1] = 2
  216. cache[2] = 3
  217. cache[3] = 4
  218. cache[4] = 5
  219. cache[5] = 6
  220. self.assertEqual([1, 2, 3, 4, 5], sorted(cache.keys()))
  221. cache[6] = 7
  222. self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
  223. cache.resize(max_cache=8, after_cleanup_count=6)
  224. self.assertEqual([3, 4, 5, 6], sorted(cache.keys()))
  225. cache[7] = 8
  226. cache[8] = 9
  227. cache[9] = 10
  228. cache[10] = 11
  229. self.assertEqual([3, 4, 5, 6, 7, 8, 9, 10], sorted(cache.keys()))
  230. cache[11] = 12 # triggers cleanup back to new after_cleanup_count
  231. self.assertEqual([6, 7, 8, 9, 10, 11], sorted(cache.keys()))
  232. class TestLRUSizeCache(TestCase):
  233. def test_basic_init(self) -> None:
  234. cache = lru_cache.LRUSizeCache()
  235. self.assertEqual(2048, cache._max_cache)
  236. self.assertEqual(int(cache._max_size * 0.8), cache._after_cleanup_size)
  237. self.assertEqual(0, cache._value_size)
  238. def test_add__null_key(self) -> None:
  239. cache = lru_cache.LRUSizeCache()
  240. self.assertRaises(ValueError, cache.add, lru_cache._null_key, 1)
  241. def test_add_tracks_size(self) -> None:
  242. cache = lru_cache.LRUSizeCache()
  243. self.assertEqual(0, cache._value_size)
  244. cache.add("my key", "my value text")
  245. self.assertEqual(13, cache._value_size)
  246. def test_remove_tracks_size(self) -> None:
  247. cache = lru_cache.LRUSizeCache()
  248. self.assertEqual(0, cache._value_size)
  249. cache.add("my key", "my value text")
  250. self.assertEqual(13, cache._value_size)
  251. node = cache._cache["my key"]
  252. cache._remove_node(node)
  253. self.assertEqual(0, cache._value_size)
  254. def test_no_add_over_size(self) -> None:
  255. """Adding a large value may not be cached at all."""
  256. cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
  257. self.assertEqual(0, cache._value_size)
  258. self.assertEqual({}, cache.items())
  259. cache.add("test", "key")
  260. self.assertEqual(3, cache._value_size)
  261. self.assertEqual({"test": "key"}, cache.items())
  262. cache.add("test2", "key that is too big")
  263. self.assertEqual(3, cache._value_size)
  264. self.assertEqual({"test": "key"}, cache.items())
  265. # If we would add a key, only to cleanup and remove all cached entries,
  266. # then obviously that value should not be stored
  267. cache.add("test3", "bigkey")
  268. self.assertEqual(3, cache._value_size)
  269. self.assertEqual({"test": "key"}, cache.items())
  270. cache.add("test4", "bikey")
  271. self.assertEqual(3, cache._value_size)
  272. self.assertEqual({"test": "key"}, cache.items())
  273. def test_no_add_over_size_cleanup(self) -> None:
  274. """If a large value is not cached, we will call cleanup right away."""
  275. cleanup_calls = []
  276. def cleanup(key, value) -> None:
  277. cleanup_calls.append((key, value))
  278. cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=5)
  279. self.assertEqual(0, cache._value_size)
  280. self.assertEqual({}, cache.items())
  281. cache.add("test", "key that is too big", cleanup=cleanup)
  282. # key was not added
  283. self.assertEqual(0, cache._value_size)
  284. self.assertEqual({}, cache.items())
  285. # and cleanup was called
  286. self.assertEqual([("test", "key that is too big")], cleanup_calls)
  287. def test_adding_clears_cache_based_on_size(self) -> None:
  288. """The cache is cleared in LRU order until small enough."""
  289. cache = lru_cache.LRUSizeCache(max_size=20)
  290. cache.add("key1", "value") # 5 chars
  291. cache.add("key2", "value2") # 6 chars
  292. cache.add("key3", "value23") # 7 chars
  293. self.assertEqual(5 + 6 + 7, cache._value_size)
  294. cache["key2"] # reference key2 so it gets a newer reference time
  295. cache.add("key4", "value234") # 8 chars, over limit
  296. # We have to remove 2 keys to get back under limit
  297. self.assertEqual(6 + 8, cache._value_size)
  298. self.assertEqual({"key2": "value2", "key4": "value234"}, cache.items())
  299. def test_adding_clears_to_after_cleanup_size(self) -> None:
  300. cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
  301. cache.add("key1", "value") # 5 chars
  302. cache.add("key2", "value2") # 6 chars
  303. cache.add("key3", "value23") # 7 chars
  304. self.assertEqual(5 + 6 + 7, cache._value_size)
  305. cache["key2"] # reference key2 so it gets a newer reference time
  306. cache.add("key4", "value234") # 8 chars, over limit
  307. # We have to remove 3 keys to get back under limit
  308. self.assertEqual(8, cache._value_size)
  309. self.assertEqual({"key4": "value234"}, cache.items())
  310. def test_custom_sizes(self) -> None:
  311. def size_of_list(lst):
  312. return sum(len(x) for x in lst)
  313. cache = lru_cache.LRUSizeCache(
  314. max_size=20, after_cleanup_size=10, compute_size=size_of_list
  315. )
  316. cache.add("key1", ["val", "ue"]) # 5 chars
  317. cache.add("key2", ["val", "ue2"]) # 6 chars
  318. cache.add("key3", ["val", "ue23"]) # 7 chars
  319. self.assertEqual(5 + 6 + 7, cache._value_size)
  320. cache["key2"] # reference key2 so it gets a newer reference time
  321. cache.add("key4", ["value", "234"]) # 8 chars, over limit
  322. # We have to remove 3 keys to get back under limit
  323. self.assertEqual(8, cache._value_size)
  324. self.assertEqual({"key4": ["value", "234"]}, cache.items())
  325. def test_cleanup(self) -> None:
  326. cache = lru_cache.LRUSizeCache(max_size=20, after_cleanup_size=10)
  327. # Add these in order
  328. cache.add("key1", "value") # 5 chars
  329. cache.add("key2", "value2") # 6 chars
  330. cache.add("key3", "value23") # 7 chars
  331. self.assertEqual(5 + 6 + 7, cache._value_size)
  332. cache.cleanup()
  333. # Only the most recent fits after cleaning up
  334. self.assertEqual(7, cache._value_size)
  335. def test_keys(self) -> None:
  336. cache = lru_cache.LRUSizeCache(max_size=10)
  337. cache[1] = "a"
  338. cache[2] = "b"
  339. cache[3] = "cdef"
  340. self.assertEqual([1, 2, 3], sorted(cache.keys()))
  341. def test_resize_smaller(self) -> None:
  342. cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
  343. cache[1] = "abc"
  344. cache[2] = "def"
  345. cache[3] = "ghi"
  346. cache[4] = "jkl"
  347. # Triggers a cleanup
  348. self.assertEqual([2, 3, 4], sorted(cache.keys()))
  349. # Resize should also cleanup again
  350. cache.resize(max_size=6, after_cleanup_size=4)
  351. self.assertEqual([4], sorted(cache.keys()))
  352. # Adding should use the new max size
  353. cache[5] = "mno"
  354. self.assertEqual([4, 5], sorted(cache.keys()))
  355. cache[6] = "pqr"
  356. self.assertEqual([6], sorted(cache.keys()))
  357. def test_resize_larger(self) -> None:
  358. cache = lru_cache.LRUSizeCache(max_size=10, after_cleanup_size=9)
  359. cache[1] = "abc"
  360. cache[2] = "def"
  361. cache[3] = "ghi"
  362. cache[4] = "jkl"
  363. # Triggers a cleanup
  364. self.assertEqual([2, 3, 4], sorted(cache.keys()))
  365. cache.resize(max_size=15, after_cleanup_size=12)
  366. self.assertEqual([2, 3, 4], sorted(cache.keys()))
  367. cache[5] = "mno"
  368. cache[6] = "pqr"
  369. self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
  370. cache[7] = "stu"
  371. self.assertEqual([4, 5, 6, 7], sorted(cache.keys()))