test_treecache.py 2.9 KB

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