]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
test: More robust ipv6 detection 2187/head
authorBen Darnell <ben@bendarnell.com>
Sun, 5 Nov 2017 15:38:03 +0000 (10:38 -0500)
committerBen Darnell <ben@bendarnell.com>
Sun, 5 Nov 2017 15:38:03 +0000 (10:38 -0500)
There are many ways ipv6 can be disabled or broken (and it just
stopped working on travis-ci), so instead of checking flags like
socket.has_ipv6, attempt to actually bind to an ipv6 address.

Closes #2185

tornado/test/simple_httpclient_test.py
tornado/test/util.py

index f8d1a0546203bdcad5dedad30cc7543e40a12f76..df41abd7b87766f68f8f5e6ad2965d6ac041d58d 100644 (file)
@@ -271,16 +271,9 @@ class SimpleHTTPClientTestMixin(object):
 
     @skipIfNoIPv6
     def test_ipv6(self):
-        try:
-            [sock] = bind_sockets(None, '::1', family=socket.AF_INET6)
-            port = sock.getsockname()[1]
-            self.http_server.add_socket(sock)
-        except socket.gaierror as e:
-            if e.args[0] == socket.EAI_ADDRFAMILY:
-                # python supports ipv6, but it's not configured on the network
-                # interface, so skip this test.
-                return
-            raise
+        [sock] = bind_sockets(None, '::1', family=socket.AF_INET6)
+        port = sock.getsockname()[1]
+        self.http_server.add_socket(sock)
         url = '%s://[::1]:%d/hello' % (self.get_protocol(), port)
 
         # ipv6 is currently enabled by default but can be disabled
index 0a9438babe01bfebe2987847e3e226286c4c444f..1d34ec083310d89dc9d38fcdfa4cd3f334d6e5f0 100644 (file)
@@ -28,9 +28,6 @@ skipOnAppEngine = unittest.skipIf('APPENGINE_RUNTIME' in os.environ,
 skipIfNoNetwork = unittest.skipIf('NO_NETWORK' in os.environ,
                                   'network access disabled')
 
-skipIfNoIPv6 = unittest.skipIf(not socket.has_ipv6, 'ipv6 support not present')
-
-
 skipBefore33 = unittest.skipIf(sys.version_info < (3, 3), 'PEP 380 (yield from) not available')
 skipBefore35 = unittest.skipIf(sys.version_info < (3, 5), 'PEP 492 (async/await) not available')
 skipNotCPython = unittest.skipIf(platform.python_implementation() != 'CPython',
@@ -44,6 +41,24 @@ skipPypy3V58 = unittest.skipIf(platform.python_implementation() == 'PyPy' and
                                sys.pypy_version_info < (5, 9),
                                'pypy3 5.8 has buggy ssl module')
 
+def _detect_ipv6():
+    if not socket.has_ipv6:
+        # socket.has_ipv6 check reports whether ipv6 was present at compile
+        # time. It's usually true even when ipv6 doesn't work for other reasons.
+        return False
+    sock = None
+    try:
+        sock = socket.socket(socket.AF_INET6)
+        sock.bind(('::1', 0))
+    except socket.error:
+        return False
+    finally:
+        if sock is not None:
+            sock.close()
+    return True
+
+skipIfNoIPv6 = unittest.skipIf(not _detect_ipv6(), 'ipv6 support not present')
+
 
 def refusing_port():
     """Returns a local port number that will refuse all connections.