test_retryutils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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):
  23. """A happy-path case with a new destination and a successful operation"""
  24. store = self.hs.get_datastore()
  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):
  33. """General test case which walks through the process of a failing request"""
  34. store = self.hs.get_datastore()
  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. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  45. self.assertEqual(new_timings["failure_ts"], failure_ts)
  46. self.assertEqual(new_timings["retry_last_ts"], failure_ts)
  47. self.assertEqual(new_timings["retry_interval"], MIN_RETRY_INTERVAL)
  48. # now if we try again we should get a failure
  49. self.get_failure(
  50. get_retry_limiter("test_dest", self.clock, store), NotRetryingDestination
  51. )
  52. #
  53. # advance the clock and try again
  54. #
  55. self.pump(MIN_RETRY_INTERVAL)
  56. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  57. self.pump(1)
  58. try:
  59. with limiter:
  60. self.pump(1)
  61. retry_ts = self.clock.time_msec()
  62. raise AssertionError("argh")
  63. except AssertionError:
  64. pass
  65. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  66. self.assertEqual(new_timings["failure_ts"], failure_ts)
  67. self.assertEqual(new_timings["retry_last_ts"], retry_ts)
  68. self.assertGreaterEqual(
  69. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 0.5
  70. )
  71. self.assertLessEqual(
  72. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0
  73. )
  74. #
  75. # one more go, with success
  76. #
  77. self.reactor.advance(MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0)
  78. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  79. self.pump(1)
  80. with limiter:
  81. self.pump(1)
  82. # wait for the update to land
  83. self.pump()
  84. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  85. self.assertIsNone(new_timings)