viff

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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/viff/test/test_apps.py	Mon Jul 21 23:36:31 2008 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +# Copyright 2008 VIFF Development Team.
     1.5 +#
     1.6 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.7 +#
     1.8 +# VIFF is free software: you can redistribute it and/or modify it
     1.9 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.10 +# published by the Free Software Foundation, either version 3 of the
    1.11 +# License, or (at your option) any later version.
    1.12 +#
    1.13 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.14 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.15 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.16 +# Public License for more details.
    1.17 +#
    1.18 +# You should have received a copy of the GNU Lesser General Public
    1.19 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.20 +
    1.21 +"""Test of apps/millionaires.py example."""
    1.22 +
    1.23 +import re
    1.24 +import os
    1.25 +import os.path as path
    1.26 +
    1.27 +from twisted.trial.unittest import TestCase
    1.28 +from twisted.internet import reactor
    1.29 +from twisted.internet.utils import getProcessOutput
    1.30 +from twisted.internet.defer import Deferred, gatherResults
    1.31 +
    1.32 +def execute(executable, *args):
    1.33 +    """Execute *executable* when the reactor is started."""
    1.34 +    d = Deferred()
    1.35 +    def run():
    1.36 +        p = getProcessOutput(path.abspath(executable),
    1.37 +                             args=args, env=os.environ)
    1.38 +        p.chainDeferred(d)
    1.39 +    reactor.callLater(0, run)
    1.40 +    return d
    1.41 +
    1.42 +class AppsTest(TestCase):
    1.43 +    """Test examples in apps/ directory."""
    1.44 +
    1.45 +    def setUp(self):
    1.46 +        """Switch to apps/ directory and generate config files."""
    1.47 +        self.oldcwd = os.getcwd()
    1.48 +        os.chdir(path.join(path.dirname(__file__), '..', '..', 'apps'))
    1.49 +
    1.50 +        p = execute('generate-config-files.py', '-n', '3', '-t', '1',
    1.51 +                    'localhost:10000', 'localhost:20000', 'localhost:30000')
    1.52 +        return p
    1.53 +
    1.54 +    def tearDown(self):
    1.55 +        """Switch back to the old working directory."""
    1.56 +        os.chdir(self.oldcwd)
    1.57 +
    1.58 +    def test_millionaires(self):
    1.59 +        """Test apps/millionaires.py."""
    1.60 +
    1.61 +        def check_outputs(outputs):
    1.62 +            millions = []
    1.63 +            for i, o in enumerate(outputs):
    1.64 +                self.assertIn("I am Millionaire %d" % (i+1) , o)
    1.65 +
    1.66 +                match = re.search(r"I am worth (\d+) millions", o)
    1.67 +                millions.append((int(match.group(1)), i))
    1.68 +            millions.sort()
    1.69 +
    1.70 +            for i, o in enumerate(outputs):
    1.71 +                lines = []
    1.72 +                for m, j in millions:
    1.73 +                    lines.append("  Millionaire %d" % (j+1))
    1.74 +                    if i == j:
    1.75 +                        lines[-1] += " (%d millions)" % m
    1.76 +                subtext = "\n".join(lines)
    1.77 +                self.assertIn(subtext, o)
    1.78 +
    1.79 +        m1 = execute('millionaires.py', 'player-1.ini')
    1.80 +        m2 = execute('millionaires.py', 'player-2.ini')
    1.81 +        m3 = execute('millionaires.py', 'player-3.ini')
    1.82 +        
    1.83 +        result = gatherResults([m1, m2, m3])
    1.84 +        result.addCallback(check_outputs)
    1.85 +        return result