test_ratelimiting.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2019 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. from synapse.config.homeserver import HomeServerConfig
  15. from synapse.config.ratelimiting import RatelimitSettings
  16. from tests.unittest import TestCase
  17. from tests.utils import default_config
  18. class ParseRatelimitSettingsTestcase(TestCase):
  19. def test_depth_1(self) -> None:
  20. cfg = {
  21. "a": {
  22. "per_second": 5,
  23. "burst_count": 10,
  24. }
  25. }
  26. parsed = RatelimitSettings.parse(cfg, "a")
  27. self.assertEqual(parsed, RatelimitSettings("a", 5, 10))
  28. def test_depth_2(self) -> None:
  29. cfg = {
  30. "a": {
  31. "b": {
  32. "per_second": 5,
  33. "burst_count": 10,
  34. },
  35. }
  36. }
  37. parsed = RatelimitSettings.parse(cfg, "a.b")
  38. self.assertEqual(parsed, RatelimitSettings("a.b", 5, 10))
  39. def test_missing(self) -> None:
  40. parsed = RatelimitSettings.parse(
  41. {}, "a", defaults={"per_second": 5, "burst_count": 10}
  42. )
  43. self.assertEqual(parsed, RatelimitSettings("a", 5, 10))
  44. class RatelimitConfigTestCase(TestCase):
  45. def test_parse_rc_federation(self) -> None:
  46. config_dict = default_config("test")
  47. config_dict["rc_federation"] = {
  48. "window_size": 20000,
  49. "sleep_limit": 693,
  50. "sleep_delay": 252,
  51. "reject_limit": 198,
  52. "concurrent": 7,
  53. }
  54. config = HomeServerConfig()
  55. config.parse_config_dict(config_dict, "", "")
  56. config_obj = config.ratelimiting.rc_federation
  57. self.assertEqual(config_obj.window_size, 20000)
  58. self.assertEqual(config_obj.sleep_limit, 693)
  59. self.assertEqual(config_obj.sleep_delay, 252)
  60. self.assertEqual(config_obj.reject_limit, 198)
  61. self.assertEqual(config_obj.concurrent, 7)