test_async_utils.py 3.7 KB

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