test_background_update.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2022 The 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. import yaml
  15. from synapse.storage.background_updates import BackgroundUpdater
  16. from tests.unittest import HomeserverTestCase, override_config
  17. class BackgroundUpdateConfigTestCase(HomeserverTestCase):
  18. # Tests that the default values in the config are correctly loaded. Note that the default
  19. # values are loaded when the corresponding config options are commented out, which is why there isn't
  20. # a config specified here.
  21. def test_default_configuration(self) -> None:
  22. background_updater = BackgroundUpdater(
  23. self.hs, self.hs.get_datastores().main.db_pool
  24. )
  25. self.assertEqual(background_updater.minimum_background_batch_size, 1)
  26. self.assertEqual(background_updater.default_background_batch_size, 100)
  27. self.assertEqual(background_updater.sleep_enabled, True)
  28. self.assertEqual(background_updater.sleep_duration_ms, 1000)
  29. self.assertEqual(background_updater.update_duration_ms, 100)
  30. # Tests that non-default values for the config options are properly picked up and passed on.
  31. @override_config(
  32. yaml.safe_load(
  33. """
  34. background_updates:
  35. background_update_duration_ms: 1000
  36. sleep_enabled: false
  37. sleep_duration_ms: 600
  38. min_batch_size: 5
  39. default_batch_size: 50
  40. """
  41. )
  42. )
  43. def test_custom_configuration(self) -> None:
  44. background_updater = BackgroundUpdater(
  45. self.hs, self.hs.get_datastores().main.db_pool
  46. )
  47. self.assertEqual(background_updater.minimum_background_batch_size, 5)
  48. self.assertEqual(background_updater.default_background_batch_size, 50)
  49. self.assertEqual(background_updater.sleep_enabled, False)
  50. self.assertEqual(background_updater.sleep_duration_ms, 600)
  51. self.assertEqual(background_updater.update_duration_ms, 1000)