test_cache.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 Matrix.org Foundation C.I.C.
  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 synapse.config._base import Config, RootConfig
  16. from synapse.config.cache import CacheConfig, add_resizable_cache
  17. from synapse.util.caches.lrucache import LruCache
  18. from tests.unittest import TestCase
  19. class FakeServer(Config):
  20. section = "server"
  21. class TestConfig(RootConfig):
  22. config_classes = [FakeServer, CacheConfig]
  23. class CacheConfigTests(TestCase):
  24. def setUp(self):
  25. CacheConfig._reset()
  26. def test_individual_caches_from_environ(self):
  27. """
  28. Individual cache factors will be loaded from the environment.
  29. """
  30. config = {}
  31. t = TestConfig()
  32. t.caches._environ = {
  33. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  34. "SYNAPSE_NOT_CACHE": "BLAH",
  35. }
  36. t.read_config(config, config_dir_path="", data_dir_path="")
  37. self.assertEqual(dict(t.caches.cache_factors), {"something_or_other": 2.0})
  38. def test_config_overrides_environ(self):
  39. """
  40. Individual cache factors defined in config will take precedence over
  41. ones in the environment.
  42. """
  43. config = {"caches": {"per_cache_factors": {"foo": 2, "bar": 3}}}
  44. t = TestConfig()
  45. t.caches._environ = {
  46. "SYNAPSE_CACHE_FACTOR_SOMETHING_OR_OTHER": "2",
  47. "SYNAPSE_CACHE_FACTOR_FOO": 1,
  48. }
  49. t.read_config(config, config_dir_path="", data_dir_path="")
  50. self.assertEqual(
  51. dict(t.caches.cache_factors),
  52. {"foo": 2.0, "bar": 3.0, "something_or_other": 2.0},
  53. )
  54. def test_individual_instantiated_before_config_load(self):
  55. """
  56. If a cache is instantiated before the config is read, it will be given
  57. the default cache size in the interim, and then resized once the config
  58. is loaded.
  59. """
  60. cache = LruCache(100)
  61. add_resizable_cache("foo", cache.set_cache_factor)
  62. self.assertEqual(cache.max_size, 50)
  63. config = {"caches": {"per_cache_factors": {"foo": 3}}}
  64. t = TestConfig()
  65. t.read_config(config, config_dir_path="", data_dir_path="")
  66. self.assertEqual(cache.max_size, 300)
  67. def test_individual_instantiated_after_config_load(self):
  68. """
  69. If a cache is instantiated after the config is read, it will be
  70. immediately resized to the correct size given the per_cache_factor if
  71. there is one.
  72. """
  73. config = {"caches": {"per_cache_factors": {"foo": 2}}}
  74. t = TestConfig()
  75. t.read_config(config, config_dir_path="", data_dir_path="")
  76. cache = LruCache(100)
  77. add_resizable_cache("foo", cache.set_cache_factor)
  78. self.assertEqual(cache.max_size, 200)
  79. def test_global_instantiated_before_config_load(self):
  80. """
  81. If a cache is instantiated before the config is read, it will be given
  82. the default cache size in the interim, and then resized to the new
  83. default cache size once the config is loaded.
  84. """
  85. cache = LruCache(100)
  86. add_resizable_cache("foo", cache.set_cache_factor)
  87. self.assertEqual(cache.max_size, 50)
  88. config = {"caches": {"global_factor": 4}}
  89. t = TestConfig()
  90. t.read_config(config, config_dir_path="", data_dir_path="")
  91. self.assertEqual(cache.max_size, 400)
  92. def test_global_instantiated_after_config_load(self):
  93. """
  94. If a cache is instantiated after the config is read, it will be
  95. immediately resized to the correct size given the global factor if there
  96. is no per-cache factor.
  97. """
  98. config = {"caches": {"global_factor": 1.5}}
  99. t = TestConfig()
  100. t.read_config(config, config_dir_path="", data_dir_path="")
  101. cache = LruCache(100)
  102. add_resizable_cache("foo", cache.set_cache_factor)
  103. self.assertEqual(cache.max_size, 150)