1
0

test_retryutils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.util.retryutils import (
  16. MIN_RETRY_INTERVAL,
  17. RETRY_MULTIPLIER,
  18. NotRetryingDestination,
  19. get_retry_limiter,
  20. )
  21. from tests.unittest import HomeserverTestCase
  22. class RetryLimiterTestCase(HomeserverTestCase):
  23. def test_new_destination(self):
  24. """A happy-path case with a new destination and a successful operation"""
  25. store = self.hs.get_datastore()
  26. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  27. # advance the clock a bit before making the request
  28. self.pump(1)
  29. with limiter:
  30. pass
  31. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  32. self.assertIsNone(new_timings)
  33. def test_limiter(self):
  34. """General test case which walks through the process of a failing request"""
  35. store = self.hs.get_datastore()
  36. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  37. self.pump(1)
  38. try:
  39. with limiter:
  40. self.pump(1)
  41. failure_ts = self.clock.time_msec()
  42. raise AssertionError("argh")
  43. except AssertionError:
  44. pass
  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. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  67. self.assertEqual(new_timings["failure_ts"], failure_ts)
  68. self.assertEqual(new_timings["retry_last_ts"], retry_ts)
  69. self.assertGreaterEqual(
  70. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 0.5
  71. )
  72. self.assertLessEqual(
  73. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0
  74. )
  75. #
  76. # one more go, with success
  77. #
  78. self.reactor.advance(MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0)
  79. limiter = self.get_success(get_retry_limiter("test_dest", self.clock, store))
  80. self.pump(1)
  81. with limiter:
  82. self.pump(1)
  83. # wait for the update to land
  84. self.pump()
  85. new_timings = self.get_success(store.get_destination_retry_timings("test_dest"))
  86. self.assertIsNone(new_timings)