qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject [2/4] qpid-proton git commit: NO-JIRA: Get rid of monkeypatch in exampletest.py
Date Wed, 15 Mar 2017 20:07:11 GMT
NO-JIRA: Get rid of monkeypatch in exampletest.py


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

Branch: refs/heads/master
Commit: 64a05f6e33a2b14193fff95794bbb0cc994dcbd2
Parents: 8376800
Author: Alan Conway <aconway@redhat.com>
Authored: Thu Mar 9 00:24:19 2017 -0500
Committer: Alan Conway <aconway@redhat.com>
Committed: Wed Mar 15 15:56:30 2017 -0400

----------------------------------------------------------------------
 examples/c/proactor/test.py | 12 ++++++------
 examples/exampletest.py     | 28 ++++++++++++++++------------
 2 files changed, 22 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/64a05f6e/examples/c/proactor/test.py
----------------------------------------------------------------------
diff --git a/examples/c/proactor/test.py b/examples/c/proactor/test.py
index 84c519e..4950cef 100644
--- a/examples/c/proactor/test.py
+++ b/examples/c/proactor/test.py
@@ -34,8 +34,8 @@ class Broker(object):
         self.test = test
 
     def __enter__(self):
-        with bind0() as sock:
-            self.addr = "127.0.0.1:%s/examples" % (sock.port())
+        with TestPort() as port:
+            self.addr = "127.0.0.1:%s" % port
             self.proc = self.test.proc(["broker", self.addr])
             self.proc.wait_re("listening")
             return self
@@ -67,8 +67,8 @@ class CExampleTest(ExampleTestCase):
 
     def test_send_direct(self):
         """Send to direct server"""
-        with bind0() as sock:
-            addr = "127.0.0.1:%s" % sock.port()
+        with TestPort() as port:
+            addr = "127.0.0.1:%s" % port
             d = self.proc(["direct", addr])
             d.wait_re("listening")
             self.assertEqual("10 messages sent and acknowledged\n", self.proc(["send", addr]).wait_out())
@@ -76,8 +76,8 @@ class CExampleTest(ExampleTestCase):
 
     def test_receive_direct(self):
         """Receive from direct server"""
-        with bind0() as sock:
-            addr = "127.0.0.1:%s" % sock.port()
+        with TestPort() as port:
+            addr = "127.0.0.1:%s" % port
             d = self.proc(["direct", addr])
             d.wait_re("listening")
             self.assertEqual(receive_expect(10), self.proc(["receive", addr]).wait_out())

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/64a05f6e/examples/exampletest.py
----------------------------------------------------------------------
diff --git a/examples/exampletest.py b/examples/exampletest.py
index 5c820fd..71aa463 100644
--- a/examples/exampletest.py
+++ b/examples/exampletest.py
@@ -31,18 +31,22 @@ 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.bind(('', 0))
-    return sock
-
-# Monkeypatch add port() and with support to socket.socket
-socket.socket.port = lambda(self): socket.getnameinfo(self.getsockname(), 0)[1]
-socket.socket.__enter__ = lambda(self): self
-def socket__exit__(self, *args): self.close()
-socket.socket.__exit__ = socket__exit__
+class TestPort(object):
+    """Get an unused port using bind(0) and SO_REUSEADDR and hold it till close()"""
+    def __init__(self):
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+        self.sock.bind(('', 0))
+        self.port = socket.getnameinfo(self.sock.getsockname(), 0)[1]
+
+    def __enter__(self):
+        return self.port
+
+    def __exit__(self, *args):
+        self.close()
+
+    def close(self):
+        self.sock.close()
 
 class ProcError(Exception):
     """An exception that captures failed process output"""


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


Mime
View raw message