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_support.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_support.py')
-rw-r--r-- | Lib/test/test_support.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 2d08f4d..4f89a86 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -89,6 +89,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 |