You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
3.7 KiB

  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.util.retryutils import (
  15. MIN_RETRY_INTERVAL,
  16. RETRY_MULTIPLIER,
  17. NotRetryingDestination,
  18. get_retry_limiter,
  19. )
  20. from tests.unittest import HomeserverTestCase
  21. class RetryLimiterTestCase(HomeserverTestCase):
  22. def test_new_destination(self) -> None:
  23. """A happy-path case with a new destination and a successful operation"""
  24. store = self.hs.get_datastores().main
  25. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  26. # advance the clock a bit before making the request
  27. self.pump(1)
  28. with limiter:
  29. pass
  30. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  31. self.assertIsNone(new_timings)
  32. def test_limiter(self) -> None:
  33. """General test case which walks through the process of a failing request"""
  34. store = self.hs.get_datastores().main
  35. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  36. self.pump(1)
  37. try:
  38. with limiter:
  39. self.pump(1)
  40. failure_ts = self.clock.time_msec()
  41. raise AssertionError("argh")
  42. except AssertionError:
  43. pass
  44. self.pump()
  45. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  46. assert new_timings is not None
  47. self.assertEqual(new_timings.failure_ts, failure_ts)
  48. self.assertEqual(new_timings.retry_last_ts, failure_ts)
  49. self.assertEqual(new_timings.retry_interval, MIN_RETRY_INTERVAL)
  50. # now if we try again we should get a failure
  51. self.get_failure(
  52. get_retry_limiter("test_dest", self.clock, store), NotRetryingDestination
  53. )
  54. #
  55. # advance the clock and try again
  56. #
  57. self.pump(MIN_RETRY_INTERVAL)
  58. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  59. self.pump(1)
  60. try:
  61. with limiter:
  62. self.pump(1)
  63. retry_ts = self.clock.time_msec()
  64. raise AssertionError("argh")
  65. except AssertionError:
  66. pass
  67. self.pump()
  68. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  69. assert new_timings is not None
  70. self.assertEqual(new_timings.failure_ts, failure_ts)
  71. self.assertEqual(new_timings.retry_last_ts, retry_ts)
  72. self.assertGreaterEqual(
  73. new_timings.retry_interval, MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 0.5
  74. )
  75. self.assertLessEqual(
  76. new_timings.retry_interval, MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0
  77. )
  78. #
  79. # one more go, with success
  80. #
  81. self.reactor.advance(MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0)
  82. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  83. self.pump(1)
  84. with limiter:
  85. self.pump(1)
  86. # wait for the update to land
  87. self.pump()
  88. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  89. self.assertIsNone(new_timings)