test_lrucache.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  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. from typing import List
  15. from unittest.mock import Mock
  16. from synapse.util.caches.lrucache import LruCache, setup_expire_lru_cache_entries
  17. from synapse.util.caches.treecache import TreeCache
  18. from tests import unittest
  19. from tests.unittest import override_config
  20. class LruCacheTestCase(unittest.HomeserverTestCase):
  21. def test_get_set(self):
  22. cache = LruCache(1)
  23. cache["key"] = "value"
  24. self.assertEqual(cache.get("key"), "value")
  25. self.assertEqual(cache["key"], "value")
  26. def test_eviction(self):
  27. cache = LruCache(2)
  28. cache[1] = 1
  29. cache[2] = 2
  30. self.assertEqual(cache.get(1), 1)
  31. self.assertEqual(cache.get(2), 2)
  32. cache[3] = 3
  33. self.assertEqual(cache.get(1), None)
  34. self.assertEqual(cache.get(2), 2)
  35. self.assertEqual(cache.get(3), 3)
  36. def test_setdefault(self):
  37. cache = LruCache(1)
  38. self.assertEqual(cache.setdefault("key", 1), 1)
  39. self.assertEqual(cache.get("key"), 1)
  40. self.assertEqual(cache.setdefault("key", 2), 1)
  41. self.assertEqual(cache.get("key"), 1)
  42. cache["key"] = 2 # Make sure overriding works.
  43. self.assertEqual(cache.get("key"), 2)
  44. def test_pop(self):
  45. cache = LruCache(1)
  46. cache["key"] = 1
  47. self.assertEqual(cache.pop("key"), 1)
  48. self.assertEqual(cache.pop("key"), None)
  49. def test_del_multi(self):
  50. cache = LruCache(4, cache_type=TreeCache)
  51. cache[("animal", "cat")] = "mew"
  52. cache[("animal", "dog")] = "woof"
  53. cache[("vehicles", "car")] = "vroom"
  54. cache[("vehicles", "train")] = "chuff"
  55. self.assertEqual(len(cache), 4)
  56. self.assertEqual(cache.get(("animal", "cat")), "mew")
  57. self.assertEqual(cache.get(("vehicles", "car")), "vroom")
  58. cache.del_multi(("animal",))
  59. self.assertEqual(len(cache), 2)
  60. self.assertEqual(cache.get(("animal", "cat")), None)
  61. self.assertEqual(cache.get(("animal", "dog")), None)
  62. self.assertEqual(cache.get(("vehicles", "car")), "vroom")
  63. self.assertEqual(cache.get(("vehicles", "train")), "chuff")
  64. # Man from del_multi say "Yes".
  65. def test_clear(self):
  66. cache = LruCache(1)
  67. cache["key"] = 1
  68. cache.clear()
  69. self.assertEqual(len(cache), 0)
  70. @override_config({"caches": {"per_cache_factors": {"mycache": 10}}})
  71. def test_special_size(self):
  72. cache = LruCache(10, "mycache")
  73. self.assertEqual(cache.max_size, 100)
  74. class LruCacheCallbacksTestCase(unittest.HomeserverTestCase):
  75. def test_get(self):
  76. m = Mock()
  77. cache = LruCache(1)
  78. cache.set("key", "value")
  79. self.assertFalse(m.called)
  80. cache.get("key", callbacks=[m])
  81. self.assertFalse(m.called)
  82. cache.get("key", "value")
  83. self.assertFalse(m.called)
  84. cache.set("key", "value2")
  85. self.assertEqual(m.call_count, 1)
  86. cache.set("key", "value")
  87. self.assertEqual(m.call_count, 1)
  88. def test_multi_get(self):
  89. m = Mock()
  90. cache = LruCache(1)
  91. cache.set("key", "value")
  92. self.assertFalse(m.called)
  93. cache.get("key", callbacks=[m])
  94. self.assertFalse(m.called)
  95. cache.get("key", callbacks=[m])
  96. self.assertFalse(m.called)
  97. cache.set("key", "value2")
  98. self.assertEqual(m.call_count, 1)
  99. cache.set("key", "value")
  100. self.assertEqual(m.call_count, 1)
  101. def test_set(self):
  102. m = Mock()
  103. cache = LruCache(1)
  104. cache.set("key", "value", callbacks=[m])
  105. self.assertFalse(m.called)
  106. cache.set("key", "value")
  107. self.assertFalse(m.called)
  108. cache.set("key", "value2")
  109. self.assertEqual(m.call_count, 1)
  110. cache.set("key", "value")
  111. self.assertEqual(m.call_count, 1)
  112. def test_pop(self):
  113. m = Mock()
  114. cache = LruCache(1)
  115. cache.set("key", "value", callbacks=[m])
  116. self.assertFalse(m.called)
  117. cache.pop("key")
  118. self.assertEqual(m.call_count, 1)
  119. cache.set("key", "value")
  120. self.assertEqual(m.call_count, 1)
  121. cache.pop("key")
  122. self.assertEqual(m.call_count, 1)
  123. def test_del_multi(self):
  124. m1 = Mock()
  125. m2 = Mock()
  126. m3 = Mock()
  127. m4 = Mock()
  128. cache = LruCache(4, cache_type=TreeCache)
  129. cache.set(("a", "1"), "value", callbacks=[m1])
  130. cache.set(("a", "2"), "value", callbacks=[m2])
  131. cache.set(("b", "1"), "value", callbacks=[m3])
  132. cache.set(("b", "2"), "value", callbacks=[m4])
  133. self.assertEqual(m1.call_count, 0)
  134. self.assertEqual(m2.call_count, 0)
  135. self.assertEqual(m3.call_count, 0)
  136. self.assertEqual(m4.call_count, 0)
  137. cache.del_multi(("a",))
  138. self.assertEqual(m1.call_count, 1)
  139. self.assertEqual(m2.call_count, 1)
  140. self.assertEqual(m3.call_count, 0)
  141. self.assertEqual(m4.call_count, 0)
  142. def test_clear(self):
  143. m1 = Mock()
  144. m2 = Mock()
  145. cache = LruCache(5)
  146. cache.set("key1", "value", callbacks=[m1])
  147. cache.set("key2", "value", callbacks=[m2])
  148. self.assertEqual(m1.call_count, 0)
  149. self.assertEqual(m2.call_count, 0)
  150. cache.clear()
  151. self.assertEqual(m1.call_count, 1)
  152. self.assertEqual(m2.call_count, 1)
  153. def test_eviction(self):
  154. m1 = Mock(name="m1")
  155. m2 = Mock(name="m2")
  156. m3 = Mock(name="m3")
  157. cache = LruCache(2)
  158. cache.set("key1", "value", callbacks=[m1])
  159. cache.set("key2", "value", callbacks=[m2])
  160. self.assertEqual(m1.call_count, 0)
  161. self.assertEqual(m2.call_count, 0)
  162. self.assertEqual(m3.call_count, 0)
  163. cache.set("key3", "value", callbacks=[m3])
  164. self.assertEqual(m1.call_count, 1)
  165. self.assertEqual(m2.call_count, 0)
  166. self.assertEqual(m3.call_count, 0)
  167. cache.set("key3", "value")
  168. self.assertEqual(m1.call_count, 1)
  169. self.assertEqual(m2.call_count, 0)
  170. self.assertEqual(m3.call_count, 0)
  171. cache.get("key2")
  172. self.assertEqual(m1.call_count, 1)
  173. self.assertEqual(m2.call_count, 0)
  174. self.assertEqual(m3.call_count, 0)
  175. cache.set("key1", "value", callbacks=[m1])
  176. self.assertEqual(m1.call_count, 1)
  177. self.assertEqual(m2.call_count, 0)
  178. self.assertEqual(m3.call_count, 1)
  179. class LruCacheSizedTestCase(unittest.HomeserverTestCase):
  180. def test_evict(self):
  181. cache = LruCache(5, size_callback=len)
  182. cache["key1"] = [0]
  183. cache["key2"] = [1, 2]
  184. cache["key3"] = [3]
  185. cache["key4"] = [4]
  186. self.assertEqual(cache["key1"], [0])
  187. self.assertEqual(cache["key2"], [1, 2])
  188. self.assertEqual(cache["key3"], [3])
  189. self.assertEqual(cache["key4"], [4])
  190. self.assertEqual(len(cache), 5)
  191. cache["key5"] = [5, 6]
  192. self.assertEqual(len(cache), 4)
  193. self.assertEqual(cache.get("key1"), None)
  194. self.assertEqual(cache.get("key2"), None)
  195. self.assertEqual(cache["key3"], [3])
  196. self.assertEqual(cache["key4"], [4])
  197. self.assertEqual(cache["key5"], [5, 6])
  198. def test_zero_size_drop_from_cache(self) -> None:
  199. """Test that `drop_from_cache` works correctly with 0-sized entries."""
  200. cache: LruCache[str, List[int]] = LruCache(5, size_callback=lambda x: 0)
  201. cache["key1"] = []
  202. self.assertEqual(len(cache), 0)
  203. cache.cache["key1"].drop_from_cache()
  204. self.assertIsNone(
  205. cache.pop("key1"), "Cache entry should have been evicted but wasn't"
  206. )
  207. class TimeEvictionTestCase(unittest.HomeserverTestCase):
  208. """Test that time based eviction works correctly."""
  209. def default_config(self):
  210. config = super().default_config()
  211. config.setdefault("caches", {})["expiry_time"] = "30m"
  212. return config
  213. def test_evict(self):
  214. setup_expire_lru_cache_entries(self.hs)
  215. cache = LruCache(5, clock=self.hs.get_clock())
  216. # Check that we evict entries we haven't accessed for 30 minutes.
  217. cache["key1"] = 1
  218. cache["key2"] = 2
  219. self.reactor.advance(20 * 60)
  220. self.assertEqual(cache.get("key1"), 1)
  221. self.reactor.advance(20 * 60)
  222. # We have only touched `key1` in the last 30m, so we expect that to
  223. # still be in the cache while `key2` should have been evicted.
  224. self.assertEqual(cache.get("key1"), 1)
  225. self.assertEqual(cache.get("key2"), None)
  226. # Check that re-adding an expired key works correctly.
  227. cache["key2"] = 3
  228. self.assertEqual(cache.get("key2"), 3)
  229. self.reactor.advance(20 * 60)
  230. self.assertEqual(cache.get("key2"), 3)
  231. self.reactor.advance(20 * 60)
  232. self.assertEqual(cache.get("key1"), None)
  233. self.assertEqual(cache.get("key2"), 3)