[Jython-checkins] jython: Fixes test_httplib SourceAddressTest regression test

jim.baker jython-checkins at python.org
Sat Jun 14 22:58:20 CEST 2014


http://hg.python.org/jython/rev/2bddcf5b03b2
changeset:   7295:2bddcf5b03b2
parent:      7167:b31e71644fa8
user:        Indra Talip <indra.talip at gmail.com>
date:        Mon Dec 09 20:19:16 2013 +1100
summary:
  Fixes test_httplib SourceAddressTest regression test
Changes test_support.bind_port() to create and listen on a socket bound to
port 0 to get the OS to assign an emphemeral port, the socket is then closed
and deleted. Necessary since due to late binding of the socket implematation
socket.bind always succeeds and never actually checks if the port is in use.

files:
  Lib/test/test_support.py |  33 ++++++++++-----------------
  1 files changed, 12 insertions(+), 21 deletions(-)


diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -374,27 +374,18 @@
     from bind()'ing to our host/port for the duration of the test.
     """
     if is_jython:
-        # Find some random ports that hopefully no one is listening on.
-        # Ideally each test would clean up after itself and not continue
-        # listening on any ports.  However, this isn't the case.  The last port
-        # (0) is a stop-gap that asks the O/S to assign a port.  Whenever the
-        # warning message below is printed, the test that is listening on the
-        # port should be fixed to close the socket at the end of the test.
-        # Another reason why we can't use a port is another process (possibly
-        # another instance of the test suite) is using the same port.
-
-        for port in [54321, 9907, 10243, 32999, 0]:
-            try:
-                sock.bind((host, port))
-                if port == 0:
-                    port = sock.getsockname()[1]
-                return port
-            except socket.error, (err, msg):
-                if err != errno.EADDRINUSE:
-                    raise
-                print >>sys.__stderr__, \
-                    '  WARNING: failed to listen on port %d, trying another' % port
-        raise TestFailed, 'unable to find port to listen on'
+        # Late binding of the jython socket implementation to a
+        # ServerSocketChannel or SocketChannel means that it's not possible to
+        # get the port until a call to connect() or listen(). Hence why a new
+        # socket is created and listen() is called on it.
+        tempsock = socket.socket(sock.family, sock.type)
+        tempsock.bind((host, 0))
+        tempsock.listen(1)
+        port = tempsock.getsockname()[1]
+        tempsock.close()
+        del tempsock
+        sock.bind((host, port))
+        return port
 
     elif sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
         if hasattr(socket, 'SO_REUSEADDR'):

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list