summary refs log tree commit diff
path: root/tests/util/test_log_context.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-11-19 18:03:57 +0000
committerErik Johnston <erik@matrix.org>2014-11-19 18:03:57 +0000
commit19977b465913c3cb263d88884e16f9dc13f2a05e (patch)
tree6015e4155502d34fd8aa35fc32094d8c29981a0a /tests/util/test_log_context.py
parentMerge branch 'release-v0.4.2' of github.com:matrix-org/synapse (diff)
parentMerge branch 'develop' of github.com:matrix-org/synapse into release-v0.5.0 (diff)
downloadsynapse-19977b465913c3cb263d88884e16f9dc13f2a05e.tar.xz
Merge branch 'release-v0.5.0' of github.com:matrix-org/synapse v0.5.0
Diffstat (limited to 'tests/util/test_log_context.py')
-rw-r--r--tests/util/test_log_context.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/util/test_log_context.py b/tests/util/test_log_context.py
new file mode 100644
index 0000000000..efa0f28bad
--- /dev/null
+++ b/tests/util/test_log_context.py
@@ -0,0 +1,43 @@
+from twisted.internet import defer
+from twisted.internet import reactor
+from .. import unittest
+
+from synapse.util.async import sleep
+from synapse.util.logcontext import LoggingContext
+
+class LoggingContextTestCase(unittest.TestCase):
+
+    def _check_test_key(self, value):
+        self.assertEquals(
+            LoggingContext.current_context().test_key, value
+        )
+
+    def test_with_context(self):
+        with LoggingContext() as context_one:
+            context_one.test_key = "test"
+            self._check_test_key("test")
+
+    def test_chaining(self):
+        with LoggingContext() as context_one:
+            context_one.test_key = "one"
+            with LoggingContext() as context_two:
+                self._check_test_key("one")
+                context_two.test_key = "two"
+                self._check_test_key("two")
+            self._check_test_key("one")
+
+    @defer.inlineCallbacks
+    def test_sleep(self):
+        @defer.inlineCallbacks
+        def competing_callback():
+            with LoggingContext() as competing_context:
+                competing_context.test_key = "competing"
+                yield sleep(0)
+                self._check_test_key("competing")
+
+        reactor.callLater(0, competing_callback)
+
+        with LoggingContext() as context_one:
+            context_one.test_key = "one"
+            yield sleep(0)
+            self._check_test_key("one")