diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_socket.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index f1b57fc407..d808f3f62b 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1398,10 +1398,21 @@ class GeneralModuleTests(unittest.TestCase): def testSockName(self): # Testing getsockname() - port = socket_helper.find_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) - sock.bind(("0.0.0.0", port)) + + # Since find_unused_port() is inherently subject to race conditions, we + # call it a couple times if necessary. + for i in itertools.count(): + port = socket_helper.find_unused_port() + try: + sock.bind(("0.0.0.0", port)) + except OSError as e: + if e.errno != errno.EADDRINUSE or i == 5: + raise + else: + break + name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # it reasonable to get the host's addr in addition to 0.0.0.0. |