2019-01-17 09:00:23 -05:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.defer import CancelledError, Deferred
|
|
|
|
from twisted.internet.task import Clock
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import (
|
|
|
|
SENTINEL_CONTEXT,
|
|
|
|
LoggingContext,
|
|
|
|
PreserveLoggingContext,
|
|
|
|
current_context,
|
|
|
|
)
|
2021-11-01 20:17:35 -04:00
|
|
|
from synapse.util.async_helpers import ObservableDeferred, timeout_deferred
|
2019-01-17 09:00:23 -05:00
|
|
|
|
|
|
|
from tests.unittest import TestCase
|
|
|
|
|
|
|
|
|
2021-11-01 20:17:35 -04:00
|
|
|
class ObservableDeferredTest(TestCase):
|
|
|
|
def test_succeed(self):
|
|
|
|
origin_d = Deferred()
|
|
|
|
observable = ObservableDeferred(origin_d)
|
|
|
|
|
|
|
|
observer1 = observable.observe()
|
|
|
|
observer2 = observable.observe()
|
|
|
|
|
|
|
|
self.assertFalse(observer1.called)
|
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
|
|
|
|
# check the first observer is called first
|
|
|
|
def check_called_first(res):
|
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
return res
|
|
|
|
|
|
|
|
observer1.addBoth(check_called_first)
|
|
|
|
|
|
|
|
# store the results
|
|
|
|
results = [None, None]
|
|
|
|
|
|
|
|
def check_val(res, idx):
|
|
|
|
results[idx] = res
|
|
|
|
return res
|
|
|
|
|
|
|
|
observer1.addCallback(check_val, 0)
|
|
|
|
observer2.addCallback(check_val, 1)
|
|
|
|
|
|
|
|
origin_d.callback(123)
|
|
|
|
self.assertEqual(results[0], 123, "observer 1 callback result")
|
|
|
|
self.assertEqual(results[1], 123, "observer 2 callback result")
|
|
|
|
|
|
|
|
def test_failure(self):
|
|
|
|
origin_d = Deferred()
|
|
|
|
observable = ObservableDeferred(origin_d, consumeErrors=True)
|
|
|
|
|
|
|
|
observer1 = observable.observe()
|
|
|
|
observer2 = observable.observe()
|
|
|
|
|
|
|
|
self.assertFalse(observer1.called)
|
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
|
|
|
|
# check the first observer is called first
|
|
|
|
def check_called_first(res):
|
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
return res
|
|
|
|
|
|
|
|
observer1.addBoth(check_called_first)
|
|
|
|
|
|
|
|
# store the results
|
|
|
|
results = [None, None]
|
|
|
|
|
|
|
|
def check_val(res, idx):
|
|
|
|
results[idx] = res
|
|
|
|
return None
|
|
|
|
|
|
|
|
observer1.addErrback(check_val, 0)
|
|
|
|
observer2.addErrback(check_val, 1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise Exception("gah!")
|
|
|
|
except Exception as e:
|
|
|
|
origin_d.errback(e)
|
|
|
|
self.assertEqual(str(results[0].value), "gah!", "observer 1 errback result")
|
|
|
|
self.assertEqual(str(results[1].value), "gah!", "observer 2 errback result")
|
|
|
|
|
|
|
|
|
2019-01-17 09:00:23 -05:00
|
|
|
class TimeoutDeferredTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.clock = Clock()
|
|
|
|
|
|
|
|
def test_times_out(self):
|
|
|
|
"""Basic test case that checks that the original deferred is cancelled and that
|
|
|
|
the timing-out deferred is errbacked
|
|
|
|
"""
|
|
|
|
cancelled = [False]
|
|
|
|
|
|
|
|
def canceller(_d):
|
|
|
|
cancelled[0] = True
|
|
|
|
|
|
|
|
non_completing_d = Deferred(canceller)
|
|
|
|
timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
|
|
|
|
|
|
|
|
self.assertNoResult(timing_out_d)
|
|
|
|
self.assertFalse(cancelled[0], "deferred was cancelled prematurely")
|
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 09:00:23 -05:00
|
|
|
|
|
|
|
self.assertTrue(cancelled[0], "deferred was not cancelled by timeout")
|
2019-05-10 01:12:11 -04:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2019-01-17 09:00:23 -05:00
|
|
|
|
|
|
|
def test_times_out_when_canceller_throws(self):
|
|
|
|
"""Test that we have successfully worked around
|
|
|
|
https://twistedmatrix.com/trac/ticket/9534"""
|
|
|
|
|
|
|
|
def canceller(_d):
|
|
|
|
raise Exception("can't cancel this deferred")
|
|
|
|
|
|
|
|
non_completing_d = Deferred(canceller)
|
|
|
|
timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
|
|
|
|
|
|
|
|
self.assertNoResult(timing_out_d)
|
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 09:00:23 -05:00
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2019-01-17 09:00:23 -05:00
|
|
|
|
|
|
|
def test_logcontext_is_preserved_on_cancellation(self):
|
|
|
|
blocking_was_cancelled = [False]
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def blocking():
|
|
|
|
non_completing_d = Deferred()
|
2019-07-03 10:07:04 -04:00
|
|
|
with PreserveLoggingContext():
|
2019-01-17 09:00:23 -05:00
|
|
|
try:
|
|
|
|
yield non_completing_d
|
|
|
|
except CancelledError:
|
|
|
|
blocking_was_cancelled[0] = True
|
|
|
|
raise
|
|
|
|
|
2019-07-03 10:07:04 -04:00
|
|
|
with LoggingContext("one") as context_one:
|
2019-01-17 09:00:23 -05:00
|
|
|
# the errbacks should be run in the test logcontext
|
|
|
|
def errback(res, deferred_name):
|
|
|
|
self.assertIs(
|
2020-03-24 10:45:33 -04:00
|
|
|
current_context(),
|
2019-05-10 01:12:11 -04:00
|
|
|
context_one,
|
|
|
|
"errback %s run in unexpected logcontext %s"
|
2020-03-24 10:45:33 -04:00
|
|
|
% (deferred_name, current_context()),
|
2019-01-17 09:00:23 -05:00
|
|
|
)
|
|
|
|
return res
|
|
|
|
|
|
|
|
original_deferred = blocking()
|
|
|
|
original_deferred.addErrback(errback, "orig")
|
|
|
|
timing_out_d = timeout_deferred(original_deferred, 1.0, self.clock)
|
|
|
|
self.assertNoResult(timing_out_d)
|
2020-03-24 10:45:33 -04:00
|
|
|
self.assertIs(current_context(), SENTINEL_CONTEXT)
|
2019-01-17 09:00:23 -05:00
|
|
|
timing_out_d.addErrback(errback, "timingout")
|
|
|
|
|
2019-05-10 01:12:11 -04:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 09:00:23 -05:00
|
|
|
|
|
|
|
self.assertTrue(
|
2019-05-10 01:12:11 -04:00
|
|
|
blocking_was_cancelled[0], "non-completing deferred was not cancelled"
|
2019-01-17 09:00:23 -05:00
|
|
|
)
|
2019-05-10 01:12:11 -04:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2020-03-24 10:45:33 -04:00
|
|
|
self.assertIs(current_context(), context_one)
|