1
0

test_ratelimitutils.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The 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.homeserver import HomeServerConfig
  16. from synapse.util.ratelimitutils import FederationRateLimiter
  17. from tests.server import get_clock
  18. from tests.unittest import TestCase
  19. from tests.utils import default_config
  20. class FederationRateLimiterTestCase(TestCase):
  21. def test_ratelimit(self):
  22. """A simple test with the default values"""
  23. reactor, clock = get_clock()
  24. rc_config = build_rc_config()
  25. ratelimiter = FederationRateLimiter(clock, rc_config)
  26. with ratelimiter.ratelimit("testhost") as d1:
  27. # shouldn't block
  28. self.successResultOf(d1)
  29. def test_concurrent_limit(self):
  30. """Test what happens when we hit the concurrent limit"""
  31. reactor, clock = get_clock()
  32. rc_config = build_rc_config({"rc_federation": {"concurrent": 2}})
  33. ratelimiter = FederationRateLimiter(clock, rc_config)
  34. with ratelimiter.ratelimit("testhost") as d1:
  35. # shouldn't block
  36. self.successResultOf(d1)
  37. cm2 = ratelimiter.ratelimit("testhost")
  38. d2 = cm2.__enter__()
  39. # also shouldn't block
  40. self.successResultOf(d2)
  41. cm3 = ratelimiter.ratelimit("testhost")
  42. d3 = cm3.__enter__()
  43. # this one should block, though ...
  44. self.assertNoResult(d3)
  45. # ... until we complete an earlier request
  46. cm2.__exit__(None, None, None)
  47. self.successResultOf(d3)
  48. def test_sleep_limit(self):
  49. """Test what happens when we hit the sleep limit"""
  50. reactor, clock = get_clock()
  51. rc_config = build_rc_config(
  52. {"rc_federation": {"sleep_limit": 2, "sleep_delay": 500}}
  53. )
  54. ratelimiter = FederationRateLimiter(clock, rc_config)
  55. with ratelimiter.ratelimit("testhost") as d1:
  56. # shouldn't block
  57. self.successResultOf(d1)
  58. with ratelimiter.ratelimit("testhost") as d2:
  59. # nor this
  60. self.successResultOf(d2)
  61. with ratelimiter.ratelimit("testhost") as d3:
  62. # this one should block, though ...
  63. self.assertNoResult(d3)
  64. sleep_time = _await_resolution(reactor, d3)
  65. self.assertAlmostEqual(sleep_time, 500, places=3)
  66. def _await_resolution(reactor, d):
  67. """advance the clock until the deferred completes.
  68. Returns the number of milliseconds it took to complete.
  69. """
  70. start_time = reactor.seconds()
  71. while not d.called:
  72. reactor.advance(0.01)
  73. return (reactor.seconds() - start_time) * 1000
  74. def build_rc_config(settings={}):
  75. config_dict = default_config("test")
  76. config_dict.update(settings)
  77. config = HomeServerConfig()
  78. config.parse_config_dict(config_dict, "", "")
  79. return config.rc_federation