test_dict_cache.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.assertEqual((False, set(), {}), v)
  24. seq = self.cache.sequence
  25. test_value = {"test": "test_simple_cache_hit_full"}
  26. self.cache.update(seq, key, test_value)
  27. c = self.cache.get(key)
  28. self.assertEqual(test_value, c.value)
  29. def test_simple_cache_hit_partial(self):
  30. key = "test_simple_cache_hit_partial"
  31. seq = self.cache.sequence
  32. test_value = {"test": "test_simple_cache_hit_partial"}
  33. self.cache.update(seq, key, test_value)
  34. c = self.cache.get(key, ["test"])
  35. self.assertEqual(test_value, c.value)
  36. def test_simple_cache_miss_partial(self):
  37. key = "test_simple_cache_miss_partial"
  38. seq = self.cache.sequence
  39. test_value = {"test": "test_simple_cache_miss_partial"}
  40. self.cache.update(seq, key, test_value)
  41. c = self.cache.get(key, ["test2"])
  42. self.assertEqual({}, c.value)
  43. def test_simple_cache_hit_miss_partial(self):
  44. key = "test_simple_cache_hit_miss_partial"
  45. seq = self.cache.sequence
  46. test_value = {
  47. "test": "test_simple_cache_hit_miss_partial",
  48. "test2": "test_simple_cache_hit_miss_partial2",
  49. "test3": "test_simple_cache_hit_miss_partial3",
  50. }
  51. self.cache.update(seq, key, test_value)
  52. c = self.cache.get(key, ["test2"])
  53. self.assertEqual({"test2": "test_simple_cache_hit_miss_partial2"}, c.value)
  54. def test_multi_insert(self):
  55. key = "test_simple_cache_hit_miss_partial"
  56. seq = self.cache.sequence
  57. test_value_1 = {"test": "test_simple_cache_hit_miss_partial"}
  58. self.cache.update(seq, key, test_value_1, fetched_keys=set("test"))
  59. seq = self.cache.sequence
  60. test_value_2 = {"test2": "test_simple_cache_hit_miss_partial2"}
  61. self.cache.update(seq, key, test_value_2, fetched_keys=set("test2"))
  62. c = self.cache.get(key)
  63. self.assertEqual(
  64. {
  65. "test": "test_simple_cache_hit_miss_partial",
  66. "test2": "test_simple_cache_hit_miss_partial2",
  67. },
  68. c.value,
  69. )