[Python-checkins] cpython (3.4): Issue #19875: Fix random test_getsockaddrarg() failure.

charles-francois.natali python-checkins at python.org
Fri Jul 25 19:45:59 CEST 2014


http://hg.python.org/cpython/rev/897c9e6ddb1a
changeset:   91861:897c9e6ddb1a
branch:      3.4
parent:      91773:7238c6a05ca6
user:        Charles-François Natali <cf.natali at gmail.com>
date:        Fri Jul 25 18:44:30 2014 +0100
summary:
  Issue #19875: Fix random test_getsockaddrarg() failure.

files:
  Lib/test/test_socket.py |  24 ++++++++++++++++--------
  1 files changed, 16 insertions(+), 8 deletions(-)


diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -3,6 +3,7 @@
 
 import errno
 import io
+import itertools
 import socket
 import select
 import tempfile
@@ -1145,17 +1146,24 @@
         sock.close()
 
     def test_getsockaddrarg(self):
-        host = '0.0.0.0'
+        sock = socket.socket()
+        self.addCleanup(sock.close)
         port = support.find_unused_port()
         big_port = port + 65536
         neg_port = port - 65536
-        sock = socket.socket()
-        try:
-            self.assertRaises(OverflowError, sock.bind, (host, big_port))
-            self.assertRaises(OverflowError, sock.bind, (host, neg_port))
-            sock.bind((host, port))
-        finally:
-            sock.close()
+        self.assertRaises(OverflowError, sock.bind, (HOST, big_port))
+        self.assertRaises(OverflowError, sock.bind, (HOST, neg_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 = support.find_unused_port()
+            try:
+                sock.bind((HOST, port))
+            except OSError as e:
+                if e.errno != errno.EADDRINUSE or i == 5:
+                    raise
+            else:
+                break
 
     @unittest.skipUnless(os.name == "nt", "Windows specific")
     def test_sock_ioctl(self):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list