test_retryutils.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. d = get_retry_limiter("test_dest", self.clock, store)
  27. self.pump()
  28. limiter = self.successResultOf(d)
  29. # advance the clock a bit before making the request
  30. self.pump(1)
  31. with limiter:
  32. pass
  33. d = store.get_destination_retry_timings("test_dest")
  34. self.pump()
  35. new_timings = self.successResultOf(d)
  36. self.assertIsNone(new_timings)
  37. def test_limiter(self):
  38. """General test case which walks through the process of a failing request"""
  39. store = self.hs.get_datastore()
  40. d = get_retry_limiter("test_dest", self.clock, store)
  41. self.pump()
  42. limiter = self.successResultOf(d)
  43. self.pump(1)
  44. try:
  45. with limiter:
  46. self.pump(1)
  47. failure_ts = self.clock.time_msec()
  48. raise AssertionError("argh")
  49. except AssertionError:
  50. pass
  51. # wait for the update to land
  52. self.pump()
  53. d = store.get_destination_retry_timings("test_dest")
  54. self.pump()
  55. new_timings = self.successResultOf(d)
  56. self.assertEqual(new_timings["failure_ts"], failure_ts)
  57. self.assertEqual(new_timings["retry_last_ts"], failure_ts)
  58. self.assertEqual(new_timings["retry_interval"], MIN_RETRY_INTERVAL)
  59. # now if we try again we should get a failure
  60. d = get_retry_limiter("test_dest", self.clock, store)
  61. self.pump()
  62. self.failureResultOf(d, NotRetryingDestination)
  63. #
  64. # advance the clock and try again
  65. #
  66. self.pump(MIN_RETRY_INTERVAL)
  67. d = get_retry_limiter("test_dest", self.clock, store)
  68. self.pump()
  69. limiter = self.successResultOf(d)
  70. self.pump(1)
  71. try:
  72. with limiter:
  73. self.pump(1)
  74. retry_ts = self.clock.time_msec()
  75. raise AssertionError("argh")
  76. except AssertionError:
  77. pass
  78. # wait for the update to land
  79. self.pump()
  80. d = store.get_destination_retry_timings("test_dest")
  81. self.pump()
  82. new_timings = self.successResultOf(d)
  83. self.assertEqual(new_timings["failure_ts"], failure_ts)
  84. self.assertEqual(new_timings["retry_last_ts"], retry_ts)
  85. self.assertGreaterEqual(
  86. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 0.5
  87. )
  88. self.assertLessEqual(
  89. new_timings["retry_interval"], MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0
  90. )
  91. #
  92. # one more go, with success
  93. #
  94. self.pump(MIN_RETRY_INTERVAL * RETRY_MULTIPLIER * 2.0)
  95. d = get_retry_limiter("test_dest", self.clock, store)
  96. self.pump()
  97. limiter = self.successResultOf(d)
  98. self.pump(1)
  99. with limiter:
  100. self.pump(1)
  101. # wait for the update to land
  102. self.pump()
  103. d = store.get_destination_retry_timings("test_dest")
  104. self.pump()
  105. new_timings = self.successResultOf(d)
  106. self.assertIsNone(new_timings)