test_async_utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2019 New Vector Ltd
  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 twisted.internet import defer
  15. from twisted.internet.defer import CancelledError, Deferred
  16. from twisted.internet.task import Clock
  17. from synapse.logging.context import (
  18. SENTINEL_CONTEXT,
  19. LoggingContext,
  20. PreserveLoggingContext,
  21. current_context,
  22. )
  23. from synapse.util.async_helpers import timeout_deferred
  24. from tests.unittest import TestCase
  25. class TimeoutDeferredTest(TestCase):
  26. def setUp(self):
  27. self.clock = Clock()
  28. def test_times_out(self):
  29. """Basic test case that checks that the original deferred is cancelled and that
  30. the timing-out deferred is errbacked
  31. """
  32. cancelled = [False]
  33. def canceller(_d):
  34. cancelled[0] = True
  35. non_completing_d = Deferred(canceller)
  36. timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
  37. self.assertNoResult(timing_out_d)
  38. self.assertFalse(cancelled[0], "deferred was cancelled prematurely")
  39. self.clock.pump((1.0,))
  40. self.assertTrue(cancelled[0], "deferred was not cancelled by timeout")
  41. self.failureResultOf(timing_out_d, defer.TimeoutError)
  42. def test_times_out_when_canceller_throws(self):
  43. """Test that we have successfully worked around
  44. https://twistedmatrix.com/trac/ticket/9534"""
  45. def canceller(_d):
  46. raise Exception("can't cancel this deferred")
  47. non_completing_d = Deferred(canceller)
  48. timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
  49. self.assertNoResult(timing_out_d)
  50. self.clock.pump((1.0,))
  51. self.failureResultOf(timing_out_d, defer.TimeoutError)
  52. def test_logcontext_is_preserved_on_cancellation(self):
  53. blocking_was_cancelled = [False]
  54. @defer.inlineCallbacks
  55. def blocking():
  56. non_completing_d = Deferred()
  57. with PreserveLoggingContext():
  58. try:
  59. yield non_completing_d
  60. except CancelledError:
  61. blocking_was_cancelled[0] = True
  62. raise
  63. with LoggingContext("one") as context_one:
  64. # the errbacks should be run in the test logcontext
  65. def errback(res, deferred_name):
  66. self.assertIs(
  67. current_context(),
  68. context_one,
  69. "errback %s run in unexpected logcontext %s"
  70. % (deferred_name, current_context()),
  71. )
  72. return res
  73. original_deferred = blocking()
  74. original_deferred.addErrback(errback, "orig")
  75. timing_out_d = timeout_deferred(original_deferred, 1.0, self.clock)
  76. self.assertNoResult(timing_out_d)
  77. self.assertIs(current_context(), SENTINEL_CONTEXT)
  78. timing_out_d.addErrback(errback, "timingout")
  79. self.clock.pump((1.0,))
  80. self.assertTrue(
  81. blocking_was_cancelled[0], "non-completing deferred was not cancelled"
  82. )
  83. self.failureResultOf(timing_out_d, defer.TimeoutError)
  84. self.assertIs(current_context(), context_one)