diff options
author | Gergely Risko <gergely+context@risko.hu> | 2009-09-10 17:18:03 +0300 |
---|---|---|
committer | Gergely Risko <gergely+context@risko.hu> | 2009-09-10 17:18:03 +0300 |
commit | b68f1101692bf09778b608c25a9398900ed0b2db (patch) | |
tree | b6366e03637e984021b6eda1658cc58a4fd98334 | |
parent | 37df5d352538d300585866c0aa5d9da40401c78c (diff) |
libcontextsubscriber/customer-tests: flush support for the customer tests.
3 files changed, 93 insertions, 30 deletions
diff --git a/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py b/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py index 11180b58..d1de94af 100755 --- a/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py +++ b/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py @@ -32,16 +32,9 @@ import os import signal import re import time - import unittest from subprocess import Popen, PIPE - -def timeoutHandler(signum, frame): - raise Exception('tests has been running for too long') - -class Callable: - def __init__(self, anycallable): - self.__call__ = anycallable +from cltool import CLTool class Asynchronous(unittest.TestCase): def startProvider(busname, args): @@ -51,15 +44,7 @@ class Asynchronous(unittest.TestCase): print >>ret.stdin, "info()" ret.stdout.readline().rstrip() return ret - startProvider = Callable(startProvider) - - def wanted(name, type, value): - return "%s = %s:%s" % (name, type, value) - wanted = Callable(wanted) - - def wantedUnknown(name): - return "%s is Unknown" % (name) - wantedUnknown = Callable(wantedUnknown) + startProvider = staticmethod(startProvider) #SetUp def setUp(self): @@ -97,20 +82,19 @@ class Asynchronous(unittest.TestCase): """ # check the fast property - self.context_client = Popen(["context-listen", "test.fast", "test.slow"], - stdin=PIPE, stdout=PIPE, stderr=PIPE) + self.context_client = CLTool("context-listen", "test.fast", "test.slow") - got = self.context_client.stdout.readline().rstrip() - self.assertEqual(got, - self.wanted("test.fast", "int", "42"), - "Bad value for the fast property") + self.assert_(self.context_client.expect(CLTool.STDOUT, + CLTool.wanted("test.fast", "int", "42"), + 1), # timeout == 1 second + "Bad value for the fast property, wanted 42, communication:") fast_time = time.time() # check the slow property - got = self.context_client.stdout.readline().rstrip() - self.assertEqual(got, - self.wanted("test.slow", "int", "42"), - "Bad value for the slow property") + self.assert_(self.context_client.expect(CLTool.STDOUT, + CLTool.wanted("test.slow", "int", "42"), + 10), # timeout == 10 second max, but 3 is enough usually + "Bad value for the slow property, wanted 42, communication:") slow_time = time.time() self.assert_(slow_time - fast_time > 2.0, @@ -129,9 +113,6 @@ def runTests(): result = unittest.TextTestRunner(verbosity=2).run(suiteInstallation) return len(result.errors + result.failures) - if __name__ == "__main__": sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) - signal.signal(signal.SIGALRM, timeoutHandler) - signal.alarm(10) sys.exit(runTests()) diff --git a/libcontextsubscriber/customer-tests/common/cltool.py b/libcontextsubscriber/customer-tests/common/cltool.py new file mode 100644 index 00000000..c80ce355 --- /dev/null +++ b/libcontextsubscriber/customer-tests/common/cltool.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +## +## Copyright (C) 2008, 2009 Nokia. All rights reserved. +## +## Contact: Marius Vollmer <marius.vollmer@nokia.com> +## +## This library is free software; you can redistribute it and/or +## modify it under the terms of the GNU Lesser General Public License +## version 2.1 as published by the Free Software Foundation. +## +## This library 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 this library; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +## 02110-1301 USA + +import re +import time +from subprocess import Popen, PIPE + +class CLTool: + STDOUT = 1 + STDERR = 2 + def __init__(self, *cline): + self.__process = Popen(cline, stdin=PIPE, stdout=PIPE, stderr=PIPE) + self.__io = [] + + def send(self, string): + self.__io.append((0, string)) + print >>self.__process.stdin, string + + def expect(self, fileno, exp_str, timeout): + stream = 0 + if fileno == self.STDOUT: stream = self.__process.stdout + if fileno == self.STDERR: stream = self.__process.stderr + if stream == 0: return False + + print >>self.__process.stdin, "flush" + cur_str = "" + start_time = time.time() + while True: + line = stream.readline().rstrip() + if line == "FLUSHED": + if re.match(exp_str, cur_str): + return True + else: + time.sleep(0.1) + if time.time() - start_time > timeout: + self.printio() + print "Expected:", exp_str + print "Received before the timeout:\n", cur_str + return False + print >>self.__process.stdin, "flush" + else: + cur_str += line + "\n" + self.__io.append((fileno, line)) + + def printio(self): + print + print '----------------------------------------------------' + for line in self.__io: + if line[0] == 0: + print "[IN] <<<", line[1] + if line[0] == 1: + print "[OU] >>>", line[1] + if line[0] == 2: + print "[ER] >>>", line[1] + print '----------------------------------------------------' + + def wanted(name, type, value): + return "%s = %s:%s$" % (name, type, value) + wanted = staticmethod(wanted) + + def wantedUnknown(name): + return "%s is Unknown" % (name) + wantedUnknown = staticmethod(wantedUnknown) diff --git a/libcontextsubscriber/customer-tests/runTests.sh b/libcontextsubscriber/customer-tests/runTests.sh index 82efc04f..ae24a623 100755 --- a/libcontextsubscriber/customer-tests/runTests.sh +++ b/libcontextsubscriber/customer-tests/runTests.sh @@ -1,6 +1,8 @@ #!/bin/bash +cd $(dirname $0) DIRS="commander subscription asynchronicity registry" +export PYTHONPATH="`pwd`/common/" if pkg-config contextprovider-1.0 || [ -e ../../libcontextprovider/src/.libs/libcontextprovider.so ] then |