diff options
author | Erik Johnston <erik@matrix.org> | 2015-05-21 14:58:47 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2015-05-21 14:58:47 +0100 |
commit | c2633907c530d018254c0cca8d7ce8336c380fc2 (patch) | |
tree | c55895df895f013398702154ed85940e19ab2d0e /synapse/util | |
parent | Fix _get_backfill_events to return events in the correct order (diff) | |
parent | Merge branch 'develop' of github.com:matrix-org/synapse into erikj/join_perf (diff) | |
download | synapse-c2633907c530d018254c0cca8d7ce8336c380fc2.tar.xz |
Merge branch 'erikj/join_perf' of github.com:matrix-org/synapse into erikj/backfill_fixes
Diffstat (limited to 'synapse/util')
-rw-r--r-- | synapse/util/__init__.py | 36 |
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() |