summaryrefslogtreecommitdiff
path: root/util/run_host_test
diff options
context:
space:
mode:
Diffstat (limited to 'util/run_host_test')
-rwxr-xr-xutil/run_host_test84
1 files changed, 44 insertions, 40 deletions
diff --git a/util/run_host_test b/util/run_host_test
index 3108da1852..3ee8ccc7ed 100755
--- a/util/run_host_test
+++ b/util/run_host_test
@@ -26,6 +26,7 @@ class TestResult(enum.Enum):
@property
def reason(self):
+ """Return a map of test result enums to descriptions."""
return {
TestResult.SUCCESS: 'passed',
TestResult.FAIL: 'failed',
@@ -35,11 +36,12 @@ 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'
- proc = subprocess.Popen(
+ with subprocess.Popen(
[path],
bufsize=0,
stdin=subprocess.PIPE,
@@ -47,49 +49,50 @@ def run_test(path, timeout=10):
env=env,
encoding='utf-8',
errors='replace',
- )
-
- # 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()
+ ) 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()
def parse_options(argv):
+ """Parse command line flags."""
parser = argparse.ArgumentParser()
- parser.add_argument('-t', '--timeout', type=float, default=60,
+ parser.add_argument('-t', '--timeout', type=float, default=120,
help='Timeout to kill test after.')
parser.add_argument('--coverage', action='store_const', const='coverage',
default='host', dest='test_target',
@@ -101,6 +104,7 @@ 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