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