test_log_context.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from twisted.internet import defer
  2. from twisted.internet import reactor
  3. from .. import unittest
  4. from synapse.util.async import sleep
  5. from synapse.util.logcontext import LoggingContext
  6. class LoggingContextTestCase(unittest.TestCase):
  7. def _check_test_key(self, value):
  8. self.assertEquals(
  9. LoggingContext.current_context().test_key, value
  10. )
  11. def test_with_context(self):
  12. with LoggingContext() as context_one:
  13. context_one.test_key = "test"
  14. self._check_test_key("test")
  15. @defer.inlineCallbacks
  16. def test_sleep(self):
  17. @defer.inlineCallbacks
  18. def competing_callback():
  19. with LoggingContext() as competing_context:
  20. competing_context.test_key = "competing"
  21. yield sleep(0)
  22. self._check_test_key("competing")
  23. reactor.callLater(0, competing_callback)
  24. with LoggingContext() as context_one:
  25. context_one.test_key = "one"
  26. yield sleep(0)
  27. self._check_test_key("one")