test_dict_cache.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.dictionary_cache import DictionaryCache
  16. from tests import unittest
  17. class DictCacheTestCase(unittest.TestCase):
  18. def setUp(self):
  19. self.cache = DictionaryCache("foobar")
  20. def test_simple_cache_hit_full(self):
  21. key = "test_simple_cache_hit_full"
  22. v = self.cache.get(key)
  23. self.assertIs(v.full, False)
  24. self.assertEqual(v.known_absent, set())
  25. self.assertEqual({}, v.value)
  26. seq = self.cache.sequence
  27. test_value = {"test": "test_simple_cache_hit_full"}
  28. self.cache.update(seq, key, test_value)
  29. c = self.cache.get(key)
  30. self.assertEqual(test_value, c.value)
  31. def test_simple_cache_hit_partial(self):
  32. key = "test_simple_cache_hit_partial"
  33. seq = self.cache.sequence
  34. test_value = {"test": "test_simple_cache_hit_partial"}
  35. self.cache.update(seq, key, test_value)
  36. c = self.cache.get(key, ["test"])
  37. self.assertEqual(test_value, c.value)
  38. def test_simple_cache_miss_partial(self):
  39. key = "test_simple_cache_miss_partial"
  40. seq = self.cache.sequence
  41. test_value = {"test": "test_simple_cache_miss_partial"}
  42. self.cache.update(seq, key, test_value)
  43. c = self.cache.get(key, ["test2"])
  44. self.assertEqual({}, c.value)
  45. def test_simple_cache_hit_miss_partial(self):
  46. key = "test_simple_cache_hit_miss_partial"
  47. seq = self.cache.sequence
  48. test_value = {
  49. "test": "test_simple_cache_hit_miss_partial",
  50. "test2": "test_simple_cache_hit_miss_partial2",
  51. "test3": "test_simple_cache_hit_miss_partial3",
  52. }
  53. self.cache.update(seq, key, test_value)
  54. c = self.cache.get(key, ["test2"])
  55. self.assertEqual({"test2": "test_simple_cache_hit_miss_partial2"}, c.value)
  56. def test_multi_insert(self):
  57. key = "test_simple_cache_hit_miss_partial"
  58. seq = self.cache.sequence
  59. test_value_1 = {"test": "test_simple_cache_hit_miss_partial"}
  60. self.cache.update(seq, key, test_value_1, fetched_keys=set("test"))
  61. seq = self.cache.sequence
  62. test_value_2 = {"test2": "test_simple_cache_hit_miss_partial2"}
  63. self.cache.update(seq, key, test_value_2, fetched_keys=set("test2"))
  64. c = self.cache.get(key)
  65. self.assertEqual(
  66. {
  67. "test": "test_simple_cache_hit_miss_partial",
  68. "test2": "test_simple_cache_hit_miss_partial2",
  69. },
  70. c.value,
  71. )