# HG changeset patch # User Jun Wu # Date 1487204311 28800 # Node ID 15f9084a9a0c7d88153d6de416c6529b702a7f04 # Parent d4825798818b38c723b68bb5d3f38c5c5dff465a runtests: add a function to test if IPv6 is available Previously, checkportisavailable returns True if the port is free either on IPv4 or IPv6, but the hg server only uses IPv4 by default. That leads to issues when IPv4 port is not free but the IPv6 one is. To address that, run-tests should stick with either IPv4 or IPv6. This patch adds a function similar to checkportisavailable to test if IPv6 is available, and assigns the result to a variable. The new function was tested in a Linux system script with the following steps: 1. Run "ip addr del ::1/128 dev lo" to delete lo's IPv6 address, Confirm checkipv6available() returns False. 2. Run "ip addr add ::1/128 dev lo" to add back lo's IPv6 address. Confirm checkipv6available() returns True. 3. Start a web server taking the 8000 port. Confirm checkipv6available(8000) is still True. diff -r d4825798818b -r 15f9084a9a0c tests/run-tests.py --- a/tests/run-tests.py Wed Feb 15 13:34:06 2017 -0800 +++ b/tests/run-tests.py Wed Feb 15 16:18:31 2017 -0800 @@ -112,6 +112,29 @@ # For Windows support wifexited = getattr(os, "WIFEXITED", lambda x: False) +# Whether to use IPv6 +def checkipv6available(port=20058): + """return true if we can listen on localhost's IPv6 ports""" + family = getattr(socket, 'AF_INET6', None) + if family is None: + return False + try: + s = socket.socket(family, socket.SOCK_STREAM) + s.bind(('localhost', port)) + s.close() + return True + except socket.error as exc: + if exc.errno == errno.EADDRINUSE: + return True + elif exc.errno in (errno.EADDRNOTAVAIL, errno.EPROTONOSUPPORT): + return False + else: + raise + else: + return False + +useipv6 = checkipv6available() + def checkportisavailable(port): """return true if a port seems free to bind on localhost""" families = [getattr(socket, i, None)