changeset 843:e0ba3202ebd2

Test case for apps/millionaires.py. Now that applications can be started and stopped reliably, we can begin executing them in unit tests.
author Martin Geisler <mg@daimi.au.dk>
date Mon, 21 Jul 2008 23:36:31 +0200
parents 0e9ab27bf855
children 4e84893a1725
files viff/test/test_apps.py
diffstat 1 files changed, 82 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/viff/test/test_apps.py	Mon Jul 21 23:36:31 2008 +0200
@@ -0,0 +1,82 @@
+# Copyright 2008 VIFF Development Team.
+#
+# This file is part of VIFF, the Virtual Ideal Functionality Framework.
+#
+# VIFF is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License (LGPL) as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# VIFF is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
+
+"""Test of apps/millionaires.py example."""
+
+import re
+import os
+import os.path as path
+
+from twisted.trial.unittest import TestCase
+from twisted.internet import reactor
+from twisted.internet.utils import getProcessOutput
+from twisted.internet.defer import Deferred, gatherResults
+
+def execute(executable, *args):
+    """Execute *executable* when the reactor is started."""
+    d = Deferred()
+    def run():
+        p = getProcessOutput(path.abspath(executable),
+                             args=args, env=os.environ)
+        p.chainDeferred(d)
+    reactor.callLater(0, run)
+    return d
+
+class AppsTest(TestCase):
+    """Test examples in apps/ directory."""
+
+    def setUp(self):
+        """Switch to apps/ directory and generate config files."""
+        self.oldcwd = os.getcwd()
+        os.chdir(path.join(path.dirname(__file__), '..', '..', 'apps'))
+
+        p = execute('generate-config-files.py', '-n', '3', '-t', '1',
+                    'localhost:10000', 'localhost:20000', 'localhost:30000')
+        return p
+
+    def tearDown(self):
+        """Switch back to the old working directory."""
+        os.chdir(self.oldcwd)
+
+    def test_millionaires(self):
+        """Test apps/millionaires.py."""
+
+        def check_outputs(outputs):
+            millions = []
+            for i, o in enumerate(outputs):
+                self.assertIn("I am Millionaire %d" % (i+1) , o)
+
+                match = re.search(r"I am worth (\d+) millions", o)
+                millions.append((int(match.group(1)), i))
+            millions.sort()
+
+            for i, o in enumerate(outputs):
+                lines = []
+                for m, j in millions:
+                    lines.append("  Millionaire %d" % (j+1))
+                    if i == j:
+                        lines[-1] += " (%d millions)" % m
+                subtext = "\n".join(lines)
+                self.assertIn(subtext, o)
+
+        m1 = execute('millionaires.py', 'player-1.ini')
+        m2 = execute('millionaires.py', 'player-2.ini')
+        m3 = execute('millionaires.py', 'player-3.ini')
+        
+        result = gatherResults([m1, m2, m3])
+        result.addCallback(check_outputs)
+        return result