test_cache.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright 2020 Matrix.org Foundation C.I.C.
  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.config.cache import CacheConfig, add_resizable_cache
  15. from synapse.util.caches.lrucache import LruCache
  16. from tests.unittest import TestCase
  17. class CacheConfigTests(TestCase):
  18. def setUp(self):
  19. # Reset caches before each test since there's global state involved.
  20. self.config = CacheConfig()
  21. self.config.reset()
  22. def tearDown(self):
  23. # Also reset the caches after each test to leave state pristine.
  24. self.config.reset()
  25. def test_individual_caches_from_environ(self):
  26. """
  27. Individual cache factors will be loaded from the environment.
  28. """
  29. config = {}
  30. self.config._environ = {
  31. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  32. "SYNAPSE_NOT_CACHE": "BLAH",
  33. }
  34. self.config.read_config(config, config_dir_path="", data_dir_path="")
  35. self.assertEqual(dict(self.config.cache_factors), {"something_or_other": 2.0})
  36. def test_config_overrides_environ(self):
  37. """
  38. Individual cache factors defined in the environment will take precedence
  39. over those in the config.
  40. """
  41. config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
  42. self.config._environ = {
  43. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  44. "SYNAPSE_CACHE_FACTOR_FOO": 1,
  45. }
  46. self.config.read_config(config, config_dir_path="", data_dir_path="")
  47. self.assertEqual(
  48. dict(self.config.cache_factors),
  49. {"foo": 1.0, "bar": 3.0, "something_or_other": 2.0},
  50. )
  51. def test_individual_instantiated_before_config_load(self):
  52. """
  53. If a cache is instantiated before the config is read, it will be given
  54. the default cache size in the interim, and then resized once the config
  55. is loaded.
  56. """
  57. cache = LruCache(100)
  58. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  59. self.assertEqual(cache.max_size, 50)
  60. config = {"caches": {"per_cache_factors": {"foo": 3}}}
  61. self.config.read_config(config)
  62. self.assertEqual(cache.max_size, 300)
  63. def test_individual_instantiated_after_config_load(self):
  64. """
  65. If a cache is instantiated after the config is read, it will be
  66. immediately resized to the correct size given the per_cache_factor if
  67. there is one.
  68. """
  69. config = {"caches": {"per_cache_factors": {"foo": 2}}}
  70. self.config.read_config(config, config_dir_path="", data_dir_path="")
  71. cache = LruCache(100)
  72. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  73. self.assertEqual(cache.max_size, 200)
  74. def test_global_instantiated_before_config_load(self):
  75. """
  76. If a cache is instantiated before the config is read, it will be given
  77. the default cache size in the interim, and then resized to the new
  78. default cache size once the config is loaded.
  79. """
  80. cache = LruCache(100)
  81. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  82. self.assertEqual(cache.max_size, 50)
  83. config = {"caches": {"global_factor": 4}}
  84. self.config.read_config(config, config_dir_path="", data_dir_path="")
  85. self.assertEqual(cache.max_size, 400)
  86. def test_global_instantiated_after_config_load(self):
  87. """
  88. If a cache is instantiated after the config is read, it will be
  89. immediately resized to the correct size given the global factor if there
  90. is no per-cache factor.
  91. """
  92. config = {"caches": {"global_factor": 1.5}}
  93. self.config.read_config(config, config_dir_path="", data_dir_path="")
  94. cache = LruCache(100)
  95. add_resizable_cache("foo", cache_resize_callback=cache.set_cache_factor)
  96. self.assertEqual(cache.max_size, 150)
  97. def test_cache_with_asterisk_in_name(self):
  98. """Some caches have asterisks in their name, test that they are set correctly."""
  99. config = {
  100. "caches": {
  101. "per_cache_factors": {"*cache_a*": 5, "cache_b": 6, "cache_c": 2}
  102. }
  103. }
  104. self.config._environ = {
  105. "SYNAPSE_CACHE_FACTOR_CACHE_A": "2",
  106. "SYNAPSE_CACHE_FACTOR_CACHE_B": 3,
  107. }
  108. self.config.read_config(config, config_dir_path="", data_dir_path="")
  109. cache_a = LruCache(100)
  110. add_resizable_cache("*cache_a*", cache_resize_callback=cache_a.set_cache_factor)
  111. self.assertEqual(cache_a.max_size, 200)
  112. cache_b = LruCache(100)
  113. add_resizable_cache("*Cache_b*", cache_resize_callback=cache_b.set_cache_factor)
  114. self.assertEqual(cache_b.max_size, 300)
  115. cache_c = LruCache(100)
  116. add_resizable_cache("*cache_c*", cache_resize_callback=cache_c.set_cache_factor)
  117. self.assertEqual(cache_c.max_size, 200)
  118. def test_apply_cache_factor_from_config(self):
  119. """Caches can disable applying cache factor updates, mainly used by
  120. event cache size.
  121. """
  122. config = {"caches": {"event_cache_size": "10k"}}
  123. self.config.read_config(config, config_dir_path="", data_dir_path="")
  124. cache = LruCache(
  125. max_size=self.config.event_cache_size,
  126. apply_cache_factor_from_config=False,
  127. )
  128. add_resizable_cache("event_cache", cache_resize_callback=cache.set_cache_factor)
  129. self.assertEqual(cache.max_size, 10240)