test_lrucache.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 .. import unittest
  19. class LruCacheTestCase(unittest.TestCase):
  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, 2, 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. class LruCacheCallbacksTestCase(unittest.TestCase):
  70. def test_get(self):
  71. m = Mock()
  72. cache = LruCache(1)
  73. cache.set("key", "value")
  74. self.assertFalse(m.called)
  75. cache.get("key", callbacks=[m])
  76. self.assertFalse(m.called)
  77. cache.get("key", "value")
  78. self.assertFalse(m.called)
  79. cache.set("key", "value2")
  80. self.assertEquals(m.call_count, 1)
  81. cache.set("key", "value")
  82. self.assertEquals(m.call_count, 1)
  83. def test_multi_get(self):
  84. m = Mock()
  85. cache = LruCache(1)
  86. cache.set("key", "value")
  87. self.assertFalse(m.called)
  88. cache.get("key", callbacks=[m])
  89. self.assertFalse(m.called)
  90. cache.get("key", callbacks=[m])
  91. self.assertFalse(m.called)
  92. cache.set("key", "value2")
  93. self.assertEquals(m.call_count, 1)
  94. cache.set("key", "value")
  95. self.assertEquals(m.call_count, 1)
  96. def test_set(self):
  97. m = Mock()
  98. cache = LruCache(1)
  99. cache.set("key", "value", callbacks=[m])
  100. self.assertFalse(m.called)
  101. cache.set("key", "value")
  102. self.assertFalse(m.called)
  103. cache.set("key", "value2")
  104. self.assertEquals(m.call_count, 1)
  105. cache.set("key", "value")
  106. self.assertEquals(m.call_count, 1)
  107. def test_pop(self):
  108. m = Mock()
  109. cache = LruCache(1)
  110. cache.set("key", "value", callbacks=[m])
  111. self.assertFalse(m.called)
  112. cache.pop("key")
  113. self.assertEquals(m.call_count, 1)
  114. cache.set("key", "value")
  115. self.assertEquals(m.call_count, 1)
  116. cache.pop("key")
  117. self.assertEquals(m.call_count, 1)
  118. def test_del_multi(self):
  119. m1 = Mock()
  120. m2 = Mock()
  121. m3 = Mock()
  122. m4 = Mock()
  123. cache = LruCache(4, 2, cache_type=TreeCache)
  124. cache.set(("a", "1"), "value", callbacks=[m1])
  125. cache.set(("a", "2"), "value", callbacks=[m2])
  126. cache.set(("b", "1"), "value", callbacks=[m3])
  127. cache.set(("b", "2"), "value", callbacks=[m4])
  128. self.assertEquals(m1.call_count, 0)
  129. self.assertEquals(m2.call_count, 0)
  130. self.assertEquals(m3.call_count, 0)
  131. self.assertEquals(m4.call_count, 0)
  132. cache.del_multi(("a",))
  133. self.assertEquals(m1.call_count, 1)
  134. self.assertEquals(m2.call_count, 1)
  135. self.assertEquals(m3.call_count, 0)
  136. self.assertEquals(m4.call_count, 0)
  137. def test_clear(self):
  138. m1 = Mock()
  139. m2 = Mock()
  140. cache = LruCache(5)
  141. cache.set("key1", "value", callbacks=[m1])
  142. cache.set("key2", "value", callbacks=[m2])
  143. self.assertEquals(m1.call_count, 0)
  144. self.assertEquals(m2.call_count, 0)
  145. cache.clear()
  146. self.assertEquals(m1.call_count, 1)
  147. self.assertEquals(m2.call_count, 1)
  148. def test_eviction(self):
  149. m1 = Mock(name="m1")
  150. m2 = Mock(name="m2")
  151. m3 = Mock(name="m3")
  152. cache = LruCache(2)
  153. cache.set("key1", "value", callbacks=[m1])
  154. cache.set("key2", "value", callbacks=[m2])
  155. self.assertEquals(m1.call_count, 0)
  156. self.assertEquals(m2.call_count, 0)
  157. self.assertEquals(m3.call_count, 0)
  158. cache.set("key3", "value", callbacks=[m3])
  159. self.assertEquals(m1.call_count, 1)
  160. self.assertEquals(m2.call_count, 0)
  161. self.assertEquals(m3.call_count, 0)
  162. cache.set("key3", "value")
  163. self.assertEquals(m1.call_count, 1)
  164. self.assertEquals(m2.call_count, 0)
  165. self.assertEquals(m3.call_count, 0)
  166. cache.get("key2")
  167. self.assertEquals(m1.call_count, 1)
  168. self.assertEquals(m2.call_count, 0)
  169. self.assertEquals(m3.call_count, 0)
  170. cache.set("key1", "value", callbacks=[m1])
  171. self.assertEquals(m1.call_count, 1)
  172. self.assertEquals(m2.call_count, 0)
  173. self.assertEquals(m3.call_count, 1)
  174. class LruCacheSizedTestCase(unittest.TestCase):
  175. def test_evict(self):
  176. cache = LruCache(5, size_callback=len)
  177. cache["key1"] = [0]
  178. cache["key2"] = [1, 2]
  179. cache["key3"] = [3]
  180. cache["key4"] = [4]
  181. self.assertEquals(cache["key1"], [0])
  182. self.assertEquals(cache["key2"], [1, 2])
  183. self.assertEquals(cache["key3"], [3])
  184. self.assertEquals(cache["key4"], [4])
  185. self.assertEquals(len(cache), 5)
  186. cache["key5"] = [5, 6]
  187. self.assertEquals(len(cache), 4)
  188. self.assertEquals(cache.get("key1"), None)
  189. self.assertEquals(cache.get("key2"), None)
  190. self.assertEquals(cache["key3"], [3])
  191. self.assertEquals(cache["key4"], [4])
  192. self.assertEquals(cache["key5"], [5, 6])