[Python-checkins] r52045 - in python/branches/release24-maint: Lib/test/test_socket.py Lib/test/test_socket_ssl.py Lib/test/test_socketserver.py Lib/test/test_support.py Misc/NEWS

andrew.kuchling python-checkins at python.org
Fri Sep 29 20:31:01 CEST 2006


Author: andrew.kuchling
Date: Fri Sep 29 20:30:59 2006
New Revision: 52045

Modified:
   python/branches/release24-maint/Lib/test/test_socket.py
   python/branches/release24-maint/Lib/test/test_socket_ssl.py
   python/branches/release24-maint/Lib/test/test_socketserver.py
   python/branches/release24-maint/Lib/test/test_support.py
   python/branches/release24-maint/Misc/NEWS
Log:
[Backport rev. 46882 by neal.norwitz]

Fix the socket tests so they can be run concurrently.  Backport candidate



Modified: python/branches/release24-maint/Lib/test/test_socket.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_socket.py	(original)
+++ python/branches/release24-maint/Lib/test/test_socket.py	Fri Sep 29 20:30:59 2006
@@ -20,7 +20,8 @@
     def setUp(self):
         self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-        self.serv.bind((HOST, PORT))
+        global PORT
+        PORT = test_support.bind_port(self.serv, HOST, PORT)
         self.serv.listen(1)
 
     def tearDown(self):
@@ -32,7 +33,8 @@
     def setUp(self):
         self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-        self.serv.bind((HOST, PORT))
+        global PORT
+        PORT = test_support.bind_port(self.serv, HOST, PORT)
 
     def tearDown(self):
         self.serv.close()

Modified: python/branches/release24-maint/Lib/test/test_socket_ssl.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_socket_ssl.py	(original)
+++ python/branches/release24-maint/Lib/test/test_socket_ssl.py	Fri Sep 29 20:30:59 2006
@@ -72,10 +72,10 @@
         return
 
     # some random port to connect to
-    PORT = 9934
+    PORT = [9934]
     def listener():
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.bind(('', PORT))
+        PORT[0] = test_support.bind_port(s, '', PORT[0])
         s.listen(5)
         s.accept()
         del s
@@ -83,7 +83,7 @@
 
     def connector():
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.connect(('localhost', PORT))
+        s.connect(('localhost', PORT[0]))
         try:
             ssl_sock = socket.ssl(s)
         except socket.sslerror:

Modified: python/branches/release24-maint/Lib/test/test_socketserver.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_socketserver.py	(original)
+++ python/branches/release24-maint/Lib/test/test_socketserver.py	Fri Sep 29 20:30:59 2006
@@ -6,6 +6,7 @@
 
 from SocketServer import *
 import socket
+import errno
 import select
 import time
 import threading
@@ -77,6 +78,11 @@
             pass
         if verbose: print "thread: creating server"
         svr = svrcls(self.__addr, self.__hdlrcls)
+        # pull the address out of the server in case it changed
+        # this can happen if another process is using the port
+        addr = getattr(svr, 'server_address')
+        if addr:
+            self.__addr = addr
         if verbose: print "thread: serving three times"
         svr.serve_a_few()
         if verbose: print "thread: done"
@@ -136,7 +142,25 @@
         t.join()
         if verbose: print "done"
 
-tcpservers = [TCPServer, ThreadingTCPServer]
+class ForgivingTCPServer(TCPServer):
+    # prevent errors if another process is using the port we want
+    def server_bind(self):
+        host, default_port = self.server_address
+        # this code shamelessly stolen from test.test_support
+        # the ports were changed to protect the innocent
+        import sys
+        for port in [default_port, 3434, 8798, 23833]:
+            try:
+                self.server_address = host, port
+                TCPServer.server_bind(self)
+                break
+            except socket.error, (err, msg):
+                if err != errno.EADDRINUSE:
+                    raise
+                print >>sys.__stderr__, \
+                    '  WARNING: failed to listen on port %d, trying another' % port
+
+tcpservers = [ForgivingTCPServer, ThreadingTCPServer]
 if hasattr(os, 'fork') and os.name not in ('os2',):
     tcpservers.append(ForkingTCPServer)
 udpservers = [UDPServer, ThreadingUDPServer]

Modified: python/branches/release24-maint/Lib/test/test_support.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_support.py	(original)
+++ python/branches/release24-maint/Lib/test/test_support.py	Fri Sep 29 20:30:59 2006
@@ -86,6 +86,24 @@
             msg = "Use of the `%s' resource not enabled" % resource
         raise ResourceDenied(msg)
 
+def bind_port(sock, host='', preferred_port=54321):
+    """Try to bind the sock to a port.  If we are running multiple
+    tests and we don't try multiple ports, the test can fails.  This
+    makes the test more robust."""
+
+    import socket, errno
+    # some random ports that hopefully no one is listening on.
+    for port in [preferred_port, 9907, 10243, 32999]:
+        try:
+            sock.bind((host, port))
+            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'
+
 FUZZ = 1e-6
 
 def fcmp(x, y): # fuzzy comparison function

Modified: python/branches/release24-maint/Misc/NEWS
==============================================================================
--- python/branches/release24-maint/Misc/NEWS	(original)
+++ python/branches/release24-maint/Misc/NEWS	Fri Sep 29 20:30:59 2006
@@ -228,6 +228,8 @@
 - Patch #1529686: test_iterlen and test_email_codecs are now actually
   run by regrtest.py.
 
+- Fix the socket tests so they can be run concurrently.
+
 
 What's New in Python 2.4.3?
 ===========================


More information about the Python-checkins mailing list