diff options
author | 2018-04-24 15:11:47 -0700 | |
---|---|---|
committer | 2018-04-24 16:24:43 -0700 | |
commit | 29fead7d492b3655ea876b1ddcbdf3719a91392d (patch) | |
tree | e5771648edbc91730c82c0940d1dbec734302956 /test/cpp/naming/utils | |
parent | 2d894a8c3b6bd0a236f14c5630faced4caba0a55 (diff) |
Convert c-ares test runner from bash to python, so that it works on
windows
Diffstat (limited to 'test/cpp/naming/utils')
-rwxr-xr-x | test/cpp/naming/utils/dns_resolver.py | 14 | ||||
-rwxr-xr-x | test/cpp/naming/utils/dns_server.py | 28 | ||||
-rwxr-xr-x | test/cpp/naming/utils/tcp_connect.py | 20 |
3 files changed, 52 insertions, 10 deletions
diff --git a/test/cpp/naming/utils/dns_resolver.py b/test/cpp/naming/utils/dns_resolver.py index 6b272444e7..f0d2f7fe93 100755 --- a/test/cpp/naming/utils/dns_resolver.py +++ b/test/cpp/naming/utils/dns_resolver.py @@ -16,9 +16,16 @@ """Makes DNS queries for A records to specified servers""" import argparse -import signal +import threading +import time import twisted.internet.task as task import twisted.names.client as client +import twisted.internet.reactor as reactor + +def exit_after_timeout(timeout): + time.sleep(timeout) + print('Time limit reached. Forcing exit') + reactor.stop() def main(): argp = argparse.ArgumentParser(description='Make DNS queries for A records') @@ -31,7 +38,6 @@ def main(): argp.add_argument('-t', '--timeout', default=1, type=int, help=('Force process exit after this number of seconds.')) args = argp.parse_args() - signal.alarm(args.timeout) def OnResolverResultAvailable(result): answers, authority, additional = result for a in answers: @@ -42,6 +48,10 @@ def main(): deferred_result = resolver.lookupAddress(args.qname) deferred_result.addCallback(OnResolverResultAvailable) return deferred_result + # We can't use sigalarm on windows, so start a thread. + timeout_thread = threading.Thread(target=exit_after_timeout, args=[args.timeout]) + timeout_thread.setDaemon(True) + timeout_thread.start() task.react(BeginQuery, [args.qname]) if __name__ == '__main__': diff --git a/test/cpp/naming/utils/dns_server.py b/test/cpp/naming/utils/dns_server.py index 9f42f65ee6..d63364a187 100755 --- a/test/cpp/naming/utils/dns_server.py +++ b/test/cpp/naming/utils/dns_server.py @@ -20,6 +20,8 @@ import sys import yaml import signal import os +import threading +import time import twisted import twisted.internet @@ -33,6 +35,7 @@ import twisted.names.dns import twisted.names.server from twisted.names import client, server, common, authority, dns import argparse +import platform _SERVER_HEALTH_CHECK_RECORD_NAME = 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp' # missing end '.' for twisted syntax _SERVER_HEALTH_CHECK_RECORD_DATA = '123.123.123.123' @@ -109,12 +112,27 @@ def start_local_dns_server(args): twisted.internet.reactor.suggestThreadPoolSize(1) twisted.internet.reactor.run() -def _quit_on_signal(signum, _frame): - print('Received SIGNAL %d. Quitting with exit code 0' % signum) +def shutdown_process(): twisted.internet.reactor.stop() sys.stdout.flush() sys.exit(0) +def _quit_on_signal(signum, _frame): + print('Received SIGNAL %d. Quitting with exit code 0' % signum) + shutdown_process() + +def flush_stdout_loop(): + num_timeouts_so_far = 0 + sleep_time = 1 + # Prevent zombies. Tests that use this server are short-lived. + max_timeouts = 60 * 2 + while num_timeouts_so_far < max_timeouts: + sys.stdout.flush() + time.sleep(sleep_time) + num_timeouts_so_far += 1 + print('Process timeout reached, or cancelled. Exitting 0.') + shutdown_process() + def main(): argp = argparse.ArgumentParser(description='Local DNS Server for resolver tests') argp.add_argument('-p', '--port', default=None, type=int, @@ -123,11 +141,11 @@ def main(): help=('Directory of resolver_test_record_groups.yaml file. ' 'Defauls to path needed when the test is invoked as part of run_tests.py.')) args = argp.parse_args() - signal.signal(signal.SIGALRM, _quit_on_signal) signal.signal(signal.SIGTERM, _quit_on_signal) signal.signal(signal.SIGINT, _quit_on_signal) - # Prevent zombies. Tests that use this server are short-lived. - signal.alarm(2 * 60) + output_flush_thread = threading.Thread(target=flush_stdout_loop) + output_flush_thread.setDaemon(True) + output_flush_thread.start() start_local_dns_server(args) if __name__ == '__main__': diff --git a/test/cpp/naming/utils/tcp_connect.py b/test/cpp/naming/utils/tcp_connect.py index bf7455e3c2..15fb0e7ff6 100755 --- a/test/cpp/naming/utils/tcp_connect.py +++ b/test/cpp/naming/utils/tcp_connect.py @@ -16,8 +16,17 @@ """Opens a TCP connection to a specified server and then exits.""" import argparse -import signal import socket +import threading +import time +import sys + +connect_success = False + +def try_connect(args): + socket.create_connection([args.server_host, args.server_port]) + global connect_success + connect_success = True def main(): argp = argparse.ArgumentParser(description='Open a TCP handshake to a server') @@ -28,8 +37,13 @@ def main(): argp.add_argument('-t', '--timeout', default=1, type=int, help='Force process exit after this number of seconds.') args = argp.parse_args() - signal.alarm(args.timeout) - socket.create_connection([args.server_host, args.server_port]) + t = threading.Thread(target=try_connect, args=[args]) + t.setDaemon(True) + t.start() + # We can't use sigalarm on windows, so join with a timeout. + t.join(timeout=args.timeout) + if t.isAlive() or not connect_success: + sys.exit(1) if __name__ == '__main__': main() |