test_dict_cache.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 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 twisted.internet import defer
  16. from tests import unittest
  17. from synapse.util.caches.dictionary_cache import DictionaryCache
  18. class DictCacheTestCase(unittest.TestCase):
  19. def setUp(self):
  20. self.cache = DictionaryCache("foobar")
  21. def test_simple_cache_hit_full(self):
  22. key = "test_simple_cache_hit_full"
  23. v = self.cache.get(key)
  24. self.assertEqual((False, {}), v)
  25. seq = self.cache.sequence
  26. test_value = {"test": "test_simple_cache_hit_full"}
  27. self.cache.update(seq, key, test_value, full=True)
  28. c = self.cache.get(key)
  29. self.assertEqual(test_value, c.value)
  30. def test_simple_cache_hit_partial(self):
  31. key = "test_simple_cache_hit_partial"
  32. seq = self.cache.sequence
  33. test_value = {
  34. "test": "test_simple_cache_hit_partial"
  35. }
  36. self.cache.update(seq, key, test_value, full=True)
  37. c = self.cache.get(key, ["test"])
  38. self.assertEqual(test_value, c.value)
  39. def test_simple_cache_miss_partial(self):
  40. key = "test_simple_cache_miss_partial"
  41. seq = self.cache.sequence
  42. test_value = {
  43. "test": "test_simple_cache_miss_partial"
  44. }
  45. self.cache.update(seq, key, test_value, full=True)
  46. c = self.cache.get(key, ["test2"])
  47. self.assertEqual({}, c.value)
  48. def test_simple_cache_hit_miss_partial(self):
  49. key = "test_simple_cache_hit_miss_partial"
  50. seq = self.cache.sequence
  51. test_value = {
  52. "test": "test_simple_cache_hit_miss_partial",
  53. "test2": "test_simple_cache_hit_miss_partial2",
  54. "test3": "test_simple_cache_hit_miss_partial3",
  55. }
  56. self.cache.update(seq, key, test_value, full=True)
  57. c = self.cache.get(key, ["test2"])
  58. self.assertEqual({"test2": "test_simple_cache_hit_miss_partial2"}, c.value)
  59. def test_multi_insert(self):
  60. key = "test_simple_cache_hit_miss_partial"
  61. seq = self.cache.sequence
  62. test_value_1 = {
  63. "test": "test_simple_cache_hit_miss_partial",
  64. }
  65. self.cache.update(seq, key, test_value_1, full=False)
  66. seq = self.cache.sequence
  67. test_value_2 = {
  68. "test2": "test_simple_cache_hit_miss_partial2",
  69. }
  70. self.cache.update(seq, key, test_value_2, full=False)
  71. c = self.cache.get(key)
  72. self.assertEqual(
  73. {
  74. "test": "test_simple_cache_hit_miss_partial",
  75. "test2": "test_simple_cache_hit_miss_partial2",
  76. },
  77. c.value
  78. )