summary refs log tree commit diff
path: root/tests/utils.py
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2014-08-31 16:08:20 +0100
committerMark Haines <mark.haines@matrix.org>2014-08-31 16:08:20 +0100
commit3eb45eba0e387dacb67158efe323f19731799c79 (patch)
tree79d942de91b8c9a940e787da9b4a329047139a80 /tests/utils.py
parentAdd store for server certificates and keys (diff)
parentAdd config tree to synapse. Add support for reading config from a file (diff)
downloadsynapse-3eb45eba0e387dacb67158efe323f19731799c79.tar.xz
Merge branch 'develop' into server2server_tls
Diffstat (limited to 'tests/utils.py')
-rw-r--r--tests/utils.py52
1 files changed, 46 insertions, 6 deletions
diff --git a/tests/utils.py b/tests/utils.py
index b32d5ef356..37b759febc 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -21,7 +21,7 @@ from synapse.api.events.room import (
     RoomMemberEvent, MessageEvent
 )
 
-from twisted.internet import defer
+from twisted.internet import defer, reactor
 
 from collections import namedtuple
 from mock import patch, Mock
@@ -248,8 +248,11 @@ class DeferredMockCallable(object):
 
     def __init__(self):
         self.expectations = []
+        self.calls = []
 
     def __call__(self, *args, **kwargs):
+        self.calls.append((args, kwargs))
+
         if not self.expectations:
             raise ValueError("%r has no pending calls to handle call(%s)" % (
                 self, _format_call(args, kwargs))
@@ -260,15 +263,52 @@ class DeferredMockCallable(object):
                 d.callback(None)
                 return result
 
-        raise AssertionError("Was not expecting call(%s)" %
+        failure = AssertionError("Was not expecting call(%s)" %
             _format_call(args, kwargs)
         )
 
+        for _, _, d in self.expectations:
+            try:
+                d.errback(failure)
+            except:
+                pass
+
+        raise failure
+
     def expect_call_and_return(self, call, result):
         self.expectations.append((call, result, defer.Deferred()))
 
     @defer.inlineCallbacks
-    def await_calls(self):
-        while self.expectations:
-            (_, _, d) = self.expectations.pop(0)
-            yield d
+    def await_calls(self, timeout=1000):
+        deferred = defer.DeferredList(
+            [d for _, _, d in self.expectations],
+            fireOnOneErrback=True
+        )
+
+        timer = reactor.callLater(
+            timeout/1000,
+            deferred.errback,
+            AssertionError(
+                "%d pending calls left: %s"% (
+                    len([e for e in self.expectations if not e[2].called]),
+                    [e for e in self.expectations if not e[2].called]
+                )
+            )
+        )
+
+        yield deferred
+
+        timer.cancel()
+
+        self.calls = []
+
+    def assert_had_no_calls(self):
+        if self.calls:
+            calls = self.calls
+            self.calls = []
+
+            raise AssertionError("Expected not to received any calls, got:\n" +
+                "\n".join([
+                    "call(%s)" % _format_call(c[0], c[1]) for c in calls
+                ])
+            )