From 08af3e6e7ff96720eabf77309408f148c13e14aa Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Mon, 13 Apr 2020 08:37:23 -0400 Subject: [PATCH] Test for --min-duration --- projects/TestScripts/testTimeThreshold.py | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 projects/TestScripts/testTimeThreshold.py diff --git a/projects/TestScripts/testTimeThreshold.py b/projects/TestScripts/testTimeThreshold.py new file mode 100644 index 0000000000..9715db23db --- /dev/null +++ b/projects/TestScripts/testTimeThreshold.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import subprocess +import sys + +def run_tests_with_threshold(self_test_exe, threshold): + cmd = [self_test_exe, '--min-duration', str(threshold)] + process = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if stderr: + raise RuntimeError("Unexpected error output:\n" + + stderr.decode()) + if process.returncode != 0: + raise RuntimeError("Unexpected failure to run tests\n") + result = stdout.split(b'\n') + times_reported = [float(s.split()[0]) for s in result if b' s: ' in s] + return times_reported + +def check_times_at_least(times_reported, minimum): + for time in times_reported: + assert time >= minimum, ( + 'Time {} was less that requested minimum {}' .format( + time, minimum)) + +def main(): + self_test_exe, = sys.argv[1:] + times = run_tests_with_threshold(self_test_exe, '1000') + assert not times + times = run_tests_with_threshold(self_test_exe, '0') + assert times + check_times_at_least(times, 0) + nonzero_times = sorted([t for t in times if t > 0]) + if nonzero_times: + median_nonzero_time = nonzero_times[len(nonzero_times)//2] + times = run_tests_with_threshold(self_test_exe, median_nonzero_time) + check_times_at_least(times, median_nonzero_time) + else: + times = run_tests_with_threshold(self_test_exe, 1) + assert not times + +if __name__ == '__main__': + sys.exit(main())