changeset 999:23a883d5a4b3

Test cases for deep_wait.
author Martin Geisler <mg@daimi.au.dk>
date Tue, 14 Oct 2008 15:06:43 +0200
parents 42eb1cf66ac1
children 7546e47ce876
files viff/test/test_util.py
diffstat 1 files changed, 148 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/viff/test/test_util.py	Tue Oct 14 15:06:42 2008 +0200
+++ b/viff/test/test_util.py	Tue Oct 14 15:06:43 2008 +0200
@@ -15,5 +15,153 @@
 # You should have received a copy of the GNU Lesser General Public
 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
 
+"""Tests for viff.util."""
+
+from viff.util import deep_wait
+
+from twisted.trial.unittest import TestCase
+from twisted.internet.defer import Deferred, gatherResults, succeed
+
 #: Declare doctests for Trial.
 __doctests__ = ['viff.util']
+
+
+class DeepWaitTest(TestCase):
+    """Tests for :func:`viff.util.deep_wait`."""
+
+    def setUp(self):
+        self.calls = []
+
+    def test_trivial_wait(self):
+        w = deep_wait("not a Deferred")
+        w.addCallback(lambda _: self.calls.append("w"))
+        self.assertIn("w", self.calls)
+
+    def test_simple_wait(self):
+        a = Deferred()
+        a.addCallback(self.calls.append)
+
+        w = deep_wait(a)
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        self.assertNotIn("w", self.calls)
+        a.callback("a")
+        self.assertIn("w", self.calls)
+
+    def test_tuple_wait(self):
+        a = Deferred()
+        b = Deferred()
+
+        a.addCallback(self.calls.append)
+        b.addCallback(self.calls.append)
+
+        w = deep_wait((a, 123, b))
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        self.assertNotIn("w", self.calls)
+        a.callback("a")
+        self.assertNotIn("w", self.calls)
+        b.callback("b")
+        self.assertIn("w", self.calls)
+
+    def test_list_wait(self):
+        a = Deferred()
+        b = Deferred()
+
+        a.addCallback(self.calls.append)
+        b.addCallback(self.calls.append)
+
+        w = deep_wait([a, 123, b])
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        self.assertNotIn("w", self.calls)
+        a.callback("a")
+        self.assertNotIn("w", self.calls)
+        b.callback("b")
+        self.assertIn("w", self.calls)
+
+    def test_deep_wait(self):
+        a = Deferred()
+        b = Deferred()
+
+        def return_b(_):
+            """Callbacks which return a Deferred."""
+            self.calls.append("return_b")
+            return b
+        
+        a.addCallback(self.calls.append)
+        a.addCallback(return_b)
+
+        w = deep_wait(a)
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        self.assertNotIn("a", self.calls)
+        a.callback("a")
+        self.assertIn("a", self.calls)
+        self.assertIn("return_b", self.calls)
+        self.assertNotIn("w", self.calls)
+        self.assertNotIn("b", self.calls)
+
+        b.callback("b")
+        self.assertIn("w", self.calls)
+
+    def test_mixed_deep_wait(self):
+        a = Deferred()
+        b = Deferred()
+
+        def return_mix(_):
+            """Callbacks which return a Deferred and an integer."""
+            self.calls.append("return_mix")
+            return (b, 42)
+        
+        a.addCallback(self.calls.append)
+        a.addCallback(return_mix)
+
+        w = deep_wait(a)
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        self.assertNotIn("a", self.calls)
+        a.callback("a")
+        self.assertIn("a", self.calls)
+        self.assertIn("return_mix", self.calls)
+        self.assertNotIn("w", self.calls)
+
+        b.callback("b")
+        self.assertIn("w", self.calls)
+
+    def test_complex_deep_wait(self):
+        a = Deferred()
+        b = Deferred()
+        c = Deferred()
+        d = Deferred()
+
+        a.addCallback(self.calls.append)
+        b.addCallback(self.calls.append)
+        c.addCallback(self.calls.append)
+        d.addCallback(self.calls.append)
+
+        def return_b(_):
+            self.calls.append("return_b")
+            return (b, 42)
+
+        def return_c_d(_):
+            self.calls.append("return_c")
+            return [(1, 2), "testing", [c, True], (d, 10)]
+
+        a.addCallback(return_b)
+        b.addCallback(return_c_d)
+
+        w = deep_wait(a)
+        w.addCallback(lambda _: self.calls.append("w"))
+
+        a.callback("a")
+        self.assertNotIn("w", self.calls)
+
+        c.callback("c")
+        self.assertNotIn("w", self.calls)
+
+        b.callback("b")
+        self.assertNotIn("w", self.calls)
+
+        d.callback("d")
+        self.assertIn("w", self.calls)