changeset 30918:2aaa8bfc7bd9

runtests: check ports on IPv6 address Previously, checkportisavailable only checks ports on the IPv4 address. This patch makes it check IPv6 as well. It'll be useful if "localhost" does not have an IPv4 address, or its IPv4 address does not exist somehow.
author Jun Wu <quark@fb.com>
date Thu, 09 Feb 2017 05:57:54 -0800
parents 564a96a56b73
children a95fc01aaffe
files tests/run-tests.py
diffstat 1 files changed, 13 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/tests/run-tests.py	Wed Feb 08 08:08:41 2017 -0800
+++ b/tests/run-tests.py	Thu Feb 09 05:57:54 2017 -0800
@@ -114,15 +114,19 @@
 
 def checkportisavailable(port):
     """return true if a port seems free to bind on localhost"""
-    try:
-        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.bind(('localhost', port))
-        s.close()
-        return True
-    except socket.error as exc:
-        if not exc.errno == errno.EADDRINUSE:
-            raise
-        return False
+    families = [getattr(socket, i, None)
+                for i in ('AF_INET', 'AF_INET6')
+                if getattr(socket, i, None) is not None]
+    for family in families:
+        try:
+            s = socket.socket(family, socket.SOCK_STREAM)
+            s.bind(('localhost', port))
+            s.close()
+            return True
+        except socket.error as exc:
+            if exc.errno not in (errno.EADDRINUSE, errno.EADDRNOTAVAIL):
+                raise
+    return False
 
 closefds = os.name == 'posix'
 def Popen4(cmd, wd, timeout, env=None):