test_ratelimiter.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from twisted.test.proto_helpers import MemoryReactorClock
  15. from twisted.trial import unittest
  16. from sydent.util.ratelimiter import LimitExceededException, Ratelimiter
  17. class RatelimiterTest(unittest.TestCase):
  18. def setUp(self) -> None:
  19. self.clock = MemoryReactorClock()
  20. self.ratelimiter = Ratelimiter(self.clock, burst=5, rate_hz=0.5)
  21. def test_simple(self) -> None:
  22. """Test that a request doesn't get ratelimited to start off with"""
  23. key = "key"
  24. # This should not raise as we're below the ratelimit
  25. self.ratelimiter.ratelimit(key)
  26. def test_burst(self) -> None:
  27. """Test that we can send `burst` number of messages before getting
  28. ratelimited
  29. """
  30. key = "key"
  31. # This should not raise as we're below the ratelimit
  32. for _ in range(5):
  33. self.ratelimiter.ratelimit(key)
  34. with self.assertRaises(LimitExceededException):
  35. self.ratelimiter.ratelimit(key)
  36. def test_burst_reset(self) -> None:
  37. """Test that once we hit the ratelimit we can wait a while and we'll be
  38. able to send requests again
  39. """
  40. key = "key"
  41. # This should not raise as we're below the ratelimit
  42. for _ in range(5):
  43. self.ratelimiter.ratelimit(key)
  44. with self.assertRaises(LimitExceededException):
  45. self.ratelimiter.ratelimit(key)
  46. self.clock.pump([2.0] * 5)
  47. for _ in range(5):
  48. self.ratelimiter.ratelimit(key)
  49. with self.assertRaises(LimitExceededException):
  50. self.ratelimiter.ratelimit(key)
  51. def test_average_rate(self):
  52. """Test that sending requests at a rate higher than the maximum rate
  53. gets ratelimited.
  54. """
  55. key = "key"
  56. with self.assertRaises(LimitExceededException):
  57. for _ in range(100):
  58. self.clock.advance(1)
  59. self.ratelimiter.ratelimit(key)
  60. def test_average_rate_burst(self):
  61. """Test that if we go above the maximum rate we'll get ratelimited"""
  62. key = "key"
  63. for _ in range(5):
  64. self.ratelimiter.ratelimit(key)
  65. for _ in range(100):
  66. self.clock.advance(2)
  67. self.ratelimiter.ratelimit(key)
  68. with self.assertRaises(LimitExceededException):
  69. self.ratelimiter.ratelimit(key)