diff options
Diffstat (limited to 'util/run_host_test')
-rwxr-xr-x | util/run_host_test | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/util/run_host_test b/util/run_host_test index 3ee8ccc7ed..3108da1852 100755 --- a/util/run_host_test +++ b/util/run_host_test @@ -26,7 +26,6 @@ class TestResult(enum.Enum): @property def reason(self): - """Return a map of test result enums to descriptions.""" return { TestResult.SUCCESS: 'passed', TestResult.FAIL: 'failed', @@ -36,12 +35,11 @@ class TestResult(enum.Enum): def run_test(path, timeout=10): - """Runs a test.""" start_time = time.monotonic() env = dict(os.environ) env['ASAN_OPTIONS'] = 'log_path=stderr' - with subprocess.Popen( + proc = subprocess.Popen( [path], bufsize=0, stdin=subprocess.PIPE, @@ -49,50 +47,49 @@ def run_test(path, timeout=10): env=env, encoding='utf-8', errors='replace', - ) as proc: - - # Put the output pipe in non-blocking mode. We will then select(2) - # on the pipe to know when we have bytes to process. - os.set_blocking(proc.stdout.fileno(), False) - - try: - output_buffer = io.StringIO() - while True: - select_timeout = timeout - (time.monotonic() - start_time) - if select_timeout <= 0: - return TestResult.TIMEOUT, output_buffer.getvalue() - - readable, _, _ = select.select( - [proc.stdout], [], [], select_timeout) - - if not readable: - # Indicates that select(2) timed out. - return TestResult.TIMEOUT, output_buffer.getvalue() - - output_buffer.write(proc.stdout.read()) - output_log = output_buffer.getvalue() - - if 'Pass!' in output_log: - return TestResult.SUCCESS, output_log - if 'Fail!' in output_log: - return TestResult.FAIL, output_log - if proc.poll(): - return TestResult.UNEXPECTED_TERMINATION, output_log - finally: - # Check if the process has exited. If not, send it a SIGTERM, wait - # for it to exit, and if it times out, kill the process directly. - if not proc.poll(): - try: - proc.terminate() - proc.wait(timeout) - except subprocess.TimeoutExpired: - proc.kill() + ) + + # Put the output pipe in non-blocking mode. We will then select(2) + # on the pipe to know when we have bytes to process. + os.set_blocking(proc.stdout.fileno(), False) + + try: + output_buffer = io.StringIO() + while True: + select_timeout = timeout - (time.monotonic() - start_time) + if select_timeout <= 0: + return TestResult.TIMEOUT, output_buffer.getvalue() + + readable, _, _ = select.select( + [proc.stdout], [], [], select_timeout) + + if not readable: + # Indicates that select(2) timed out. + return TestResult.TIMEOUT, output_buffer.getvalue() + + output_buffer.write(proc.stdout.read()) + output_log = output_buffer.getvalue() + + if 'Pass!' in output_log: + return TestResult.SUCCESS, output_log + if 'Fail!' in output_log: + return TestResult.FAIL, output_log + if proc.poll(): + return TestResult.UNEXPECTED_TERMINATION, output_log + finally: + # Check if the process has exited. If not, send it a SIGTERM, wait for + # it to exit, and if it times out, kill the process directly. + if not proc.poll(): + try: + proc.terminate() + proc.wait(timeout) + except subprocess.TimeoutExpired: + proc.kill() def parse_options(argv): - """Parse command line flags.""" parser = argparse.ArgumentParser() - parser.add_argument('-t', '--timeout', type=float, default=120, + parser.add_argument('-t', '--timeout', type=float, default=60, help='Timeout to kill test after.') parser.add_argument('--coverage', action='store_const', const='coverage', default='host', dest='test_target', @@ -104,7 +101,6 @@ def parse_options(argv): def main(argv): - """The main function.""" opts = parse_options(argv) # Tests will be located in build/host, unless the --coverage flag was |