1
0

test_dict_cache.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.util.caches.dictionary_cache import DictionaryCache
  15. from tests import unittest
  16. class DictCacheTestCase(unittest.TestCase):
  17. def setUp(self):
  18. self.cache = DictionaryCache("foobar")
  19. def test_simple_cache_hit_full(self):
  20. key = "test_simple_cache_hit_full"
  21. v = self.cache.get(key)
  22. self.assertIs(v.full, False)
  23. self.assertEqual(v.known_absent, set())
  24. self.assertEqual({}, v.value)
  25. seq = self.cache.sequence
  26. test_value = {"test": "test_simple_cache_hit_full"}
  27. self.cache.update(seq, key, test_value)
  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 = {"test": "test_simple_cache_hit_partial"}
  34. self.cache.update(seq, key, test_value)
  35. c = self.cache.get(key, ["test"])
  36. self.assertEqual(test_value, c.value)
  37. def test_simple_cache_miss_partial(self):
  38. key = "test_simple_cache_miss_partial"
  39. seq = self.cache.sequence
  40. test_value = {"test": "test_simple_cache_miss_partial"}
  41. self.cache.update(seq, key, test_value)
  42. c = self.cache.get(key, ["test2"])
  43. self.assertEqual({}, c.value)
  44. def test_simple_cache_hit_miss_partial(self):
  45. key = "test_simple_cache_hit_miss_partial"
  46. seq = self.cache.sequence
  47. test_value = {
  48. "test": "test_simple_cache_hit_miss_partial",
  49. "test2": "test_simple_cache_hit_miss_partial2",
  50. "test3": "test_simple_cache_hit_miss_partial3",
  51. }
  52. self.cache.update(seq, key, test_value)
  53. c = self.cache.get(key, ["test2"])
  54. self.assertEqual({"test2": "test_simple_cache_hit_miss_partial2"}, c.value)
  55. def test_multi_insert(self):
  56. key = "test_simple_cache_hit_miss_partial"
  57. seq = self.cache.sequence
  58. test_value_1 = {"test": "test_simple_cache_hit_miss_partial"}
  59. self.cache.update(seq, key, test_value_1, fetched_keys=set("test"))
  60. seq = self.cache.sequence
  61. test_value_2 = {"test2": "test_simple_cache_hit_miss_partial2"}
  62. self.cache.update(seq, key, test_value_2, fetched_keys=set("test2"))
  63. c = self.cache.get(key)
  64. self.assertEqual(
  65. {
  66. "test": "test_simple_cache_hit_miss_partial",
  67. "test2": "test_simple_cache_hit_miss_partial2",
  68. },
  69. c.value,
  70. )