test_async_utils.py 3.7 KB

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