test_treecache.py 2.9 KB

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