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