]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Backport rev. 46882 by neal.norwitz]
authorAndrew M. Kuchling <amk@amk.ca>
Fri, 29 Sep 2006 18:30:59 +0000 (18:30 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Fri, 29 Sep 2006 18:30:59 +0000 (18:30 +0000)
Fix the socket tests so they can be run concurrently.  Backport candidate

Lib/test/test_socket.py
Lib/test/test_socket_ssl.py
Lib/test/test_socketserver.py
Lib/test/test_support.py
Misc/NEWS

index 14e80f1a4087e74d628d0a132be2dcd715a9defb..4b4a628dae4d8880c3d39bd06b3873c65d7da8c6 100644 (file)
@@ -20,7 +20,8 @@ class SocketTCPTest(unittest.TestCase):
     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 @@ class SocketUDPTest(unittest.TestCase):
     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()
index e9de5f2b09d9e1629e96b0b36fe52533dc789da1..9c37de4b78dcb6148dfbb27245575ba607017ce4 100644 (file)
@@ -72,10 +72,10 @@ def test_rude_shutdown():
         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 test_rude_shutdown():
 
     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:
index 1245ba5fbf27da9a6216442d58ff7d17aaf6f4b0..93165470f922adc594064c3b5688594047cea0f7 100644 (file)
@@ -6,6 +6,7 @@ test_support.requires('network')
 
 from SocketServer import *
 import socket
+import errno
 import select
 import time
 import threading
@@ -77,6 +78,11 @@ class ServerThread(threading.Thread):
             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 @@ def testloop(proto, servers, hdlrcls, testfunc):
         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]
index a296caf6d0583916a90d5b771948e4fa006a4bd3..6318a211c6ee56d39379bfe633ae3c326f88c3af 100644 (file)
@@ -86,6 +86,24 @@ def requires(resource, msg=None):
             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
index c3d8a126c7facf7743705ec763679fa357649b0d..4df5d63d74dd698fb944b7d6e593802ee8ef9b8d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -228,6 +228,8 @@ Tests
 - 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?
 ===========================