test_retryutils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_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):
  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. self.assertEqual(new_timings.failure_ts, failure_ts)
  47. self.assertEqual(new_timings.retry_last_ts, failure_ts)
  48. self.assertEqual(new_timings.retry_interval, MIN_RETRY_INTERVAL)
  49. # now if we try again we should get a failure
  50. self.get_failure(
  51. get_retry_limiter("test_dest", self.clock, store), NotRetryingDestination
  52. )
  53. #
  54. # advance the clock and try again
  55. #
  56. self.pump(MIN_RETRY_INTERVAL)
  57. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  58. self.pump(1)
  59. try:
  60. with limiter:
  61. self.pump(1)
  62. retry_ts = self.clock.time_msec()
  63. raise AssertionError("argh")
  64. except AssertionError:
  65. pass
  66. self.pump()
  67. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  68. self.assertEqual(new_timings.failure_ts, failure_ts)
  69. self.assertEqual(new_timings.retry_last_ts, retry_ts)
  70. self.assertGreaterEqual(
  71. new_timings.retry_interval, MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 0.5
  72. )
  73. self.assertLessEqual(
  74. new_timings.retry_interval, MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0
  75. )
  76. #
  77. # one more go, with success
  78. #
  79. self.reactor.advance(MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0)
  80. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  81. self.pump(1)
  82. with limiter:
  83. self.pump(1)
  84. # wait for the update to land
  85. self.pump()
  86. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  87. self.assertIsNone(new_timings)