From: Naoki Kambe Date: Tue, 6 Sep 2011 11:17:57 +0000 (+0900) Subject: [1175] modify b10-stats-httpd_test.py X-Git-Tag: perftcpdns_before_epoll~126 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d56c782197242e32ccdd23c9e3652ff520f3d58f;p=thirdparty%2Fkea.git [1175] modify b10-stats-httpd_test.py - The function get_availaddr uses socket.getaddrinfo for getting the address family. - The function is_ipv6_enabled uses 3 random ports for checking whether IPv6 is enabled. --- diff --git a/src/bin/stats/tests/b10-stats-httpd_test.py b/src/bin/stats/tests/b10-stats-httpd_test.py index ccadeb20fd..f0a49cf1a5 100644 --- a/src/bin/stats/tests/b10-stats-httpd_test.py +++ b/src/bin/stats/tests/b10-stats-httpd_test.py @@ -33,6 +33,7 @@ import threading import http.client import xml.etree.ElementTree import signal +import random import isc import stats_httpd @@ -60,34 +61,36 @@ def get_availaddr(address='127.0.0.1', port=8001): """returns tuple of address and port available to listen on the platform. Default port range is between 8001 and 65535. If port is over flow(greater than 65535), OverflowError is thrown""" - sock = None - if is_ipv6_enabled(address): - sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - else: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - while True: + while True: + for addr in socket.getaddrinfo( + address, port, 0, + socket.SOCK_STREAM, socket.IPPROTO_TCP): + sock = socket.socket(addr[0], socket.SOCK_STREAM) try: sock.bind((address, port)) return (address, port) except socket.error: - # This address and port number are already in use. - # next port number is added - port = port + 1 - finally: - if sock: sock.close() - -def is_ipv6_enabled(address='::1', port=8000): - """checks IPv6 enabled on the platform""" - sock = None - try: - sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - sock.bind((address, port)) - return True - except socket.error: - return False - finally: - if sock: sock.close() + continue + finally: + if sock: sock.close() + # This address and port number are already in use. + # next port number is added + port = port + 1 + +def is_ipv6_enabled(address='::1', port=8001): + """checks IPv6 enabled on the platform. address for check is '::1' + and port for check is random number between 8001 and + 65535. Retrying is 3 times even if it fails.""" + for p in random.sample(range(port, 65535), 3): + try: + sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + sock.bind((address, p)) + return True + except socket.error: + continue + finally: + if sock: sock.close() + raise Exception('hoge') class TestHttpHandler(unittest.TestCase): """Tests for HttpHandler class"""