aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGergely Risko <gergely+context@risko.hu>2009-09-29 15:26:13 +0300
committerMarja Hassinen <ext-marja.2.hassinen@nokia.com>2009-09-29 17:25:26 +0300
commit4ba1e5c332ac0e0fb6a52249d163988388924d31 (patch)
treee3d6bc1cea32faee8684e5258d3eebece02109c1
parentcda81f2d8a84e59b6256d69a98cac096232e7ba6 (diff)
CLTool: using async read instead of the flush hack, new feature: comments.
Signed-off-by: Marja Hassinen <ext-marja.2.hassinen@nokia.com>
-rwxr-xr-xlibcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py16
-rw-r--r--python/ContextKit/cltool.py48
2 files changed, 42 insertions, 22 deletions
diff --git a/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py b/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py
index 9807e9f8..e603bd26 100755
--- a/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py
+++ b/libcontextsubscriber/customer-tests/asynchronicity/asynchronicity.py
@@ -57,8 +57,7 @@ class Asynchronous(unittest.TestCase):
["int","test.fast","42"])
print >>self.flexiprovider_slow.stdin, "import time ; time.sleep(3)"
-
- def testCommanderFunctionality(self):
+ def testAsynchronicity(self):
"""
Description
This test verifies that the asynchronicity of the subscriber
@@ -82,23 +81,26 @@ class Asynchronous(unittest.TestCase):
"""
# check the fast property
- self.context_client = CLTool("context-listen", "test.fast", "test.slow")
+ context_client = CLTool("context-listen", "test.fast", "test.slow")
- self.assert_(self.context_client.expect(CLTool.STDOUT,
+ self.assert_(context_client.expect(CLTool.STDOUT,
CLTool.wanted("test.fast", "int", "42"),
1), # timeout == 1 second
- "Bad value for the fast property, wanted 42, communication:")
+ "Bad value for the fast property, wanted 42")
fast_time = time.time()
+ context_client.comment("Fast property arrived with good value at: " + str(fast_time))
# check the slow property
- self.assert_(self.context_client.expect(CLTool.STDOUT,
+ self.assert_(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:")
+ "Bad value for the slow property, wanted 42")
slow_time = time.time()
+ context_client.comment("Slow property arrived with good value at: " + str(slow_time))
self.assert_(slow_time - fast_time > 2.0,
"The arrival time of the fast and slow property is not far enough from each other")
+ #context_client.printio()
#TearDown
def tearDown(self):
diff --git a/python/ContextKit/cltool.py b/python/ContextKit/cltool.py
index c80ce355..b1dec437 100644
--- a/python/ContextKit/cltool.py
+++ b/python/ContextKit/cltool.py
@@ -21,6 +21,8 @@
import re
import time
from subprocess import Popen, PIPE
+from fcntl import fcntl, F_GETFL, F_SETFL
+from os import O_NONBLOCK
class CLTool:
STDOUT = 1
@@ -32,6 +34,7 @@ class CLTool:
def send(self, string):
self.__io.append((0, string))
print >>self.__process.stdin, string
+ self.__process.stdin.flush()
def expect(self, fileno, exp_str, timeout):
stream = 0
@@ -39,25 +42,38 @@ class CLTool:
if fileno == self.STDERR: stream = self.__process.stderr
if stream == 0: return False
- print >>self.__process.stdin, "flush"
+ # set the stream to nonblocking
+ fd = stream.fileno()
+ flags = fcntl(fd, F_GETFL)
+ fcntl(fd, F_SETFL, flags | O_NONBLOCK)
+
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:
+ try:
+ line = stream.readline()
+ if not line: # eof
+ self.__io.append((fileno, "--------- STREAM UNEXPECTEDLY CLOSED -----------"))
+ self.printio()
+ print "Expected:", exp_str
+ print "Received before the stream got closed:\n", cur_str
+ return False
+ line = line.rstrip()
cur_str += line + "\n"
self.__io.append((fileno, line))
+ except:
+ time.sleep(0.1) # no data were available
+ if re.match(exp_str, cur_str):
+ return True
+ else:
+ if time.time() - start_time > timeout: # timeout
+ self.printio()
+ print "Expected:", exp_str
+ print "Received before the timeout:\n", cur_str
+ return False
+
+ def comment(self, st):
+ self.__io.append((-1, st))
def printio(self):
print
@@ -69,10 +85,12 @@ class CLTool:
print "[OU] >>>", line[1]
if line[0] == 2:
print "[ER] >>>", line[1]
+ if line[0] == -1:
+ print "[CM] ===", line[1]
print '----------------------------------------------------'
def wanted(name, type, value):
- return "%s = %s:%s$" % (name, type, value)
+ return "^%s = %s:%s$" % (name, type, value)
wanted = staticmethod(wanted)
def wantedUnknown(name):