test_lrucache.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from mock import Mock
  16. from synapse.util.caches.lrucache import LruCache
  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.assertEquals(cache.get("key"), "value")
  25. self.assertEquals(cache["key"], "value")
  26. def test_eviction(self):
  27. cache = LruCache(2)
  28. cache[1] = 1
  29. cache[2] = 2
  30. self.assertEquals(cache.get(1), 1)
  31. self.assertEquals(cache.get(2), 2)
  32. cache[3] = 3
  33. self.assertEquals(cache.get(1), None)
  34. self.assertEquals(cache.get(2), 2)
  35. self.assertEquals(cache.get(3), 3)
  36. def test_setdefault(self):
  37. cache = LruCache(1)
  38. self.assertEquals(cache.setdefault("key", 1), 1)
  39. self.assertEquals(cache.get("key"), 1)
  40. self.assertEquals(cache.setdefault("key", 2), 1)
  41. self.assertEquals(cache.get("key"), 1)
  42. cache["key"] = 2 # Make sure overriding works.
  43. self.assertEquals(cache.get("key"), 2)
  44. def test_pop(self):
  45. cache = LruCache(1)
  46. cache["key"] = 1
  47. self.assertEquals(cache.pop("key"), 1)
  48. self.assertEquals(cache.pop("key"), None)
  49. def test_del_multi(self):
  50. cache = LruCache(4, keylen=2, 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.assertEquals(len(cache), 4)
  56. self.assertEquals(cache.get(("animal", "cat")), "mew")
  57. self.assertEquals(cache.get(("vehicles", "car")), "vroom")
  58. cache.del_multi(("animal",))
  59. self.assertEquals(len(cache), 2)
  60. self.assertEquals(cache.get(("animal", "cat")), None)
  61. self.assertEquals(cache.get(("animal", "dog")), None)
  62. self.assertEquals(cache.get(("vehicles", "car")), "vroom")
  63. self.assertEquals(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.assertEquals(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.assertEquals(m.call_count, 1)
  86. cache.set("key", "value")
  87. self.assertEquals(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.assertEquals(m.call_count, 1)
  99. cache.set("key", "value")
  100. self.assertEquals(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.assertEquals(m.call_count, 1)
  110. cache.set("key", "value")
  111. self.assertEquals(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.assertEquals(m.call_count, 1)
  119. cache.set("key", "value")
  120. self.assertEquals(m.call_count, 1)
  121. cache.pop("key")
  122. self.assertEquals(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, keylen=2, 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.assertEquals(m1.call_count, 0)
  134. self.assertEquals(m2.call_count, 0)
  135. self.assertEquals(m3.call_count, 0)
  136. self.assertEquals(m4.call_count, 0)
  137. cache.del_multi(("a",))
  138. self.assertEquals(m1.call_count, 1)
  139. self.assertEquals(m2.call_count, 1)
  140. self.assertEquals(m3.call_count, 0)
  141. self.assertEquals(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.assertEquals(m1.call_count, 0)
  149. self.assertEquals(m2.call_count, 0)
  150. cache.clear()
  151. self.assertEquals(m1.call_count, 1)
  152. self.assertEquals(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.assertEquals(m1.call_count, 0)
  161. self.assertEquals(m2.call_count, 0)
  162. self.assertEquals(m3.call_count, 0)
  163. cache.set("key3", "value", callbacks=[m3])
  164. self.assertEquals(m1.call_count, 1)
  165. self.assertEquals(m2.call_count, 0)
  166. self.assertEquals(m3.call_count, 0)
  167. cache.set("key3", "value")
  168. self.assertEquals(m1.call_count, 1)
  169. self.assertEquals(m2.call_count, 0)
  170. self.assertEquals(m3.call_count, 0)
  171. cache.get("key2")
  172. self.assertEquals(m1.call_count, 1)
  173. self.assertEquals(m2.call_count, 0)
  174. self.assertEquals(m3.call_count, 0)
  175. cache.set("key1", "value", callbacks=[m1])
  176. self.assertEquals(m1.call_count, 1)
  177. self.assertEquals(m2.call_count, 0)
  178. self.assertEquals(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.assertEquals(cache["key1"], [0])
  187. self.assertEquals(cache["key2"], [1, 2])
  188. self.assertEquals(cache["key3"], [3])
  189. self.assertEquals(cache["key4"], [4])
  190. self.assertEquals(len(cache), 5)
  191. cache["key5"] = [5, 6]
  192. self.assertEquals(len(cache), 4)
  193. self.assertEquals(cache.get("key1"), None)
  194. self.assertEquals(cache.get("key2"), None)
  195. self.assertEquals(cache["key3"], [3])
  196. self.assertEquals(cache["key4"], [4])
  197. self.assertEquals(cache["key5"], [5, 6])