viff

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 diff
     1.1 --- a/viff/test/test_util.py	Tue Oct 14 15:06:42 2008 +0200
     1.2 +++ b/viff/test/test_util.py	Tue Oct 14 15:06:43 2008 +0200
     1.3 @@ -15,5 +15,153 @@
     1.4  # You should have received a copy of the GNU Lesser General Public
     1.5  # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
     1.6  
     1.7 +"""Tests for viff.util."""
     1.8 +
     1.9 +from viff.util import deep_wait
    1.10 +
    1.11 +from twisted.trial.unittest import TestCase
    1.12 +from twisted.internet.defer import Deferred, gatherResults, succeed
    1.13 +
    1.14  #: Declare doctests for Trial.
    1.15  __doctests__ = ['viff.util']
    1.16 +
    1.17 +
    1.18 +class DeepWaitTest(TestCase):
    1.19 +    """Tests for :func:`viff.util.deep_wait`."""
    1.20 +
    1.21 +    def setUp(self):
    1.22 +        self.calls = []
    1.23 +
    1.24 +    def test_trivial_wait(self):
    1.25 +        w = deep_wait("not a Deferred")
    1.26 +        w.addCallback(lambda _: self.calls.append("w"))
    1.27 +        self.assertIn("w", self.calls)
    1.28 +
    1.29 +    def test_simple_wait(self):
    1.30 +        a = Deferred()
    1.31 +        a.addCallback(self.calls.append)
    1.32 +
    1.33 +        w = deep_wait(a)
    1.34 +        w.addCallback(lambda _: self.calls.append("w"))
    1.35 +
    1.36 +        self.assertNotIn("w", self.calls)
    1.37 +        a.callback("a")
    1.38 +        self.assertIn("w", self.calls)
    1.39 +
    1.40 +    def test_tuple_wait(self):
    1.41 +        a = Deferred()
    1.42 +        b = Deferred()
    1.43 +
    1.44 +        a.addCallback(self.calls.append)
    1.45 +        b.addCallback(self.calls.append)
    1.46 +
    1.47 +        w = deep_wait((a, 123, b))
    1.48 +        w.addCallback(lambda _: self.calls.append("w"))
    1.49 +
    1.50 +        self.assertNotIn("w", self.calls)
    1.51 +        a.callback("a")
    1.52 +        self.assertNotIn("w", self.calls)
    1.53 +        b.callback("b")
    1.54 +        self.assertIn("w", self.calls)
    1.55 +
    1.56 +    def test_list_wait(self):
    1.57 +        a = Deferred()
    1.58 +        b = Deferred()
    1.59 +
    1.60 +        a.addCallback(self.calls.append)
    1.61 +        b.addCallback(self.calls.append)
    1.62 +
    1.63 +        w = deep_wait([a, 123, b])
    1.64 +        w.addCallback(lambda _: self.calls.append("w"))
    1.65 +
    1.66 +        self.assertNotIn("w", self.calls)
    1.67 +        a.callback("a")
    1.68 +        self.assertNotIn("w", self.calls)
    1.69 +        b.callback("b")
    1.70 +        self.assertIn("w", self.calls)
    1.71 +
    1.72 +    def test_deep_wait(self):
    1.73 +        a = Deferred()
    1.74 +        b = Deferred()
    1.75 +
    1.76 +        def return_b(_):
    1.77 +            """Callbacks which return a Deferred."""
    1.78 +            self.calls.append("return_b")
    1.79 +            return b
    1.80 +        
    1.81 +        a.addCallback(self.calls.append)
    1.82 +        a.addCallback(return_b)
    1.83 +
    1.84 +        w = deep_wait(a)
    1.85 +        w.addCallback(lambda _: self.calls.append("w"))
    1.86 +
    1.87 +        self.assertNotIn("a", self.calls)
    1.88 +        a.callback("a")
    1.89 +        self.assertIn("a", self.calls)
    1.90 +        self.assertIn("return_b", self.calls)
    1.91 +        self.assertNotIn("w", self.calls)
    1.92 +        self.assertNotIn("b", self.calls)
    1.93 +
    1.94 +        b.callback("b")
    1.95 +        self.assertIn("w", self.calls)
    1.96 +
    1.97 +    def test_mixed_deep_wait(self):
    1.98 +        a = Deferred()
    1.99 +        b = Deferred()
   1.100 +
   1.101 +        def return_mix(_):
   1.102 +            """Callbacks which return a Deferred and an integer."""
   1.103 +            self.calls.append("return_mix")
   1.104 +            return (b, 42)
   1.105 +        
   1.106 +        a.addCallback(self.calls.append)
   1.107 +        a.addCallback(return_mix)
   1.108 +
   1.109 +        w = deep_wait(a)
   1.110 +        w.addCallback(lambda _: self.calls.append("w"))
   1.111 +
   1.112 +        self.assertNotIn("a", self.calls)
   1.113 +        a.callback("a")
   1.114 +        self.assertIn("a", self.calls)
   1.115 +        self.assertIn("return_mix", self.calls)
   1.116 +        self.assertNotIn("w", self.calls)
   1.117 +
   1.118 +        b.callback("b")
   1.119 +        self.assertIn("w", self.calls)
   1.120 +
   1.121 +    def test_complex_deep_wait(self):
   1.122 +        a = Deferred()
   1.123 +        b = Deferred()
   1.124 +        c = Deferred()
   1.125 +        d = Deferred()
   1.126 +
   1.127 +        a.addCallback(self.calls.append)
   1.128 +        b.addCallback(self.calls.append)
   1.129 +        c.addCallback(self.calls.append)
   1.130 +        d.addCallback(self.calls.append)
   1.131 +
   1.132 +        def return_b(_):
   1.133 +            self.calls.append("return_b")
   1.134 +            return (b, 42)
   1.135 +
   1.136 +        def return_c_d(_):
   1.137 +            self.calls.append("return_c")
   1.138 +            return [(1, 2), "testing", [c, True], (d, 10)]
   1.139 +
   1.140 +        a.addCallback(return_b)
   1.141 +        b.addCallback(return_c_d)
   1.142 +
   1.143 +        w = deep_wait(a)
   1.144 +        w.addCallback(lambda _: self.calls.append("w"))
   1.145 +
   1.146 +        a.callback("a")
   1.147 +        self.assertNotIn("w", self.calls)
   1.148 +
   1.149 +        c.callback("c")
   1.150 +        self.assertNotIn("w", self.calls)
   1.151 +
   1.152 +        b.callback("b")
   1.153 +        self.assertNotIn("w", self.calls)
   1.154 +
   1.155 +        d.callback("d")
   1.156 +        self.assertIn("w", self.calls)