2017-03-17 21:56:54 +01:00
|
|
|
import twisted.python.failure
|
2018-07-09 08:09:20 +02:00
|
|
|
from twisted.internet import defer, reactor
|
2014-10-30 02:21:33 +01:00
|
|
|
|
2019-07-03 16:07:04 +02:00
|
|
|
from synapse.logging.context import (
|
|
|
|
LoggingContext,
|
|
|
|
PreserveLoggingContext,
|
|
|
|
make_deferred_yieldable,
|
|
|
|
nested_logging_context,
|
|
|
|
run_in_background,
|
|
|
|
)
|
|
|
|
from synapse.util import Clock
|
2014-10-30 02:21:33 +01:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from .. import unittest
|
|
|
|
|
2016-02-09 15:57:43 +01:00
|
|
|
|
2014-10-30 02:21:33 +01:00
|
|
|
class LoggingContextTestCase(unittest.TestCase):
|
|
|
|
def _check_test_key(self, value):
|
2018-08-10 15:54:09 +02:00
|
|
|
self.assertEquals(LoggingContext.current_context().request, value)
|
2014-10-30 02:21:33 +01:00
|
|
|
|
|
|
|
def test_with_context(self):
|
|
|
|
with LoggingContext() as context_one:
|
2018-01-11 23:40:51 +01:00
|
|
|
context_one.request = "test"
|
2014-10-30 02:21:33 +01:00
|
|
|
self._check_test_key("test")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_sleep(self):
|
2018-06-22 10:37:10 +02:00
|
|
|
clock = Clock(reactor)
|
|
|
|
|
2014-10-30 02:21:33 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def competing_callback():
|
|
|
|
with LoggingContext() as competing_context:
|
2018-01-11 23:40:51 +01:00
|
|
|
competing_context.request = "competing"
|
2018-06-22 10:37:10 +02:00
|
|
|
yield clock.sleep(0)
|
2014-10-30 02:21:33 +01:00
|
|
|
self._check_test_key("competing")
|
|
|
|
|
|
|
|
reactor.callLater(0, competing_callback)
|
|
|
|
|
|
|
|
with LoggingContext() as context_one:
|
2018-01-11 23:40:51 +01:00
|
|
|
context_one.request = "one"
|
2018-06-22 10:37:10 +02:00
|
|
|
yield clock.sleep(0)
|
2014-10-30 02:21:33 +01:00
|
|
|
self._check_test_key("one")
|
2017-03-17 21:56:54 +01:00
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
def _test_run_in_background(self, function):
|
2017-03-17 21:56:54 +01:00
|
|
|
sentinel_context = LoggingContext.current_context()
|
|
|
|
|
|
|
|
callback_completed = [False]
|
|
|
|
|
2019-07-02 20:01:28 +02:00
|
|
|
with LoggingContext() as context_one:
|
2018-01-11 23:40:51 +01:00
|
|
|
context_one.request = "one"
|
2019-07-02 20:01:28 +02:00
|
|
|
|
|
|
|
# fire off function, but don't wait on it.
|
2019-07-03 16:07:04 +02:00
|
|
|
d2 = run_in_background(function)
|
2017-03-17 21:56:54 +01:00
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
def cb(res):
|
|
|
|
callback_completed[0] = True
|
|
|
|
return res
|
2018-08-10 15:54:09 +02:00
|
|
|
|
2019-07-02 20:01:28 +02:00
|
|
|
d2.addCallback(cb)
|
2017-03-17 21:56:54 +01: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()
|
|
|
|
|
|
|
|
def check_logcontext():
|
|
|
|
if not callback_completed[0]:
|
|
|
|
reactor.callLater(0.01, check_logcontext)
|
|
|
|
return
|
|
|
|
|
|
|
|
# make sure that the context was reset before it got thrown back
|
|
|
|
# into the reactor
|
|
|
|
try:
|
2018-08-10 15:54:09 +02:00
|
|
|
self.assertIs(LoggingContext.current_context(), sentinel_context)
|
2017-03-17 21:56:54 +01: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
|
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
def test_run_in_background_with_blocking_fn(self):
|
2017-03-17 21:56:54 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def blocking_function():
|
2018-06-22 10:37:10 +02:00
|
|
|
yield Clock(reactor).sleep(0)
|
2017-03-17 21:56:54 +01:00
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
return self._test_run_in_background(blocking_function)
|
2017-03-17 21:56:54 +01:00
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
def test_run_in_background_with_non_blocking_fn(self):
|
2017-03-17 21:56:54 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def nonblocking_function():
|
2019-07-03 16:07:04 +02:00
|
|
|
with PreserveLoggingContext():
|
2017-03-17 21:56:54 +01:00
|
|
|
yield defer.succeed(None)
|
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
return self._test_run_in_background(nonblocking_function)
|
|
|
|
|
|
|
|
def test_run_in_background_with_chained_deferred(self):
|
|
|
|
# a function which returns a deferred which looks like it has been
|
|
|
|
# called, but is actually paused
|
|
|
|
def testfunc():
|
2019-07-03 16:07:04 +02:00
|
|
|
return make_deferred_yieldable(_chained_deferred_function())
|
2018-05-02 12:46:23 +02:00
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
2017-10-17 11:52:31 +02:00
|
|
|
|
2019-07-02 20:01:28 +02:00
|
|
|
def test_run_in_background_with_coroutine(self):
|
|
|
|
async def testfunc():
|
|
|
|
self._check_test_key("one")
|
|
|
|
d = Clock(reactor).sleep(0)
|
|
|
|
self.assertIs(LoggingContext.current_context(), LoggingContext.sentinel)
|
|
|
|
await d
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
|
|
|
|
|
|
|
def test_run_in_background_with_nonblocking_coroutine(self):
|
|
|
|
async def testfunc():
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
return self._test_run_in_background(testfunc)
|
|
|
|
|
2017-10-17 11:52:31 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_make_deferred_yieldable(self):
|
|
|
|
# a function which retuns an incomplete deferred, but doesn't follow
|
|
|
|
# the synapse rules.
|
|
|
|
def blocking_function():
|
|
|
|
d = defer.Deferred()
|
|
|
|
reactor.callLater(0, d.callback, None)
|
|
|
|
return d
|
|
|
|
|
|
|
|
sentinel_context = LoggingContext.current_context()
|
|
|
|
|
|
|
|
with LoggingContext() as context_one:
|
2018-01-11 23:40:51 +01:00
|
|
|
context_one.request = "one"
|
2017-10-17 11:52:31 +02:00
|
|
|
|
2019-07-03 16:07:04 +02:00
|
|
|
d1 = make_deferred_yieldable(blocking_function())
|
2017-10-17 11:52:31 +02:00
|
|
|
# make sure that the context was reset by make_deferred_yieldable
|
|
|
|
self.assertIs(LoggingContext.current_context(), sentinel_context)
|
|
|
|
|
|
|
|
yield d1
|
|
|
|
|
|
|
|
# now it should be restored
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_make_deferred_yieldable_with_chained_deferreds(self):
|
|
|
|
sentinel_context = LoggingContext.current_context()
|
|
|
|
|
|
|
|
with LoggingContext() as context_one:
|
|
|
|
context_one.request = "one"
|
|
|
|
|
2019-07-03 16:07:04 +02:00
|
|
|
d1 = make_deferred_yieldable(_chained_deferred_function())
|
2018-05-02 12:46:23 +02:00
|
|
|
# make sure that the context was reset by make_deferred_yieldable
|
|
|
|
self.assertIs(LoggingContext.current_context(), sentinel_context)
|
|
|
|
|
|
|
|
yield d1
|
|
|
|
|
|
|
|
# now it should be restored
|
|
|
|
self._check_test_key("one")
|
|
|
|
|
2017-10-17 11:52:31 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_make_deferred_yieldable_on_non_deferred(self):
|
|
|
|
"""Check that make_deferred_yieldable does the right thing when its
|
|
|
|
argument isn't actually a deferred"""
|
|
|
|
|
|
|
|
with LoggingContext() as context_one:
|
2018-01-11 23:40:51 +01:00
|
|
|
context_one.request = "one"
|
2017-10-17 11:52:31 +02:00
|
|
|
|
2019-07-03 16:07:04 +02:00
|
|
|
d1 = make_deferred_yieldable("bum")
|
2017-10-17 11:52:31 +02:00
|
|
|
self._check_test_key("one")
|
|
|
|
|
|
|
|
r = yield d1
|
|
|
|
self.assertEqual(r, "bum")
|
|
|
|
self._check_test_key("one")
|
2018-05-02 12:46:23 +02:00
|
|
|
|
2018-09-27 12:25:34 +02:00
|
|
|
def test_nested_logging_context(self):
|
|
|
|
with LoggingContext(request="foo"):
|
2019-07-03 16:07:04 +02:00
|
|
|
nested_context = nested_logging_context(suffix="bar")
|
2018-09-27 12:25:34 +02:00
|
|
|
self.assertEqual(nested_context.request, "foo-bar")
|
|
|
|
|
2018-05-02 12:46:23 +02: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.
|
|
|
|
def _chained_deferred_function():
|
|
|
|
d = defer.succeed(None)
|
|
|
|
|
|
|
|
def cb(res):
|
|
|
|
d2 = defer.Deferred()
|
|
|
|
reactor.callLater(0, d2.callback, res)
|
|
|
|
return d2
|
2018-08-10 15:54:09 +02:00
|
|
|
|
2018-05-02 12:46:23 +02:00
|
|
|
d.addCallback(cb)
|
|
|
|
return d
|