You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2017/02/27 18:04:53 UTC

[5/6] qpid-proton git commit: PROTON-1403: c example test framework fixes

PROTON-1403: c example test framework fixes

- start broker for each test case
- grep for "listening" in output instead of trying to connect to port
  (faster and avoids confusing "no proton header" errors)


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/105b939f
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/105b939f
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/105b939f

Branch: refs/heads/master
Commit: 105b939f2d361577d23b374a6f85fbf00e20f8da
Parents: a6b4164
Author: Alan Conway <ac...@redhat.com>
Authored: Mon Feb 27 12:53:40 2017 -0500
Committer: Alan Conway <ac...@redhat.com>
Committed: Mon Feb 27 12:53:40 2017 -0500

----------------------------------------------------------------------
 examples/c/proactor/test.py | 48 ++++++++++++++++++++++++++++------------
 examples/exampletest.py     | 40 +++++----------------------------
 2 files changed, 39 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/105b939f/examples/c/proactor/test.py
----------------------------------------------------------------------
diff --git a/examples/c/proactor/test.py b/examples/c/proactor/test.py
index f6a6562..692e4be 100644
--- a/examples/c/proactor/test.py
+++ b/examples/c/proactor/test.py
@@ -29,29 +29,49 @@ def python_cmd(name):
 def receive_expect(n):
     return ''.join('{"sequence"=%s}\n'%i for i in xrange(1, n+1)) + "%s messages received\n"%n
 
-class CExampleTest(BrokerTestCase):
-    broker_exe = ["broker"]
+class Broker(object):
+    def __init__(self, test):
+        self.test = test
+
+    def __enter__(self):
+        with bind0() as sock:
+            self.addr = "127.0.0.1:%s/examples" % (sock.port())
+            self.proc = self.test.proc(["broker", "-a", self.addr])
+            self.proc.wait_re("listening")
+            return self
+
+    def __exit__(self, *args):
+        b = getattr(self, "proc")
+        if b:
+            if b.poll() !=  None: # Broker crashed
+                raise ProcError(b, "broker crash")
+            b.kill()
+
+class CExampleTest(ExampleTestCase):
 
     def test_send_receive(self):
         """Send first then receive"""
-        s = self.proc(["send", "-a", self.addr])
-        self.assertEqual("100 messages sent and acknowledged\n", s.wait_out())
-        r = self.proc(["receive", "-a", self.addr])
-        self.assertEqual(receive_expect(100), r.wait_out())
+        with Broker(self) as b:
+            s = self.proc(["send", "-a", b.addr])
+            self.assertEqual("100 messages sent and acknowledged\n", s.wait_out())
+            r = self.proc(["receive", "-a", b.addr])
+            self.assertEqual(receive_expect(100), r.wait_out())
 
     def test_receive_send(self):
         """Start receiving  first, then send."""
-        r = self.proc(["receive", "-a", self.addr]);
-        s = self.proc(["send", "-a", self.addr]);
-        self.assertEqual("100 messages sent and acknowledged\n", s.wait_out())
-        self.assertEqual(receive_expect(100), r.wait_out())
+        with Broker(self) as b:
+            r = self.proc(["receive", "-a", b.addr]);
+            s = self.proc(["send", "-a", b.addr]);
+            self.assertEqual("100 messages sent and acknowledged\n", s.wait_out())
+            self.assertEqual(receive_expect(100), r.wait_out())
 
     def test_timed_send(self):
         """Send with timed delay"""
-        s = self.proc(["send", "-a", self.addr, "-d100", "-m3"])
-        self.assertEqual("3 messages sent and acknowledged\n", s.wait_out())
-        r = self.proc(["receive", "-a", self.addr, "-m3"])
-        self.assertEqual(receive_expect(3), r.wait_out())
+        with Broker(self) as b:
+            s = self.proc(["send", "-a", b.addr, "-d100", "-m3"])
+            self.assertEqual("3 messages sent and acknowledged\n", s.wait_out())
+            r = self.proc(["receive", "-a", b.addr, "-m3"])
+            self.assertEqual(receive_expect(3), r.wait_out())
 
     def test_send_direct(self):
         """Send to direct server"""

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/105b939f/examples/exampletest.py
----------------------------------------------------------------------
diff --git a/examples/exampletest.py b/examples/exampletest.py
index 546b426..5c820fd 100644
--- a/examples/exampletest.py
+++ b/examples/exampletest.py
@@ -29,10 +29,12 @@ from copy import copy
 import platform
 from os.path import dirname as dirname
 
+DEFAULT_TIMEOUT=10
+
 def bind0():
     """Bind a socket with bind(0) and SO_REUSEADDR to get a free port to listen on"""
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)    
+    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     sock.bind(('', 0))
     return sock
 
@@ -92,7 +94,7 @@ class Proc(Popen):
             pass                # Already exited.
         return self.out
 
-    def wait_out(self, timeout=10, expect=0):
+    def wait_out(self, timeout=DEFAULT_TIMEOUT, expect=0):
         """Wait for process to exit, return output. Raise ProcError  on failure."""
         t = threading.Thread(target=self.wait)
         t.start()
@@ -104,7 +106,7 @@ class Proc(Popen):
             raise ProcError(self)
         return self.out
 
-    def wait_re(self, regexp, timeout=10):
+    def wait_re(self, regexp, timeout=DEFAULT_TIMEOUT):
         """
         Wait for regexp to appear in the output, returns the re.search match result.
         The target process should flush() important output to ensure it appears.
@@ -158,37 +160,5 @@ class ExampleTestCase(TestCase):
         self.procs.append(p)
         return p
 
-class BrokerTestCase(ExampleTestCase):
-    """
-    ExampleTest that starts a broker in setUpClass and kills it in tearDownClass.
-    Subclass must set `broker_exe` class variable with the name of the broker executable.
-    """
-
-    @classmethod
-    def setUpClass(cls):
-        sock = bind0()
-        cls.port = sock.port()
-        cls.addr = "127.0.0.1:%s/examples" % (cls.port)
-        cls.broker = None       # In case Proc throws, create the attribute.
-        cls.broker = Proc(cls.broker_exe + ["-a", cls.addr], bufsize=0)
-        try:
-            cls.broker.wait_re("listening")
-        except Exception, e:
-            cls.broker.kill()
-            raise
-        finally:
-            sock.close()
-
-    @classmethod
-    def tearDownClass(cls):
-        if cls.broker: cls.broker.kill()
-
-    def tearDown(self):
-        b = type(self).broker
-        if b and b.poll() !=  None: # Broker crashed
-            type(self).setUpClass() # Start another for the next test.
-            raise ProcError(b, "broker crash")
-        super(BrokerTestCase, self).tearDown()
-
 if __name__ == "__main__":
     unittest.main()


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org