test_lrucache.py 8.9 KB

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