diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2006-06-12 02:13:21 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2006-06-12 02:13:21 (GMT) |
commit | 909eb12c9529971db352c91327c8d72bd0d16e4b (patch) | |
tree | 414723ff1756ab7060ca4a81bd5df7e383430fd3 /Lib/test/test_socketserver.py | |
parent | b9845e72f9af2c47fbeeb0d27bd1659e38a9ffbd (diff) | |
download | cpython-909eb12c9529971db352c91327c8d72bd0d16e4b.zip cpython-909eb12c9529971db352c91327c8d72bd0d16e4b.tar.gz cpython-909eb12c9529971db352c91327c8d72bd0d16e4b.tar.bz2 |
Fix the socket tests so they can be run concurrently. Backport candidate
Diffstat (limited to 'Lib/test/test_socketserver.py')
-rw-r--r-- | Lib/test/test_socketserver.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py index 1245ba5..9316547 100644 --- a/Lib/test/test_socketserver.py +++ b/Lib/test/test_socketserver.py @@ -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] |