1
0

test_dict_cache.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 tests import unittest
  16. from synapse.util.caches.dictionary_cache import DictionaryCache
  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, {}), v)
  24. seq = self.cache.sequence
  25. test_value = {"test": "test_simple_cache_hit_full"}
  26. self.cache.update(seq, key, test_value, full=True)
  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 = {
  33. "test": "test_simple_cache_hit_partial"
  34. }
  35. self.cache.update(seq, key, test_value, full=True)
  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 = {
  42. "test": "test_simple_cache_miss_partial"
  43. }
  44. self.cache.update(seq, key, test_value, full=True)
  45. c = self.cache.get(key, ["test2"])
  46. self.assertEqual({}, c.value)
  47. def test_simple_cache_hit_miss_partial(self):
  48. key = "test_simple_cache_hit_miss_partial"
  49. seq = self.cache.sequence
  50. test_value = {
  51. "test": "test_simple_cache_hit_miss_partial",
  52. "test2": "test_simple_cache_hit_miss_partial2",
  53. "test3": "test_simple_cache_hit_miss_partial3",
  54. }
  55. self.cache.update(seq, key, test_value, full=True)
  56. c = self.cache.get(key, ["test2"])
  57. self.assertEqual({"test2": "test_simple_cache_hit_miss_partial2"}, c.value)
  58. def test_multi_insert(self):
  59. key = "test_simple_cache_hit_miss_partial"
  60. seq = self.cache.sequence
  61. test_value_1 = {
  62. "test": "test_simple_cache_hit_miss_partial",
  63. }
  64. self.cache.update(seq, key, test_value_1, full=False)
  65. seq = self.cache.sequence
  66. test_value_2 = {
  67. "test2": "test_simple_cache_hit_miss_partial2",
  68. }
  69. self.cache.update(seq, key, test_value_2, full=False)
  70. c = self.cache.get(key)
  71. self.assertEqual(
  72. {
  73. "test": "test_simple_cache_hit_miss_partial",
  74. "test2": "test_simple_cache_hit_miss_partial2",
  75. },
  76. c.value
  77. )