1
0

test_treecache.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 synapse.util.caches.treecache import TreeCache, iterate_tree_cache_entry
  15. from .. import unittest
  16. class TreeCacheTestCase(unittest.TestCase):
  17. def test_get_set_onelevel(self) -> None:
  18. cache = TreeCache()
  19. cache[("a",)] = "A"
  20. cache[("b",)] = "B"
  21. self.assertEqual(cache.get(("a",)), "A")
  22. self.assertEqual(cache.get(("b",)), "B")
  23. self.assertEqual(len(cache), 2)
  24. def test_pop_onelevel(self) -> None:
  25. cache = TreeCache()
  26. cache[("a",)] = "A"
  27. cache[("b",)] = "B"
  28. self.assertEqual(cache.pop(("a",)), "A")
  29. self.assertEqual(cache.pop(("a",)), None)
  30. self.assertEqual(cache.get(("b",)), "B")
  31. self.assertEqual(len(cache), 1)
  32. def test_get_set_twolevel(self) -> None:
  33. cache = TreeCache()
  34. cache[("a", "a")] = "AA"
  35. cache[("a", "b")] = "AB"
  36. cache[("b", "a")] = "BA"
  37. self.assertEqual(cache.get(("a", "a")), "AA")
  38. self.assertEqual(cache.get(("a", "b")), "AB")
  39. self.assertEqual(cache.get(("b", "a")), "BA")
  40. self.assertEqual(len(cache), 3)
  41. def test_pop_twolevel(self) -> None:
  42. cache = TreeCache()
  43. cache[("a", "a")] = "AA"
  44. cache[("a", "b")] = "AB"
  45. cache[("b", "a")] = "BA"
  46. self.assertEqual(cache.pop(("a", "a")), "AA")
  47. self.assertEqual(cache.get(("a", "a")), None)
  48. self.assertEqual(cache.get(("a", "b")), "AB")
  49. self.assertEqual(cache.pop(("b", "a")), "BA")
  50. self.assertEqual(cache.pop(("b", "a")), None)
  51. self.assertEqual(len(cache), 1)
  52. def test_pop_mixedlevel(self) -> None:
  53. cache = TreeCache()
  54. cache[("a", "a")] = "AA"
  55. cache[("a", "b")] = "AB"
  56. cache[("b", "a")] = "BA"
  57. self.assertEqual(cache.get(("a", "a")), "AA")
  58. popped = cache.pop(("a",))
  59. self.assertEqual(cache.get(("a", "a")), None)
  60. self.assertEqual(cache.get(("a", "b")), None)
  61. self.assertEqual(cache.get(("b", "a")), "BA")
  62. self.assertEqual(len(cache), 1)
  63. self.assertEqual({"AA", "AB"}, set(iterate_tree_cache_entry(popped)))
  64. def test_clear(self) -> None:
  65. cache = TreeCache()
  66. cache[("a",)] = "A"
  67. cache[("b",)] = "B"
  68. cache.clear()
  69. self.assertEqual(len(cache), 0)
  70. def test_contains(self) -> None:
  71. cache = TreeCache()
  72. cache[("a",)] = "A"
  73. self.assertTrue(("a",) in cache)
  74. self.assertFalse(("b",) in cache)