2019-01-17 17:00:23 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2019-01-17 17:00:23 +03:00
|
|
|
#
|
|
|
|
#
|
2022-03-01 12:34:30 +03:00
|
|
|
import traceback
|
2024-10-30 13:51:04 +03:00
|
|
|
from typing import Any, Coroutine, Generator, List, NoReturn, Optional, Tuple, TypeVar
|
2022-03-01 12:34:30 +03:00
|
|
|
|
2022-03-14 20:52:15 +03:00
|
|
|
from parameterized import parameterized_class
|
|
|
|
|
2019-01-17 17:00:23 +03:00
|
|
|
from twisted.internet import defer
|
2022-03-01 12:34:30 +03:00
|
|
|
from twisted.internet.defer import CancelledError, Deferred, ensureDeferred
|
2019-01-17 17:00:23 +03:00
|
|
|
from twisted.internet.task import Clock
|
2022-03-01 12:34:30 +03:00
|
|
|
from twisted.python.failure import Failure
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2020-03-24 17:45:33 +03:00
|
|
|
from synapse.logging.context import (
|
|
|
|
SENTINEL_CONTEXT,
|
|
|
|
LoggingContext,
|
|
|
|
PreserveLoggingContext,
|
|
|
|
current_context,
|
2022-03-14 20:52:15 +03:00
|
|
|
make_deferred_yieldable,
|
2020-03-24 17:45:33 +03:00
|
|
|
)
|
2022-03-01 12:34:30 +03:00
|
|
|
from synapse.util.async_helpers import (
|
2022-05-10 12:39:54 +03:00
|
|
|
AwakenableSleeper,
|
2022-03-01 12:34:30 +03:00
|
|
|
ObservableDeferred,
|
|
|
|
concurrently_execute,
|
2022-03-14 20:52:15 +03:00
|
|
|
delay_cancellation,
|
2024-10-30 13:51:04 +03:00
|
|
|
gather_optional_coroutines,
|
2022-03-01 16:51:03 +03:00
|
|
|
stop_cancellation,
|
2022-03-01 12:34:30 +03:00
|
|
|
timeout_deferred,
|
|
|
|
)
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-05-10 12:39:54 +03:00
|
|
|
from tests.server import get_clock
|
2019-01-17 17:00:23 +03:00
|
|
|
from tests.unittest import TestCase
|
|
|
|
|
2024-10-30 13:51:04 +03:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2021-11-02 03:17:35 +03:00
|
|
|
class ObservableDeferredTest(TestCase):
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_succeed(self) -> None:
|
|
|
|
origin_d: "Deferred[int]" = Deferred()
|
2021-11-02 03:17:35 +03:00
|
|
|
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
|
2022-12-02 20:58:56 +03:00
|
|
|
def check_called_first(res: int) -> int:
|
2021-11-02 03:17:35 +03:00
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
return res
|
|
|
|
|
|
|
|
observer1.addBoth(check_called_first)
|
|
|
|
|
|
|
|
# store the results
|
2023-08-24 17:53:07 +03:00
|
|
|
results: List[Optional[int]] = [None, None]
|
2021-11-02 03:17:35 +03:00
|
|
|
|
2023-08-24 17:53:07 +03:00
|
|
|
def check_val(res: int, idx: int) -> int:
|
2021-11-02 03:17:35 +03:00
|
|
|
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")
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_failure(self) -> None:
|
|
|
|
origin_d: Deferred = Deferred()
|
2021-11-02 03:17:35 +03:00
|
|
|
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
|
2022-12-02 20:58:56 +03:00
|
|
|
def check_called_first(res: int) -> int:
|
2021-11-02 03:17:35 +03:00
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
return res
|
|
|
|
|
|
|
|
observer1.addBoth(check_called_first)
|
|
|
|
|
|
|
|
# store the results
|
2023-08-24 17:53:07 +03:00
|
|
|
results: List[Optional[Failure]] = [None, None]
|
2021-11-02 03:17:35 +03:00
|
|
|
|
2023-08-24 17:53:07 +03:00
|
|
|
def check_failure(res: Failure, idx: int) -> None:
|
2021-11-02 03:17:35 +03:00
|
|
|
results[idx] = res
|
|
|
|
return None
|
|
|
|
|
2023-08-24 17:53:07 +03:00
|
|
|
observer1.addErrback(check_failure, 0)
|
|
|
|
observer2.addErrback(check_failure, 1)
|
2021-11-02 03:17:35 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
raise Exception("gah!")
|
|
|
|
except Exception as e:
|
|
|
|
origin_d.errback(e)
|
2022-12-02 20:58:56 +03:00
|
|
|
assert results[0] is not None
|
2021-11-02 03:17:35 +03:00
|
|
|
self.assertEqual(str(results[0].value), "gah!", "observer 1 errback result")
|
2022-12-02 20:58:56 +03:00
|
|
|
assert results[1] is not None
|
2021-11-02 03:17:35 +03:00
|
|
|
self.assertEqual(str(results[1].value), "gah!", "observer 2 errback result")
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_cancellation(self) -> None:
|
2022-03-04 16:10:05 +03:00
|
|
|
"""Test that cancelling an observer does not affect other observers."""
|
|
|
|
origin_d: "Deferred[int]" = Deferred()
|
|
|
|
observable = ObservableDeferred(origin_d, consumeErrors=True)
|
|
|
|
|
|
|
|
observer1 = observable.observe()
|
|
|
|
observer2 = observable.observe()
|
|
|
|
observer3 = observable.observe()
|
|
|
|
|
|
|
|
self.assertFalse(observer1.called)
|
|
|
|
self.assertFalse(observer2.called)
|
|
|
|
self.assertFalse(observer3.called)
|
|
|
|
|
|
|
|
# cancel the second observer
|
|
|
|
observer2.cancel()
|
|
|
|
self.assertFalse(observer1.called)
|
|
|
|
self.failureResultOf(observer2, CancelledError)
|
|
|
|
self.assertFalse(observer3.called)
|
|
|
|
|
|
|
|
# other observers resolve as normal
|
|
|
|
origin_d.callback(123)
|
|
|
|
self.assertEqual(observer1.result, 123, "observer 1 callback result")
|
|
|
|
self.assertEqual(observer3.result, 123, "observer 3 callback result")
|
|
|
|
|
|
|
|
# additional observers resolve as normal
|
|
|
|
observer4 = observable.observe()
|
|
|
|
self.assertEqual(observer4.result, 123, "observer 4 callback result")
|
|
|
|
|
2021-11-02 03:17:35 +03:00
|
|
|
|
2019-01-17 17:00:23 +03:00
|
|
|
class TimeoutDeferredTest(TestCase):
|
2022-12-02 20:58:56 +03:00
|
|
|
def setUp(self) -> None:
|
2019-01-17 17:00:23 +03:00
|
|
|
self.clock = Clock()
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_times_out(self) -> None:
|
2019-01-17 17:00:23 +03:00
|
|
|
"""Basic test case that checks that the original deferred is cancelled and that
|
|
|
|
the timing-out deferred is errbacked
|
|
|
|
"""
|
2022-12-02 20:58:56 +03:00
|
|
|
cancelled = False
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def canceller(_d: Deferred) -> None:
|
|
|
|
nonlocal cancelled
|
|
|
|
cancelled = True
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
non_completing_d: Deferred = Deferred(canceller)
|
2019-01-17 17:00:23 +03:00
|
|
|
timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
|
|
|
|
|
|
|
|
self.assertNoResult(timing_out_d)
|
2022-12-02 20:58:56 +03:00
|
|
|
self.assertFalse(cancelled, "deferred was cancelled prematurely")
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
self.assertTrue(cancelled, "deferred was not cancelled by timeout")
|
2019-05-10 08:12:11 +03:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_times_out_when_canceller_throws(self) -> None:
|
2019-01-17 17:00:23 +03:00
|
|
|
"""Test that we have successfully worked around
|
|
|
|
https://twistedmatrix.com/trac/ticket/9534"""
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def canceller(_d: Deferred) -> None:
|
2019-01-17 17:00:23 +03:00
|
|
|
raise Exception("can't cancel this deferred")
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
non_completing_d: Deferred = Deferred(canceller)
|
2019-01-17 17:00:23 +03:00
|
|
|
timing_out_d = timeout_deferred(non_completing_d, 1.0, self.clock)
|
|
|
|
|
|
|
|
self.assertNoResult(timing_out_d)
|
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2019-01-17 17:00:23 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_logcontext_is_preserved_on_cancellation(self) -> None:
|
|
|
|
blocking_was_cancelled = False
|
2019-01-17 17:00:23 +03:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def blocking() -> Generator["Deferred[object]", object, None]:
|
|
|
|
nonlocal blocking_was_cancelled
|
|
|
|
|
|
|
|
non_completing_d: Deferred = Deferred()
|
2019-07-03 17:07:04 +03:00
|
|
|
with PreserveLoggingContext():
|
2019-01-17 17:00:23 +03:00
|
|
|
try:
|
|
|
|
yield non_completing_d
|
|
|
|
except CancelledError:
|
2022-12-02 20:58:56 +03:00
|
|
|
blocking_was_cancelled = True
|
2019-01-17 17:00:23 +03:00
|
|
|
raise
|
|
|
|
|
2019-07-03 17:07:04 +03:00
|
|
|
with LoggingContext("one") as context_one:
|
2019-01-17 17:00:23 +03:00
|
|
|
# the errbacks should be run in the test logcontext
|
2022-12-02 20:58:56 +03:00
|
|
|
def errback(res: Failure, deferred_name: str) -> Failure:
|
2019-01-17 17:00:23 +03:00
|
|
|
self.assertIs(
|
2020-03-24 17:45:33 +03:00
|
|
|
current_context(),
|
2019-05-10 08:12:11 +03:00
|
|
|
context_one,
|
|
|
|
"errback %s run in unexpected logcontext %s"
|
2020-03-24 17:45:33 +03:00
|
|
|
% (deferred_name, current_context()),
|
2019-01-17 17:00:23 +03: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 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), SENTINEL_CONTEXT)
|
2019-01-17 17:00:23 +03:00
|
|
|
timing_out_d.addErrback(errback, "timingout")
|
|
|
|
|
2019-05-10 08:12:11 +03:00
|
|
|
self.clock.pump((1.0,))
|
2019-01-17 17:00:23 +03:00
|
|
|
|
|
|
|
self.assertTrue(
|
2022-12-02 20:58:56 +03:00
|
|
|
blocking_was_cancelled, "non-completing deferred was not cancelled"
|
2019-01-17 17:00:23 +03:00
|
|
|
)
|
2019-05-10 08:12:11 +03:00
|
|
|
self.failureResultOf(timing_out_d, defer.TimeoutError)
|
2020-03-24 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), context_one)
|
2022-03-01 12:34:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
class _TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ConcurrentlyExecuteTest(TestCase):
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_limits_runners(self) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
"""If we have more tasks than runners, we should get the limit of runners"""
|
|
|
|
started = 0
|
|
|
|
waiters = []
|
|
|
|
processed = []
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def callback(v: int) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
# when we first enter, bump the start count
|
|
|
|
nonlocal started
|
|
|
|
started += 1
|
|
|
|
|
|
|
|
# record the fact we got an item
|
|
|
|
processed.append(v)
|
|
|
|
|
|
|
|
# wait for the goahead before returning
|
2022-12-02 20:58:56 +03:00
|
|
|
d2: "Deferred[int]" = Deferred()
|
2022-03-01 12:34:30 +03:00
|
|
|
waiters.append(d2)
|
|
|
|
await d2
|
|
|
|
|
|
|
|
# set it going
|
|
|
|
d2 = ensureDeferred(concurrently_execute(callback, [1, 2, 3, 4, 5], 3))
|
|
|
|
|
|
|
|
# check we got exactly 3 processes
|
|
|
|
self.assertEqual(started, 3)
|
|
|
|
self.assertEqual(len(waiters), 3)
|
|
|
|
|
|
|
|
# let one finish
|
|
|
|
waiters.pop().callback(0)
|
|
|
|
|
|
|
|
# ... which should start another
|
|
|
|
self.assertEqual(started, 4)
|
|
|
|
self.assertEqual(len(waiters), 3)
|
|
|
|
|
|
|
|
# we still shouldn't be done
|
|
|
|
self.assertNoResult(d2)
|
|
|
|
|
|
|
|
# finish the job
|
|
|
|
while waiters:
|
|
|
|
waiters.pop().callback(0)
|
|
|
|
|
|
|
|
# check everything got done
|
|
|
|
self.assertEqual(started, 5)
|
|
|
|
self.assertCountEqual(processed, [1, 2, 3, 4, 5])
|
|
|
|
self.successResultOf(d2)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_preserves_stacktraces(self) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
"""Test that the stacktrace from an exception thrown in the callback is preserved"""
|
2022-12-02 20:58:56 +03:00
|
|
|
d1: "Deferred[int]" = Deferred()
|
2022-03-01 12:34:30 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def callback(v: int) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
# alas, this doesn't work at all without an await here
|
|
|
|
await d1
|
|
|
|
raise _TestException("bah")
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def caller() -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
try:
|
|
|
|
await concurrently_execute(callback, [1], 2)
|
|
|
|
except _TestException as e:
|
|
|
|
tb = traceback.extract_tb(e.__traceback__)
|
|
|
|
# we expect to see "caller", "concurrently_execute" and "callback".
|
|
|
|
self.assertEqual(tb[0].name, "caller")
|
|
|
|
self.assertEqual(tb[1].name, "concurrently_execute")
|
|
|
|
self.assertEqual(tb[-1].name, "callback")
|
|
|
|
else:
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
|
|
|
|
d2 = ensureDeferred(caller())
|
|
|
|
d1.callback(0)
|
|
|
|
self.successResultOf(d2)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_preserves_stacktraces_on_preformed_failure(self) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
"""Test that the stacktrace on a Failure returned by the callback is preserved"""
|
2022-12-02 20:58:56 +03:00
|
|
|
d1: "Deferred[int]" = Deferred()
|
2022-03-01 12:34:30 +03:00
|
|
|
f = Failure(_TestException("bah"))
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def callback(v: int) -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
# alas, this doesn't work at all without an await here
|
|
|
|
await d1
|
|
|
|
await defer.fail(f)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def caller() -> None:
|
2022-03-01 12:34:30 +03:00
|
|
|
try:
|
|
|
|
await concurrently_execute(callback, [1], 2)
|
|
|
|
except _TestException as e:
|
|
|
|
tb = traceback.extract_tb(e.__traceback__)
|
2024-11-07 19:22:09 +03:00
|
|
|
|
|
|
|
# Remove twisted internals from the stack, as we don't care
|
|
|
|
# about the precise details.
|
|
|
|
tb = traceback.StackSummary(
|
|
|
|
t for t in tb if "/twisted/" not in t.filename
|
|
|
|
)
|
|
|
|
|
|
|
|
# we expect to see "caller", "concurrently_execute" at the top of the stack
|
2022-03-01 12:34:30 +03:00
|
|
|
self.assertEqual(tb[0].name, "caller")
|
|
|
|
self.assertEqual(tb[1].name, "concurrently_execute")
|
2024-11-07 19:22:09 +03:00
|
|
|
# ... some stack frames from the implementation of `concurrently_execute` ...
|
|
|
|
# and at the bottom of the stack we expect to see "callback"
|
|
|
|
self.assertEqual(tb[-1].name, "callback")
|
2022-03-01 12:34:30 +03:00
|
|
|
else:
|
|
|
|
self.fail("No exception thrown")
|
|
|
|
|
|
|
|
d2 = ensureDeferred(caller())
|
|
|
|
d1.callback(0)
|
|
|
|
self.successResultOf(d2)
|
2022-03-01 16:51:03 +03:00
|
|
|
|
|
|
|
|
2022-03-14 20:52:15 +03:00
|
|
|
@parameterized_class(
|
|
|
|
("wrapper",),
|
|
|
|
[("stop_cancellation",), ("delay_cancellation",)],
|
|
|
|
)
|
|
|
|
class CancellationWrapperTests(TestCase):
|
|
|
|
"""Common tests for the `stop_cancellation` and `delay_cancellation` functions."""
|
|
|
|
|
|
|
|
wrapper: str
|
|
|
|
|
|
|
|
def wrap_deferred(self, deferred: "Deferred[str]") -> "Deferred[str]":
|
|
|
|
if self.wrapper == "stop_cancellation":
|
|
|
|
return stop_cancellation(deferred)
|
|
|
|
elif self.wrapper == "delay_cancellation":
|
|
|
|
return delay_cancellation(deferred)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unsupported wrapper type: {self.wrapper}")
|
2022-03-01 16:51:03 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_succeed(self) -> None:
|
2022-03-01 16:51:03 +03:00
|
|
|
"""Test that the new `Deferred` receives the result."""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
2022-03-14 20:52:15 +03:00
|
|
|
wrapper_deferred = self.wrap_deferred(deferred)
|
2022-03-01 16:51:03 +03:00
|
|
|
|
|
|
|
# Success should propagate through.
|
|
|
|
deferred.callback("success")
|
|
|
|
self.assertTrue(wrapper_deferred.called)
|
|
|
|
self.assertEqual("success", self.successResultOf(wrapper_deferred))
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_failure(self) -> None:
|
2022-03-01 16:51:03 +03:00
|
|
|
"""Test that the new `Deferred` receives the `Failure`."""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
2022-03-14 20:52:15 +03:00
|
|
|
wrapper_deferred = self.wrap_deferred(deferred)
|
2022-03-01 16:51:03 +03:00
|
|
|
|
|
|
|
# Failure should propagate through.
|
|
|
|
deferred.errback(ValueError("abc"))
|
|
|
|
self.assertTrue(wrapper_deferred.called)
|
|
|
|
self.failureResultOf(wrapper_deferred, ValueError)
|
|
|
|
self.assertIsNone(deferred.result, "`Failure` was not consumed")
|
|
|
|
|
2022-03-14 20:52:15 +03:00
|
|
|
|
|
|
|
class StopCancellationTests(TestCase):
|
|
|
|
"""Tests for the `stop_cancellation` function."""
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_cancellation(self) -> None:
|
2022-03-01 16:51:03 +03:00
|
|
|
"""Test that cancellation of the new `Deferred` leaves the original running."""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
|
|
|
wrapper_deferred = stop_cancellation(deferred)
|
|
|
|
|
|
|
|
# Cancel the new `Deferred`.
|
|
|
|
wrapper_deferred.cancel()
|
|
|
|
self.assertTrue(wrapper_deferred.called)
|
|
|
|
self.failureResultOf(wrapper_deferred, CancelledError)
|
|
|
|
self.assertFalse(
|
2022-03-14 20:52:15 +03:00
|
|
|
deferred.called, "Original `Deferred` was unexpectedly cancelled"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now make the original `Deferred` fail.
|
|
|
|
# The `Failure` must be consumed, otherwise unwanted tracebacks will be printed
|
|
|
|
# in logs.
|
|
|
|
deferred.errback(ValueError("abc"))
|
|
|
|
self.assertIsNone(deferred.result, "`Failure` was not consumed")
|
|
|
|
|
|
|
|
|
|
|
|
class DelayCancellationTests(TestCase):
|
|
|
|
"""Tests for the `delay_cancellation` function."""
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_deferred_cancellation(self) -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
"""Test that cancellation of the new `Deferred` waits for the original."""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
|
|
|
wrapper_deferred = delay_cancellation(deferred)
|
|
|
|
|
|
|
|
# Cancel the new `Deferred`.
|
|
|
|
wrapper_deferred.cancel()
|
|
|
|
self.assertNoResult(wrapper_deferred)
|
|
|
|
self.assertFalse(
|
|
|
|
deferred.called, "Original `Deferred` was unexpectedly cancelled"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now make the original `Deferred` fail.
|
|
|
|
# The `Failure` must be consumed, otherwise unwanted tracebacks will be printed
|
|
|
|
# in logs.
|
|
|
|
deferred.errback(ValueError("abc"))
|
|
|
|
self.assertIsNone(deferred.result, "`Failure` was not consumed")
|
|
|
|
|
|
|
|
# Now that the original `Deferred` has failed, we should get a `CancelledError`.
|
|
|
|
self.failureResultOf(wrapper_deferred, CancelledError)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_coroutine_cancellation(self) -> None:
|
2022-04-22 20:20:06 +03:00
|
|
|
"""Test that cancellation of the new `Deferred` waits for the original."""
|
|
|
|
blocking_deferred: "Deferred[None]" = Deferred()
|
|
|
|
completion_deferred: "Deferred[None]" = Deferred()
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def task() -> NoReturn:
|
2022-04-22 20:20:06 +03:00
|
|
|
await blocking_deferred
|
|
|
|
completion_deferred.callback(None)
|
|
|
|
# Raise an exception. Twisted should consume it, otherwise unwanted
|
|
|
|
# tracebacks will be printed in logs.
|
|
|
|
raise ValueError("abc")
|
|
|
|
|
|
|
|
wrapper_deferred = delay_cancellation(task())
|
|
|
|
|
|
|
|
# Cancel the new `Deferred`.
|
|
|
|
wrapper_deferred.cancel()
|
|
|
|
self.assertNoResult(wrapper_deferred)
|
|
|
|
self.assertFalse(
|
|
|
|
blocking_deferred.called, "Cancellation was propagated too deep"
|
|
|
|
)
|
|
|
|
self.assertFalse(completion_deferred.called)
|
|
|
|
|
|
|
|
# Unblock the task.
|
|
|
|
blocking_deferred.callback(None)
|
|
|
|
self.assertTrue(completion_deferred.called)
|
|
|
|
|
|
|
|
# Now that the original coroutine has failed, we should get a `CancelledError`.
|
|
|
|
self.failureResultOf(wrapper_deferred, CancelledError)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_suppresses_second_cancellation(self) -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
"""Test that a second cancellation is suppressed.
|
|
|
|
|
|
|
|
Identical to `test_cancellation` except the new `Deferred` is cancelled twice.
|
|
|
|
"""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
|
|
|
wrapper_deferred = delay_cancellation(deferred)
|
|
|
|
|
|
|
|
# Cancel the new `Deferred`, twice.
|
|
|
|
wrapper_deferred.cancel()
|
|
|
|
wrapper_deferred.cancel()
|
|
|
|
self.assertNoResult(wrapper_deferred)
|
|
|
|
self.assertFalse(
|
|
|
|
deferred.called, "Original `Deferred` was unexpectedly cancelled"
|
2022-03-01 16:51:03 +03:00
|
|
|
)
|
|
|
|
|
2022-03-14 20:52:15 +03:00
|
|
|
# Now make the original `Deferred` fail.
|
2022-03-01 16:51:03 +03:00
|
|
|
# The `Failure` must be consumed, otherwise unwanted tracebacks will be printed
|
|
|
|
# in logs.
|
|
|
|
deferred.errback(ValueError("abc"))
|
|
|
|
self.assertIsNone(deferred.result, "`Failure` was not consumed")
|
2022-03-14 20:52:15 +03:00
|
|
|
|
|
|
|
# Now that the original `Deferred` has failed, we should get a `CancelledError`.
|
|
|
|
self.failureResultOf(wrapper_deferred, CancelledError)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_propagates_cancelled_error(self) -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
"""Test that a `CancelledError` from the original `Deferred` gets propagated."""
|
|
|
|
deferred: "Deferred[str]" = Deferred()
|
|
|
|
wrapper_deferred = delay_cancellation(deferred)
|
|
|
|
|
|
|
|
# Fail the original `Deferred` with a `CancelledError`.
|
|
|
|
cancelled_error = CancelledError()
|
|
|
|
deferred.errback(cancelled_error)
|
|
|
|
|
|
|
|
# The new `Deferred` should fail with exactly the same `CancelledError`.
|
|
|
|
self.assertTrue(wrapper_deferred.called)
|
|
|
|
self.assertIs(cancelled_error, self.failureResultOf(wrapper_deferred).value)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_preserves_logcontext(self) -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
"""Test that logging contexts are preserved."""
|
|
|
|
blocking_d: "Deferred[None]" = Deferred()
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def inner() -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
await make_deferred_yieldable(blocking_d)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
async def outer() -> None:
|
2022-03-14 20:52:15 +03:00
|
|
|
with LoggingContext("c") as c:
|
|
|
|
try:
|
2022-04-22 20:20:06 +03:00
|
|
|
await delay_cancellation(inner())
|
2022-03-14 20:52:15 +03:00
|
|
|
self.fail("`CancelledError` was not raised")
|
|
|
|
except CancelledError:
|
|
|
|
self.assertEqual(c, current_context())
|
|
|
|
# Succeed with no error, unless the logging context is wrong.
|
|
|
|
|
|
|
|
# Run and block inside `inner()`.
|
|
|
|
d = defer.ensureDeferred(outer())
|
|
|
|
self.assertEqual(SENTINEL_CONTEXT, current_context())
|
|
|
|
|
|
|
|
d.cancel()
|
|
|
|
|
|
|
|
# Now unblock. `outer()` will consume the `CancelledError` and check the
|
|
|
|
# logging context.
|
|
|
|
blocking_d.callback(None)
|
|
|
|
self.successResultOf(d)
|
2022-05-10 12:39:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
class AwakenableSleeperTests(TestCase):
|
|
|
|
"Tests AwakenableSleeper"
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_sleep(self) -> None:
|
2022-05-10 12:39:54 +03:00
|
|
|
reactor, _ = get_clock()
|
|
|
|
sleeper = AwakenableSleeper(reactor)
|
|
|
|
|
|
|
|
d = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
reactor.pump([0.0])
|
|
|
|
self.assertFalse(d.called)
|
|
|
|
|
|
|
|
reactor.advance(0.5)
|
|
|
|
self.assertFalse(d.called)
|
|
|
|
|
|
|
|
reactor.advance(0.6)
|
|
|
|
self.assertTrue(d.called)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_explicit_wake(self) -> None:
|
2022-05-10 12:39:54 +03:00
|
|
|
reactor, _ = get_clock()
|
|
|
|
sleeper = AwakenableSleeper(reactor)
|
|
|
|
|
|
|
|
d = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
reactor.pump([0.0])
|
|
|
|
self.assertFalse(d.called)
|
|
|
|
|
|
|
|
reactor.advance(0.5)
|
|
|
|
self.assertFalse(d.called)
|
|
|
|
|
|
|
|
sleeper.wake("name")
|
|
|
|
self.assertTrue(d.called)
|
|
|
|
|
|
|
|
reactor.advance(0.6)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_multiple_sleepers_timeout(self) -> None:
|
2022-05-10 12:39:54 +03:00
|
|
|
reactor, _ = get_clock()
|
|
|
|
sleeper = AwakenableSleeper(reactor)
|
|
|
|
|
|
|
|
d1 = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
reactor.advance(0.6)
|
|
|
|
self.assertFalse(d1.called)
|
|
|
|
|
|
|
|
# Add another sleeper
|
|
|
|
d2 = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
# Only the first sleep should time out now.
|
|
|
|
reactor.advance(0.6)
|
|
|
|
self.assertTrue(d1.called)
|
|
|
|
self.assertFalse(d2.called)
|
|
|
|
|
|
|
|
reactor.advance(0.6)
|
|
|
|
self.assertTrue(d2.called)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_multiple_sleepers_wake(self) -> None:
|
2022-05-10 12:39:54 +03:00
|
|
|
reactor, _ = get_clock()
|
|
|
|
sleeper = AwakenableSleeper(reactor)
|
|
|
|
|
|
|
|
d1 = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
reactor.advance(0.5)
|
|
|
|
self.assertFalse(d1.called)
|
|
|
|
|
|
|
|
# Add another sleeper
|
|
|
|
d2 = defer.ensureDeferred(sleeper.sleep("name", 1000))
|
|
|
|
|
|
|
|
# Neither should fire yet
|
|
|
|
reactor.advance(0.3)
|
|
|
|
self.assertFalse(d1.called)
|
|
|
|
self.assertFalse(d2.called)
|
|
|
|
|
|
|
|
# Explicitly waking both up works
|
|
|
|
sleeper.wake("name")
|
|
|
|
self.assertTrue(d1.called)
|
|
|
|
self.assertTrue(d2.called)
|
2024-10-30 13:51:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
class GatherCoroutineTests(TestCase):
|
|
|
|
"""Tests for `gather_optional_coroutines`"""
|
|
|
|
|
|
|
|
def make_coroutine(self) -> Tuple[Coroutine[Any, Any, T], "defer.Deferred[T]"]:
|
|
|
|
"""Returns a coroutine and a deferred that it is waiting on to resolve"""
|
|
|
|
|
|
|
|
d: "defer.Deferred[T]" = defer.Deferred()
|
|
|
|
|
|
|
|
async def inner() -> T:
|
|
|
|
with PreserveLoggingContext():
|
|
|
|
return await d
|
|
|
|
|
|
|
|
return inner(), d
|
|
|
|
|
|
|
|
def test_single(self) -> None:
|
|
|
|
"Test passing in a single coroutine works"
|
|
|
|
|
|
|
|
with LoggingContext("test_ctx") as text_ctx:
|
|
|
|
deferred: "defer.Deferred[None]"
|
|
|
|
coroutine, deferred = self.make_coroutine()
|
|
|
|
|
|
|
|
gather_deferred = defer.ensureDeferred(
|
|
|
|
gather_optional_coroutines(coroutine)
|
|
|
|
)
|
|
|
|
|
|
|
|
# We shouldn't have a result yet, and should be in the sentinel
|
|
|
|
# context.
|
|
|
|
self.assertNoResult(gather_deferred)
|
|
|
|
self.assertEqual(current_context(), SENTINEL_CONTEXT)
|
|
|
|
|
|
|
|
# Resolving the deferred will resolve the coroutine
|
|
|
|
deferred.callback(None)
|
|
|
|
|
|
|
|
# All coroutines have resolved, and so we should have the results
|
|
|
|
result = self.successResultOf(gather_deferred)
|
|
|
|
self.assertEqual(result, (None,))
|
|
|
|
|
|
|
|
# We should be back in the normal context.
|
|
|
|
self.assertEqual(current_context(), text_ctx)
|
|
|
|
|
|
|
|
def test_multiple_resolve(self) -> None:
|
|
|
|
"Test passing in multiple coroutine that all resolve works"
|
|
|
|
|
|
|
|
with LoggingContext("test_ctx") as test_ctx:
|
|
|
|
deferred1: "defer.Deferred[int]"
|
|
|
|
coroutine1, deferred1 = self.make_coroutine()
|
|
|
|
deferred2: "defer.Deferred[str]"
|
|
|
|
coroutine2, deferred2 = self.make_coroutine()
|
|
|
|
|
|
|
|
gather_deferred = defer.ensureDeferred(
|
|
|
|
gather_optional_coroutines(coroutine1, coroutine2)
|
|
|
|
)
|
|
|
|
|
|
|
|
# We shouldn't have a result yet, and should be in the sentinel
|
|
|
|
# context.
|
|
|
|
self.assertNoResult(gather_deferred)
|
|
|
|
self.assertEqual(current_context(), SENTINEL_CONTEXT)
|
|
|
|
|
|
|
|
# Even if we resolve one of the coroutines, we shouldn't have a result
|
|
|
|
# yet
|
|
|
|
deferred2.callback("test")
|
|
|
|
self.assertNoResult(gather_deferred)
|
|
|
|
self.assertEqual(current_context(), SENTINEL_CONTEXT)
|
|
|
|
|
|
|
|
deferred1.callback(1)
|
|
|
|
|
|
|
|
# All coroutines have resolved, and so we should have the results
|
|
|
|
result = self.successResultOf(gather_deferred)
|
|
|
|
self.assertEqual(result, (1, "test"))
|
|
|
|
|
|
|
|
# We should be back in the normal context.
|
|
|
|
self.assertEqual(current_context(), test_ctx)
|
|
|
|
|
|
|
|
def test_multiple_fail(self) -> None:
|
|
|
|
"Test passing in multiple coroutine where one fails does the right thing"
|
|
|
|
|
|
|
|
with LoggingContext("test_ctx") as test_ctx:
|
|
|
|
deferred1: "defer.Deferred[int]"
|
|
|
|
coroutine1, deferred1 = self.make_coroutine()
|
|
|
|
deferred2: "defer.Deferred[str]"
|
|
|
|
coroutine2, deferred2 = self.make_coroutine()
|
|
|
|
|
|
|
|
gather_deferred = defer.ensureDeferred(
|
|
|
|
gather_optional_coroutines(coroutine1, coroutine2)
|
|
|
|
)
|
|
|
|
|
|
|
|
# We shouldn't have a result yet, and should be in the sentinel
|
|
|
|
# context.
|
|
|
|
self.assertNoResult(gather_deferred)
|
|
|
|
self.assertEqual(current_context(), SENTINEL_CONTEXT)
|
|
|
|
|
|
|
|
# Throw an exception in one of the coroutines
|
|
|
|
exc = Exception("test")
|
|
|
|
deferred2.errback(exc)
|
|
|
|
|
|
|
|
# Expect the gather deferred to immediately fail
|
|
|
|
result_exc = self.failureResultOf(gather_deferred)
|
|
|
|
self.assertEqual(result_exc.value, exc)
|
|
|
|
|
|
|
|
# We should be back in the normal context.
|
|
|
|
self.assertEqual(current_context(), test_ctx)
|