summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socketserver.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_socketserver.py')
-rw-r--r--Lib/test/test_socketserver.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
index 1245ba5..dd4532f 100644
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -1,11 +1,13 @@
# Test suite for SocketServer.py
from test import test_support
-from test.test_support import verbose, verify, TESTFN, TestSkipped
+from test.test_support import (verbose, verify, TESTFN, TestSkipped,
+ reap_children)
test_support.requires('network')
from SocketServer import *
import socket
+import errno
import select
import time
import threading
@@ -77,6 +79,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 +143,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]
@@ -175,6 +200,7 @@ def test_main():
testall()
finally:
cleanup()
+ reap_children()
if __name__ == "__main__":
test_main()