test_dict_cache.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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", max_entries=10)
  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={"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={"test2"})
  63. c = self.cache.get(key, dict_keys=["test", "test2"])
  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. )
  71. self.assertEqual(c.full, False)
  72. def test_invalidation(self):
  73. """Test that the partial dict and full dicts get invalidated
  74. separately.
  75. """
  76. key = "some_key"
  77. seq = self.cache.sequence
  78. # start by populating a "full dict" entry
  79. self.cache.update(seq, key, {"a": "b", "c": "d"})
  80. # add a bunch of individual entries, also keeping the individual
  81. # entry for "a" warm.
  82. for i in range(20):
  83. self.cache.get(key, ["a"])
  84. self.cache.update(seq, f"key{i}", {1: 2})
  85. # We should have evicted the full dict...
  86. r = self.cache.get(key)
  87. self.assertFalse(r.full)
  88. self.assertTrue("c" not in r.value)
  89. # ... but kept the "a" entry that we kept querying.
  90. r = self.cache.get(key, dict_keys=["a"])
  91. self.assertFalse(r.full)
  92. self.assertEqual(r.value, {"a": "b"})