blob: 4571320d59f7f4a3bfc4d2347c5e23fb2d11b4d4 [file] [log] [blame]
Johnny Chene8d9dc62011-10-31 19:04:07 +00001#!/usr/bin/env python
2
3"""
4Run the test suite using a separate process for each test file.
5"""
6
Greg Clayton2256d0d2014-03-24 23:01:57 +00007import multiprocessing
Todd Fiala3f0a3602014-07-08 06:42:37 +00008import os
9import platform
Vince Harron17f429f2014-12-13 00:08:19 +000010import shlex
11import subprocess
Todd Fiala3f0a3602014-07-08 06:42:37 +000012import sys
Steve Puccibefe2b12014-03-07 00:01:11 +000013
Johnny Chene8d9dc62011-10-31 19:04:07 +000014from optparse import OptionParser
15
Vince Harron17f429f2014-12-13 00:08:19 +000016def 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
31timeout_command = get_timeout_command()
32
33default_timeout = os.getenv("LLDB_TEST_TIMEOUT") or "5m"
34
35# Status codes for running command with timeout.
36eTimedOut, ePassed, eFailed = 124, 0, 1
37
38def 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 Chene8d9dc62011-10-31 19:04:07 +000050
Steve Puccibefe2b12014-03-07 00:01:11 +000051def process_dir(root, files, test_root, dotest_options):
52 """Examine a directory for tests, and invoke any found within it."""
Vince Harron17f429f2014-12-13 00:08:19 +000053 timed_out = []
Daniel Maleacbaef262013-02-15 21:31:37 +000054 failed = []
55 passed = []
Steve Puccibefe2b12014-03-07 00:01:11 +000056 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
Vince Harron17f429f2014-12-13 00:08:19 +000067 command = ([sys.executable, "%s/dotest.py" % test_root] +
68 (shlex.split(dotest_options) if dotest_options else []) +
69 ["-p", name, root])
70
71 timeout_name = os.path.basename(os.path.splitext(name)[0]).upper()
72
73 timeout = os.getenv("LLDB_%s_TIMEOUT" % timeout_name) or default_timeout
74
75 exit_status = call_with_timeout(command, timeout)
76
77 if ePassed == exit_status:
Steve Puccibefe2b12014-03-07 00:01:11 +000078 passed.append(name)
Vince Harron17f429f2014-12-13 00:08:19 +000079 else:
80 if eTimedOut == exit_status:
81 timed_out.append(name)
82 failed.append(name)
83 return (timed_out, failed, passed)
Steve Puccibefe2b12014-03-07 00:01:11 +000084
85in_q = None
86out_q = None
87
Todd Fiala3f0a3602014-07-08 06:42:37 +000088def process_dir_worker(arg_tuple):
Steve Puccibefe2b12014-03-07 00:01:11 +000089 """Worker thread main loop when in multithreaded mode.
90 Takes one directory specification at a time and works on it."""
Todd Fiala3f0a3602014-07-08 06:42:37 +000091 (root, files, test_root, dotest_options) = arg_tuple
92 return process_dir(root, files, test_root, dotest_options)
Steve Puccibefe2b12014-03-07 00:01:11 +000093
94def walk_and_invoke(test_root, dotest_options, num_threads):
95 """Look for matched files and invoke test driver on each one.
96 In single-threaded mode, each test driver is invoked directly.
97 In multi-threaded mode, submit each test driver to a worker
98 queue, and then wait for all to complete."""
Todd Fiala3f0a3602014-07-08 06:42:37 +000099
100 # Collect the test files that we'll run.
101 test_work_items = []
102 for root, dirs, files in os.walk(test_root, topdown=False):
103 test_work_items.append((root, files, test_root, dotest_options))
104
105 # Run the items, either in a pool (for multicore speedup) or
106 # calling each individually.
107 if num_threads > 1:
108 pool = multiprocessing.Pool(num_threads)
109 test_results = pool.map(process_dir_worker, test_work_items)
110 else:
111 test_results = []
112 for work_item in test_work_items:
113 test_results.append(process_dir_worker(work_item))
114
Vince Harron17f429f2014-12-13 00:08:19 +0000115 timed_out = []
Steve Puccibefe2b12014-03-07 00:01:11 +0000116 failed = []
117 passed = []
Todd Fiala3f0a3602014-07-08 06:42:37 +0000118
119 for test_result in test_results:
Vince Harron17f429f2014-12-13 00:08:19 +0000120 (dir_timed_out, dir_failed, dir_passed) = test_result
121 timed_out += dir_timed_out
Todd Fiala3f0a3602014-07-08 06:42:37 +0000122 failed += dir_failed
123 passed += dir_passed
124
Vince Harron17f429f2014-12-13 00:08:19 +0000125 return (timed_out, failed, passed)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000126
127def main():
Johnny Chene8d9dc62011-10-31 19:04:07 +0000128 test_root = sys.path[0]
129
130 parser = OptionParser(usage="""\
131Run lldb test suite using a separate process for each test file.
132""")
133 parser.add_option('-o', '--options',
134 type='string', action='store',
135 dest='dotest_options',
136 help="""The options passed to 'dotest.py' if specified.""")
137
Greg Clayton2256d0d2014-03-24 23:01:57 +0000138 parser.add_option('-t', '--threads',
139 type='int',
140 dest='num_threads',
Ed Mastecec2a5b2014-11-21 02:41:25 +0000141 help="""The number of threads to use when running tests separately.""")
Greg Clayton2256d0d2014-03-24 23:01:57 +0000142
Johnny Chene8d9dc62011-10-31 19:04:07 +0000143 opts, args = parser.parse_args()
144 dotest_options = opts.dotest_options
Ed Mastecec2a5b2014-11-21 02:41:25 +0000145
146 if opts.num_threads:
147 num_threads = opts.num_threads
148 else:
Greg Clayton2256d0d2014-03-24 23:01:57 +0000149 num_threads_str = os.environ.get("LLDB_TEST_THREADS")
150 if num_threads_str:
151 num_threads = int(num_threads_str)
Greg Clayton2256d0d2014-03-24 23:01:57 +0000152 else:
Ed Mastecec2a5b2014-11-21 02:41:25 +0000153 num_threads = multiprocessing.cpu_count()
154 if num_threads < 1:
155 num_threads = 1
Johnny Chene8d9dc62011-10-31 19:04:07 +0000156
Daniel Maleab42556f2013-04-19 18:32:53 +0000157 system_info = " ".join(platform.uname())
Vince Harron17f429f2014-12-13 00:08:19 +0000158 (timed_out, failed, passed) = walk_and_invoke(test_root, dotest_options,
159 num_threads)
160 timed_out = set(timed_out)
Daniel Maleacbaef262013-02-15 21:31:37 +0000161 num_tests = len(failed) + len(passed)
Daniel Maleab42556f2013-04-19 18:32:53 +0000162
Daniel Maleacbaef262013-02-15 21:31:37 +0000163 print "Ran %d tests." % num_tests
164 if len(failed) > 0:
Shawn Best13491c42014-10-22 19:29:00 +0000165 failed.sort()
Daniel Maleacbaef262013-02-15 21:31:37 +0000166 print "Failing Tests (%d)" % len(failed)
167 for f in failed:
Vince Harron17f429f2014-12-13 00:08:19 +0000168 print "%s: LLDB (suite) :: %s (%s)" % (
169 "TIMEOUT" if f in timed_out else "FAIL", f, system_info
170 )
Daniel Maleacbaef262013-02-15 21:31:37 +0000171 sys.exit(1)
172 sys.exit(0)
Johnny Chene8d9dc62011-10-31 19:04:07 +0000173
174if __name__ == '__main__':
175 main()