2022-12-02 20:58:56 +03:00
|
|
|
#
|
2023-11-21 23:29:58 +03:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 14:26:48 +03:00
|
|
|
# Copyright 2014-2022 The Matrix.org Foundation C.I.C.
|
2023-11-21 23:29:58 +03:00
|
|
|
# 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]
|
2022-12-02 20:58:56 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from typing import Callable, Generator, cast
|
|
|
|
|
2017-03-17 23:56:54 +03:00
|
|
|
import twisted.python.failure
|
2022-12-02 20:58:56 +03:00
|
|
|
from twisted.internet import defer, reactor as _reactor
|
2014-10-30 04:21:33 +03:00
|
|
|
|
2019-07-03 17:07:04 +03:00
|
|
|
from synapse.logging.context import (
|
2020-03-24 17:45:33 +03:00
|
|
|
SENTINEL_CONTEXT,
|
2019-07-03 17:07:04 +03:00
|
|
|
LoggingContext,
|
|
|
|
PreserveLoggingContext,
|
2020-03-24 17:45:33 +03:00
|
|
|
current_context,
|
2019-07-03 17:07:04 +03:00
|
|
|
make_deferred_yieldable,
|
|
|
|
nested_logging_context,
|
|
|
|
run_in_background,
|
|
|
|
)
|
2022-12-02 20:58:56 +03:00
|
|
|
from synapse.types import ISynapseReactor
|
2019-07-03 17:07:04 +03:00
|
|
|
from synapse.util import Clock
|
2014-10-30 04:21:33 +03:00
|
|
|
|
2018-07-09 09:09:20 +03:00
|
|
|
from .. import unittest
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
reactor = cast(ISynapseReactor, _reactor)
|
|
|
|
|
2016-02-09 17:57:43 +03:00
|
|
|
|
2014-10-30 04:21:33 +03:00
|
|
|
class LoggingContextTestCase(unittest.TestCase):
|
2022-12-02 20:58:56 +03:00
|
|
|
def _check_test_key(self, value: str) -> None:
|
|
|
|
context = current_context()
|
|
|
|
assert isinstance(context, LoggingContext)
|
|
|
|
self.assertEqual(context.name, value)
|
2014-10-30 04:21:33 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_with_context(self) -> None:
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("test"):
|
2014-10-30 04:21:33 +03:00
|
|
|
self._check_test_key("test")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_sleep(self) -> Generator["defer.Deferred[object]", object, None]:
|
2018-06-22 11:37:10 +03:00
|
|
|
clock = Clock(reactor)
|
|
|
|
|
2014-10-30 04:21:33 +03:00
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def competing_callback() -> Generator["defer.Deferred[object]", object, None]:
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("competing"):
|
2018-06-22 11:37:10 +03:00
|
|
|
yield clock.sleep(0)
|
2014-10-30 04:21:33 +03:00
|
|
|
self._check_test_key("competing")
|
|
|
|
|
|
|
|
reactor.callLater(0, competing_callback)
|
|
|
|
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("one"):
|
2018-06-22 11:37:10 +03:00
|
|
|
yield clock.sleep(0)
|
2014-10-30 04:21:33 +03:00
|
|
|
self._check_test_key("one")
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def _test_run_in_background(self, function: Callable[[], object]) -> defer.Deferred:
|
2020-03-24 17:45:33 +03:00
|
|
|
sentinel_context = current_context()
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
callback_completed = False
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("one"):
|
2019-07-02 21:01:28 +03:00
|
|
|
# fire off function, but don't wait on it.
|
2019-07-03 17:07:04 +03:00
|
|
|
d2 = run_in_background(function)
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def cb(res: object) -> object:
|
|
|
|
nonlocal callback_completed
|
|
|
|
callback_completed = True
|
2018-05-02 13:46:23 +03:00
|
|
|
return res
|
2018-08-10 16:54:09 +03:00
|
|
|
|
2019-07-02 21:01:28 +03:00
|
|
|
d2.addCallback(cb)
|
2017-03-17 23:56:54 +03:00
|
|
|
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
# now wait for the function under test to have run, and check that
|
|
|
|
# the logcontext is left in a sane state.
|
|
|
|
d2 = defer.Deferred()
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def check_logcontext() -> None:
|
|
|
|
if not callback_completed:
|
2017-03-17 23:56:54 +03:00
|
|
|
reactor.callLater(0.01, check_logcontext)
|
|
|
|
return
|
|
|
|
|
|
|
|
# make sure that the context was reset before it got thrown back
|
|
|
|
# into the reactor
|
|
|
|
try:
|
2020-03-24 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), sentinel_context)
|
2017-03-17 23:56:54 +03:00
|
|
|
d2.callback(None)
|
|
|
|
except BaseException:
|
|
|
|
d2.errback(twisted.python.failure.Failure())
|
|
|
|
|
|
|
|
reactor.callLater(0.01, check_logcontext)
|
|
|
|
|
|
|
|
# test is done once d2 finishes
|
|
|
|
return d2
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_run_in_background_with_blocking_fn(self) -> defer.Deferred:
|
2017-03-17 23:56:54 +03:00
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def blocking_function() -> Generator["defer.Deferred[object]", object, None]:
|
2018-06-22 11:37:10 +03:00
|
|
|
yield Clock(reactor).sleep(0)
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2018-05-02 13:46:23 +03:00
|
|
|
return self._test_run_in_background(blocking_function)
|
2017-03-17 23:56:54 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_run_in_background_with_non_blocking_fn(self) -> defer.Deferred:
|
2017-03-17 23:56:54 +03:00
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def nonblocking_function() -> Generator["defer.Deferred[object]", object, None]:
|
2019-07-03 17:07:04 +03:00
|
|
|
with PreserveLoggingContext():
|
2017-03-17 23:56:54 +03:00
|
|
|
yield defer.succeed(None)
|
|
|
|
|
2018-05-02 13:46:23 +03:00
|
|
|
return self._test_run_in_background(nonblocking_function)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_run_in_background_with_chained_deferred(self) -> defer.Deferred:
|
2018-05-02 13:46:23 +03:00
|
|
|
# a function which returns a deferred which looks like it has been
|
|
|
|
# called, but is actually paused
|
2022-12-02 20:58:56 +03:00
|
|
|
def testfunc() -> defer.Deferred:
|
2019-07-03 17:07:04 +03:00
|
|
|
return make_deferred_yieldable(_chained_deferred_function())
|
2018-05-02 13:46:23 +03:00
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
2017-10-17 12:52:31 +03:00
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_run_in_background_with_coroutine(self) -> defer.Deferred:
|
|
|
|
async def testfunc() -> None:
|
2019-07-02 21:01:28 +03:00
|
|
|
self._check_test_key("one")
|
|
|
|
d = Clock(reactor).sleep(0)
|
2020-03-24 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), SENTINEL_CONTEXT)
|
2019-07-02 21:01:28 +03:00
|
|
|
await d
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_run_in_background_with_nonblocking_coroutine(self) -> defer.Deferred:
|
|
|
|
async def testfunc() -> None:
|
2019-07-02 21:01:28 +03:00
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
|
|
|
|
2017-10-17 12:52:31 +03:00
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_make_deferred_yieldable(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[object]", object, None]:
|
2020-07-09 16:52:58 +03:00
|
|
|
# a function which returns an incomplete deferred, but doesn't follow
|
2017-10-17 12:52:31 +03:00
|
|
|
# the synapse rules.
|
2022-12-02 20:58:56 +03:00
|
|
|
def blocking_function() -> defer.Deferred:
|
|
|
|
d: defer.Deferred = defer.Deferred()
|
2017-10-17 12:52:31 +03:00
|
|
|
reactor.callLater(0, d.callback, None)
|
|
|
|
return d
|
|
|
|
|
2020-03-24 17:45:33 +03:00
|
|
|
sentinel_context = current_context()
|
2017-10-17 12:52:31 +03:00
|
|
|
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("one"):
|
2019-07-03 17:07:04 +03:00
|
|
|
d1 = make_deferred_yieldable(blocking_function())
|
2017-10-17 12:52:31 +03:00
|
|
|
# make sure that the context was reset by make_deferred_yieldable
|
2020-03-24 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), sentinel_context)
|
2017-10-17 12:52:31 +03:00
|
|
|
|
|
|
|
yield d1
|
|
|
|
|
|
|
|
# now it should be restored
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
2018-05-02 13:46:23 +03:00
|
|
|
@defer.inlineCallbacks
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_make_deferred_yieldable_with_chained_deferreds(
|
|
|
|
self,
|
|
|
|
) -> Generator["defer.Deferred[object]", object, None]:
|
2020-03-24 17:45:33 +03:00
|
|
|
sentinel_context = current_context()
|
2018-05-02 13:46:23 +03:00
|
|
|
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("one"):
|
2019-07-03 17:07:04 +03:00
|
|
|
d1 = make_deferred_yieldable(_chained_deferred_function())
|
2018-05-02 13:46:23 +03:00
|
|
|
# make sure that the context was reset by make_deferred_yieldable
|
2020-03-24 17:45:33 +03:00
|
|
|
self.assertIs(current_context(), sentinel_context)
|
2018-05-02 13:46:23 +03:00
|
|
|
|
|
|
|
yield d1
|
|
|
|
|
|
|
|
# now it should be restored
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def test_nested_logging_context(self) -> None:
|
2021-04-08 15:01:14 +03:00
|
|
|
with LoggingContext("foo"):
|
2019-07-03 17:07:04 +03:00
|
|
|
nested_context = nested_logging_context(suffix="bar")
|
2021-04-08 15:01:14 +03:00
|
|
|
self.assertEqual(nested_context.name, "foo-bar")
|
2018-09-27 13:25:34 +03:00
|
|
|
|
2018-05-02 13:46:23 +03:00
|
|
|
|
|
|
|
# a function which returns a deferred which has been "called", but
|
|
|
|
# which had a function which returned another incomplete deferred on
|
|
|
|
# its callback list, so won't yet call any other new callbacks.
|
2022-12-02 20:58:56 +03:00
|
|
|
def _chained_deferred_function() -> defer.Deferred:
|
2018-05-02 13:46:23 +03:00
|
|
|
d = defer.succeed(None)
|
|
|
|
|
2022-12-02 20:58:56 +03:00
|
|
|
def cb(res: object) -> defer.Deferred:
|
|
|
|
d2: defer.Deferred = defer.Deferred()
|
2018-05-02 13:46:23 +03:00
|
|
|
reactor.callLater(0, d2.callback, res)
|
|
|
|
return d2
|
2018-08-10 16:54:09 +03:00
|
|
|
|
2018-05-02 13:46:23 +03:00
|
|
|
d.addCallback(cb)
|
|
|
|
return d
|