test_async_utils.py 3.7 KB

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