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 2018/07/06 16:59:14 UTC

[02/11] qpid-proton git commit: PROTON-1798: [c, cpp] update example drivers to work with python 3

PROTON-1798: [c,cpp] update example drivers to work with python 3


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

Branch: refs/heads/master
Commit: b41f60d46cb81909c349ef705c0b5ccccf402231
Parents: e2858e6
Author: Alan Conway <ac...@redhat.com>
Authored: Thu Jul 5 14:25:40 2018 -0400
Committer: Alan Conway <ac...@redhat.com>
Committed: Fri Jul 6 12:20:32 2018 -0400

----------------------------------------------------------------------
 c/examples/example_test.py   | 78 +++++++++++++++++++---------------
 c/tests/fdlimit.py           |  2 +-
 cpp/examples/example_test.py | 88 ++++++++++++++++++++++-----------------
 3 files changed, 94 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b41f60d4/c/examples/example_test.py
----------------------------------------------------------------------
diff --git a/c/examples/example_test.py b/c/examples/example_test.py
index f7aa911..2cf87d8 100644
--- a/c/examples/example_test.py
+++ b/c/examples/example_test.py
@@ -21,7 +21,31 @@
 # Example executables must be in PATH
 
 import unittest, sys, time, re
-from subprocess import Popen, PIPE, check_output, CalledProcessError, STDOUT
+
+import subprocess
+
+class Server(subprocess.Popen):
+    def __init__(self, *args, **kwargs):
+        self.kill_me = kwargs.pop('kill_me', False)
+        kwargs.update({'universal_newlines': True,
+                       'stdout': subprocess.PIPE,
+                       'stderr': subprocess.STDOUT})
+        super(Server, self).__init__(*args, **kwargs)
+
+    def __enter__(self):
+        line = self.stdout.readline()
+        self.port = re.search("listening on ([0-9]+)$", line).group(1)
+        return self
+
+    def __exit__(self, *args):
+        if self.kill_me:
+            self.kill()
+            self.stdout.close() # Doesn't get closed if killed
+        self.wait()
+
+def check_output(*args, **kwargs):
+    kwargs.update({'universal_newlines': True})
+    return subprocess.check_output(*args, **kwargs)
 
 MESSAGES=10
 
@@ -31,25 +55,15 @@ def receive_expect(n=MESSAGES): return receive_expect_messages(n)+receive_expect
 def send_expect(n=MESSAGES): return "%s messages sent and acknowledged\n" % n
 def send_abort_expect(n=MESSAGES): return "%s messages started and aborted\n" % n
 
-def wait_listening(p):
-    return re.search("listening on ([0-9]+)$", p.stdout.readline()).group(1)
-
-class Broker(Popen):
+class Broker(Server):
     def __init__(self):
-        super(Broker, self).__init__(["broker", "", "0"], stdout=PIPE)
-
-    def __enter__(self):
-        self.port = wait_listening(self)
-        return self
-
-    def __exit__(self, *args):
-        self.kill()
+        super(Broker, self).__init__(["broker", "", "0"], kill_me=True)
 
 class ExampleTest(unittest.TestCase):
 
     def runex(self, name, port, messages=MESSAGES):
         """Run an example with standard arguments, return output"""
-        return check_output([name, "", str(port), "xtest", str(messages)], stderr=STDOUT)
+        return check_output([name, "", port, "xtest", str(messages)])
 
     def test_send_receive(self):
         """Send first then receive"""
@@ -65,24 +79,22 @@ class ExampleTest(unittest.TestCase):
 
     def test_send_direct(self):
         """Send to direct server"""
-        d = Popen(["direct", "", "0"], stdout=PIPE)
-        port = wait_listening(d)
-        self.assertEqual(send_expect(), self.runex("send", port))
-        self.assertMultiLineEqual(receive_expect(), d.communicate()[0])
+        with Server(["direct", "", "0"]) as d:
+            self.assertEqual(send_expect(), self.runex("send", d.port))
+            self.assertMultiLineEqual(receive_expect(), d.communicate()[0])
 
     def test_receive_direct(self):
         """Receive from direct server"""
-        d = Popen(["direct", "", "0"], stdout=PIPE)
-        port = wait_listening(d)
-        self.assertMultiLineEqual(receive_expect(), self.runex("receive", port))
-        self.assertEqual("10 messages sent and acknowledged\n", d.communicate()[0])
+        with Server(["direct", "", "0"]) as d:
+            self.assertMultiLineEqual(receive_expect(), self.runex("receive", d.port))
+            self.assertEqual("10 messages sent and acknowledged\n", d.communicate()[0])
 
     def test_send_abort_broker(self):
         """Sending aborted messages to a broker"""
         with Broker() as b:
             self.assertEqual(send_expect(), self.runex("send", b.port))
             self.assertEqual(send_abort_expect(), self.runex("send-abort", b.port))
-            for i in xrange(MESSAGES):
+            for i in range(MESSAGES):
                 self.assertEqual("Message aborted\n", b.stdout.readline())
             self.assertEqual(send_expect(), self.runex("send", b.port))
             expect = receive_expect_messages(MESSAGES)+receive_expect_messages(MESSAGES)+receive_expect_total(20)
@@ -90,14 +102,13 @@ class ExampleTest(unittest.TestCase):
 
     def test_send_abort_direct(self):
         """Send aborted messages to the direct server"""
-        d = Popen(["direct", "", "0", "examples", "20"], stdout=PIPE)
-        port = wait_listening(d)
-        self.assertEqual(send_expect(), self.runex("send", port))
-        self.assertEqual(send_abort_expect(), self.runex("send-abort", port))
-        self.assertEqual(send_expect(), self.runex("send", port))
-        expect = receive_expect_messages() + "Message aborted\n"*MESSAGES + receive_expect_messages()+receive_expect_total(20)
-        self.maxDiff = None
-        self.assertMultiLineEqual(expect, d.communicate()[0])
+        with Server(["direct", "", "0", "examples", "20"]) as d:
+            self.assertEqual(send_expect(), self.runex("send", d.port))
+            self.assertEqual(send_abort_expect(), self.runex("send-abort", d.port))
+            self.assertEqual(send_expect(), self.runex("send", d.port))
+            expect = receive_expect_messages() + "Message aborted\n"*MESSAGES + receive_expect_messages()+receive_expect_total(20)
+            self.maxDiff = None
+            self.assertMultiLineEqual(expect, d.communicate()[0])
 
     def test_send_ssl_receive(self):
         """Send with SSL, then receive"""
@@ -107,9 +118,8 @@ class ExampleTest(unittest.TestCase):
                 self.assertIn("secure connection:", got)
                 self.assertIn(send_expect(), got)
                 self.assertMultiLineEqual(receive_expect(), self.runex("receive", b.port))
-        except CalledProcessError as e:
-            print "FIXME", e.output
-            if e.output.startswith("error initializing SSL"):
+        except subprocess.CalledProcessError as e:
+            if e.output.startswith(b"error initializing SSL"):
                 print("Skipping %s: SSL not available" % self.id())
             else:
                 raise

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b41f60d4/c/tests/fdlimit.py
----------------------------------------------------------------------
diff --git a/c/tests/fdlimit.py b/c/tests/fdlimit.py
index 4836140..e5cedac 100644
--- a/c/tests/fdlimit.py
+++ b/c/tests/fdlimit.py
@@ -22,7 +22,7 @@ import os, sys
 from subprocess import Popen, PIPE
 
 def wait_listening(p):
-    return re.search("listening on ([0-9]+)$", p.stdout.readline()).group(1)
+    return re.search(b"listening on ([0-9]+)$", p.stdout.readline()).group(1)
 
 class LimitedBroker(Popen):
     def __init__(self, fdlimit):

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/b41f60d4/cpp/examples/example_test.py
----------------------------------------------------------------------
diff --git a/cpp/examples/example_test.py b/cpp/examples/example_test.py
index b83540f..e287ba5 100644
--- a/cpp/examples/example_test.py
+++ b/cpp/examples/example_test.py
@@ -21,10 +21,40 @@
 # Example executables must be in PATH
 
 import unittest, sys, time, re, shutil, os
-from subprocess import Popen, PIPE, STDOUT, check_output, check_call
 from os.path import dirname
 from string import Template
 
+import subprocess
+
+class Server(subprocess.Popen):
+    def __init__(self, *args, **kwargs):
+        self.port = None
+        self.kill_me = kwargs.pop('kill_me', False)
+        kwargs.update({'universal_newlines': True,
+                       'stdout': subprocess.PIPE,
+                       'stderr': subprocess.STDOUT})
+        super(Server, self).__init__(*args, **kwargs)
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *args):
+        if self.kill_me:
+            self.kill()
+            self.stdout.close() # Doesn't get closed if killed
+        self.wait()
+
+    @property
+    def addr(self):
+        if not self.port:
+            line = self.stdout.readline()
+            self.port = re.search("listening on ([0-9]+)$", line).group(1)
+        return ":%s/example" % self.port
+
+def check_output(*args, **kwargs):
+    kwargs.update({'universal_newlines': True})
+    return subprocess.check_output(*args, **kwargs)
+
 def _cyrusSetup(conf_dir):
   """Write out simple SASL config.tests
   """
@@ -42,24 +72,18 @@ mech_list: EXTERNAL DIGEST-MD5 SCRAM-SHA-1 CRAM-MD5 PLAIN ANONYMOUS
         f.write(t.substitute(db=db))
     cmd_template = Template("echo password | ${saslpasswd} -c -p -f ${db} -u proton user")
     cmd = cmd_template.substitute(db=db, saslpasswd=saslpasswd)
-    check_call(args=cmd, shell=True)
+    check_output(args=cmd, shell=True)
     os.environ['PN_SASL_CONFIG_PATH'] = abs_conf_dir
 
 # Globally initialize Cyrus SASL configuration
 _cyrusSetup('sasl-conf')
 
 def wait_listening(p):
-    return re.search("listening on ([0-9]+)$", p.stdout.readline()).group(1)
-
-class Broker(Popen):
-    port = None
-    def __init__(self):
-        super(Broker, self).__init__(["broker", "-a", "//:0"], stdout=PIPE, stderr=open(os.devnull))
-        Broker.port = wait_listening(self)
-        Broker.addr = "amqp://:%s/example" % self.port
-    def __enter__(self): return self
-    def __exit__(self, *args): self.kill()
+    return re.search(b"listening on ([0-9]+)$", p.stdout.readline()).group(1)
 
+class Broker(Server):
+  def __init__(self):
+    super(Broker, self).__init__(["broker", "-a", "//:0"], kill_me=True)
 
 CLIENT_EXPECT="""Twas brillig, and the slithy toves => TWAS BRILLIG, AND THE SLITHY TOVES
 Did gire and gymble in the wabe. => DID GIRE AND GYMBLE IN THE WABE.
@@ -73,49 +97,36 @@ def recv_expect():
 class ContainerExampleTest(unittest.TestCase):
     """Run the container examples, verify they behave as expected."""
 
-    broker_exe = "broker"
-
     def test_helloworld(self):
-        self.assertMultiLineEqual('Hello World!\n', check_output(["helloworld", Broker.addr]))
+      self.assertMultiLineEqual('Hello World!\n', check_output(["helloworld", Broker.addr]))
 
     def test_simple_send_recv(self):
         self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", Broker.addr]))
         self.assertMultiLineEqual(recv_expect(), check_output(["simple_recv", "-a", Broker.addr]))
 
     def test_simple_recv_send(self):
-        recv = Popen(["simple_recv", "-a", Broker.addr], stdout=PIPE)
+        recv = Server(["simple_recv", "-a", Broker.addr])
         self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", Broker.addr]))
         self.assertMultiLineEqual(recv_expect(), recv.communicate()[0])
 
-
     def test_simple_send_direct_recv(self):
-        recv = Popen(["direct_recv", "-a", "//:0"], stdout=PIPE)
-        addr = "//:%s/examples" % wait_listening(recv)
-        self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", addr]))
+        recv = Server(["direct_recv", "-a", "//:0"])
+        self.assertMultiLineEqual("all messages confirmed\n", check_output(["simple_send", "-a", recv.addr]))
         self.assertMultiLineEqual(recv_expect(), recv.communicate()[0])
 
-
     def test_simple_recv_direct_send(self):
-        send = Popen(["direct_send", "-a", "//:0"], stdout=PIPE)
-        addr = "//:%s/examples" % wait_listening(send)
-        self.assertMultiLineEqual(recv_expect(), check_output(["simple_recv", "-a", addr]))
+        send = Server(["direct_send", "-a", "//:0"])
+        self.assertMultiLineEqual(recv_expect(), check_output(["simple_recv", "-a", send.addr]))
         self.assertMultiLineEqual("all messages confirmed\n", send.communicate()[0])
 
     def test_request_response(self):
-        server = Popen(["server", Broker.addr, "example"], stdout=PIPE)
-        self.assertIn("connected to", server.stdout.readline())
-        try:
+        with Server(["server", Broker.addr, "example"], kill_me=True) as server:
+            self.assertIn("connected to", server.stdout.readline())
             self.assertMultiLineEqual(CLIENT_EXPECT, check_output(["client", "-a", Broker.addr]))
-        finally:
-            server.kill()
 
     def test_request_response_direct(self):
-        server = Popen(["server_direct", "-a", "//:0"], stdout=PIPE)
-        addr = "//:%s/examples" % wait_listening(server);
-        try:
-            self.assertMultiLineEqual(CLIENT_EXPECT, check_output(["client", "-a", addr]))
-        finally:
-            server.kill()
+        with Server(["server_direct", "-a", "//:0"], kill_me=True) as server:
+            self.assertMultiLineEqual(CLIENT_EXPECT, check_output(["client", "-a", server.addr]))
 
     def test_flow_control(self):
         want="""success: Example 1: simple credit
@@ -192,8 +203,6 @@ expected conversion_error: "unexpected type, want: uint got: string"
 class ContainerExampleSSLTest(unittest.TestCase):
     """Run the SSL container examples, verify they behave as expected."""
 
-    broker_exe = "broker"
-
     def ssl_certs_dir(self):
         """Absolute path to the test SSL certificates"""
         return os.path.join(dirname(sys.argv[0]), "ssl-certs")
@@ -225,5 +234,6 @@ Hello World!
         self.assertIn(expect, out)
 
 if __name__ == "__main__":
-    with Broker():
-        unittest.main()
+    with Broker() as b:
+      Broker.addr = b.addr
+      unittest.main()


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