Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Run the test suite using a separate process for each test file. |
| 5 | """ |
| 6 | |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 7 | import multiprocessing |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 8 | import os |
| 9 | import platform |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 10 | import shlex |
| 11 | import subprocess |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 12 | import sys |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 13 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 14 | from optparse import OptionParser |
| 15 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 16 | def get_timeout_command(): |
| 17 | if sys.platform.startswith("win32"): |
| 18 | return None |
| 19 | try: |
| 20 | subprocess.call("timeout") |
| 21 | return "timeout" |
| 22 | except OSError: |
| 23 | pass |
| 24 | try: |
| 25 | subprocess.call("gtimeout") |
| 26 | return "gtimeout" |
| 27 | except OSError: |
| 28 | pass |
| 29 | return None |
| 30 | |
| 31 | timeout_command = get_timeout_command() |
| 32 | |
| 33 | default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "5m" |
| 34 | |
| 35 | # Status codes for running command with timeout. |
| 36 | eTimedOut, ePassed, eFailed = 124, 0, 1 |
| 37 | |
| 38 | def call_with_timeout(command, timeout): |
| 39 | """Each test will timeout after 5 minutes by default. |
| 40 | Override the default timeout of 5 minutes with LLDB_TEST_TIMEOUT. |
| 41 | E.g., LLDB_TEST_TIMEOUT=10m |
| 42 | Override the timeout for individual tests with LLDB_[TEST NAME]_TIMEOUT. |
| 43 | E.g., LLDB_TESTCONCURRENTEVENTS_TIMEOUT=2m |
| 44 | Set to "0" to run without timeout.""" |
| 45 | if timeout_command: |
| 46 | return subprocess.call([timeout_command, timeout] + command, |
| 47 | stdin=subprocess.PIPE) |
| 48 | return (ePassed if subprocess.call(command, stdin=subprocess.PIPE) == 0 |
| 49 | else eFailed) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 50 | |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 51 | def process_dir(root, files, test_root, dotest_options): |
| 52 | """Examine a directory for tests, and invoke any found within it.""" |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 53 | timed_out = [] |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 54 | failed = [] |
| 55 | passed = [] |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 56 | for name in files: |
| 57 | path = os.path.join(root, name) |
| 58 | |
| 59 | # We're only interested in the test file with the "Test*.py" naming pattern. |
| 60 | if not name.startswith("Test") or not name.endswith(".py"): |
| 61 | continue |
| 62 | |
| 63 | # Neither a symbolically linked file. |
| 64 | if os.path.islink(path): |
| 65 | continue |
| 66 | |
Zachary Turner | f6896b0 | 2015-01-05 19:37:03 +0000 | [diff] [blame] | 67 | script_file = os.path.join(test_root, "dotest.py") |
| 68 | is_posix = (os.name == "posix") |
| 69 | split_args = shlex.split(dotest_options, posix=is_posix) if dotest_options else [] |
| 70 | command = ([sys.executable, script_file] + |
| 71 | split_args + |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 72 | ["-p", name, root]) |
| 73 | |
| 74 | timeout_name = os.path.basename(os.path.splitext(name)[0]).upper() |
| 75 | |
| 76 | timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout |
| 77 | |
| 78 | exit_status = call_with_timeout(command, timeout) |
| 79 | |
| 80 | if ePassed == exit_status: |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 81 | passed.append(name) |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 82 | else: |
| 83 | if eTimedOut == exit_status: |
| 84 | timed_out.append(name) |
| 85 | failed.append(name) |
| 86 | return (timed_out, failed, passed) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 87 | |
| 88 | in_q = None |
| 89 | out_q = None |
| 90 | |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 91 | def process_dir_worker(arg_tuple): |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 92 | """Worker thread main loop when in multithreaded mode. |
| 93 | Takes one directory specification at a time and works on it.""" |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 94 | (root, files, test_root, dotest_options) = arg_tuple |
| 95 | return process_dir(root, files, test_root, dotest_options) |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 96 | |
| 97 | def walk_and_invoke(test_root, dotest_options, num_threads): |
| 98 | """Look for matched files and invoke test driver on each one. |
| 99 | In single-threaded mode, each test driver is invoked directly. |
| 100 | In multi-threaded mode, submit each test driver to a worker |
| 101 | queue, and then wait for all to complete.""" |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 102 | |
| 103 | # Collect the test files that we'll run. |
| 104 | test_work_items = [] |
| 105 | for root, dirs, files in os.walk(test_root, topdown=False): |
| 106 | test_work_items.append((root, files, test_root, dotest_options)) |
| 107 | |
| 108 | # Run the items, either in a pool (for multicore speedup) or |
| 109 | # calling each individually. |
| 110 | if num_threads > 1: |
| 111 | pool = multiprocessing.Pool(num_threads) |
| 112 | test_results = pool.map(process_dir_worker, test_work_items) |
| 113 | else: |
| 114 | test_results = [] |
| 115 | for work_item in test_work_items: |
| 116 | test_results.append(process_dir_worker(work_item)) |
| 117 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 118 | timed_out = [] |
Steve Pucci | befe2b1 | 2014-03-07 00:01:11 +0000 | [diff] [blame] | 119 | failed = [] |
| 120 | passed = [] |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 121 | |
| 122 | for test_result in test_results: |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 123 | (dir_timed_out, dir_failed, dir_passed) = test_result |
| 124 | timed_out += dir_timed_out |
Todd Fiala | 3f0a360 | 2014-07-08 06:42:37 +0000 | [diff] [blame] | 125 | failed += dir_failed |
| 126 | passed += dir_passed |
| 127 | |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 128 | return (timed_out, failed, passed) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 129 | |
| 130 | def main(): |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 131 | test_root = sys.path[0] |
| 132 | |
| 133 | parser = OptionParser(usage="""\ |
| 134 | Run lldb test suite using a separate process for each test file. |
| 135 | """) |
| 136 | parser.add_option('-o', '--options', |
| 137 | type='string', action='store', |
| 138 | dest='dotest_options', |
| 139 | help="""The options passed to 'dotest.py' if specified.""") |
| 140 | |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 141 | parser.add_option('-t', '--threads', |
| 142 | type='int', |
| 143 | dest='num_threads', |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 144 | help="""The number of threads to use when running tests separately.""") |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 145 | |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 146 | opts, args = parser.parse_args() |
| 147 | dotest_options = opts.dotest_options |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 148 | |
| 149 | if opts.num_threads: |
| 150 | num_threads = opts.num_threads |
| 151 | else: |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 152 | num_threads_str = os.environ.get("LLDB_TEST_THREADS") |
| 153 | if num_threads_str: |
| 154 | num_threads = int(num_threads_str) |
Greg Clayton | 2256d0d | 2014-03-24 23:01:57 +0000 | [diff] [blame] | 155 | else: |
Ed Maste | cec2a5b | 2014-11-21 02:41:25 +0000 | [diff] [blame] | 156 | num_threads = multiprocessing.cpu_count() |
| 157 | if num_threads < 1: |
| 158 | num_threads = 1 |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 159 | |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 160 | system_info = " ".join(platform.uname()) |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 161 | (timed_out, failed, passed) = walk_and_invoke(test_root, dotest_options, |
| 162 | num_threads) |
| 163 | timed_out = set(timed_out) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 164 | num_tests = len(failed) + len(passed) |
Daniel Malea | b42556f | 2013-04-19 18:32:53 +0000 | [diff] [blame] | 165 | |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 166 | print "Ran %d tests." % num_tests |
| 167 | if len(failed) > 0: |
Shawn Best | 13491c4 | 2014-10-22 19:29:00 +0000 | [diff] [blame] | 168 | failed.sort() |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 169 | print "Failing Tests (%d)" % len(failed) |
| 170 | for f in failed: |
Vince Harron | 17f429f | 2014-12-13 00:08:19 +0000 | [diff] [blame] | 171 | print "%s: LLDB (suite) :: %s (%s)" % ( |
| 172 | "TIMEOUT" if f in timed_out else "FAIL", f, system_info |
| 173 | ) |
Daniel Malea | cbaef26 | 2013-02-15 21:31:37 +0000 | [diff] [blame] | 174 | sys.exit(1) |
| 175 | sys.exit(0) |
Johnny Chen | e8d9dc6 | 2011-10-31 19:04:07 +0000 | [diff] [blame] | 176 | |
| 177 | if __name__ == '__main__': |
| 178 | main() |