summary refs log tree commit diff
path: root/synapse/util
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-05-21 14:54:52 +0100
committerErik Johnston <erik@matrix.org>2015-05-21 14:54:52 +0100
commitebfdd2eb5bd9c27ec25b365c544a458b4e92f157 (patch)
tree1a6d7abe69139aa3864af6ccbd0689c494c278b7 /synapse/util
parentMerge branch 'develop' of github.com:matrix-org/synapse into erikj/join_perf (diff)
parentMerge pull request #155 from matrix-org/erikj/perf (diff)
downloadsynapse-ebfdd2eb5bd9c27ec25b365c544a458b4e92f157.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/join_perf
Diffstat (limited to 'synapse/util')
-rw-r--r--synapse/util/__init__.py36
1 files changed, 32 insertions, 4 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index c1a16b639a..260714ccc2 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -29,6 +29,34 @@ def unwrapFirstError(failure):
     return failure.value.subFailure
 
 
+def unwrap_deferred(d):
+    """Given a deferred that we know has completed, return its value or raise
+    the failure as an exception
+    """
+    if not d.called:
+        raise RuntimeError("deferred has not finished")
+
+    res = []
+
+    def f(r):
+        res.append(r)
+        return r
+    d.addCallback(f)
+
+    if res:
+        return res[0]
+
+    def f(r):
+        res.append(r)
+        return r
+    d.addErrback(f)
+
+    if res:
+        res[0].raiseException()
+    else:
+        raise RuntimeError("deferred did not call callbacks")
+
+
 class Clock(object):
     """A small utility that obtains current time-of-day so that time may be
     mocked during unit-tests.
@@ -52,16 +80,16 @@ class Clock(object):
     def stop_looping_call(self, loop):
         loop.stop()
 
-    def call_later(self, delay, callback):
+    def call_later(self, delay, callback, *args, **kwargs):
         current_context = LoggingContext.current_context()
 
-        def wrapped_callback():
+        def wrapped_callback(*args, **kwargs):
             with PreserveLoggingContext():
                 LoggingContext.thread_local.current_context = current_context
-                callback()
+                callback(*args, **kwargs)
 
         with PreserveLoggingContext():
-            return reactor.callLater(delay, wrapped_callback)
+            return reactor.callLater(delay, wrapped_callback, *args, **kwargs)
 
     def cancel_call_later(self, timer):
         timer.cancel()