Skip to content

Commit d25c766

Browse files
committed
[lit] Create derived classes for serial/parallel test runs
The hope is that with a little OO we can nicely factor out the differences. llvm-svn: 375128
1 parent d06a2f3 commit d25c766

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

llvm/utils/lit/lit/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ def progress_callback(test):
215215
if opts.incremental:
216216
update_incremental_cache(test)
217217

218-
run = lit.run.Run(tests, litConfig, progress_callback, opts.maxTime,
219-
opts.numWorkers)
218+
run = lit.run.create_run(tests, litConfig, opts.numWorkers,
219+
progress_callback, opts.maxTime)
220220

221221
try:
222-
elapsed = run_tests_in_tmp_dir(run.execute_tests, litConfig)
222+
elapsed = run_tests_in_tmp_dir(run.execute, litConfig)
223223
except KeyboardInterrupt:
224224
#TODO(yln): should we attempt to cleanup the progress bar here?
225225
sys.exit(2)

llvm/utils/lit/lit/run.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ class NopSemaphore(object):
1111
def acquire(self): pass
1212
def release(self): pass
1313

14+
def create_run(tests, lit_config, workers, progress_callback, max_time):
15+
if workers == 1:
16+
return SerialRun(tests, lit_config, progress_callback, max_time)
17+
return ParallelRun(tests, lit_config, progress_callback, max_time, workers)
18+
1419
class Run(object):
1520
"""A concrete, configured testing run."""
1621

17-
def __init__(self, tests, lit_config, progress_callback, max_time, workers):
22+
def __init__(self, tests, lit_config, progress_callback, max_time):
1823
self.tests = tests
1924
self.lit_config = lit_config
2025
self.progress_callback = progress_callback
2126
self.max_time = max_time
22-
self.workers = workers
2327

24-
def execute_tests(self):
28+
def execute(self):
2529
"""
2630
Execute the tests in the run using up to the specified number of
2731
parallel tasks, and inform the caller of each individual result. The
@@ -46,12 +50,7 @@ def execute_tests(self):
4650
self.hit_max_failures = False
4751

4852
start = time.time()
49-
50-
if self.workers == 1:
51-
self._execute_in_serial()
52-
else:
53-
self._execute_in_parallel()
54-
53+
self._execute()
5554
end = time.time()
5655

5756
# Mark any tests that weren't run as UNRESOLVED.
@@ -61,15 +60,53 @@ def execute_tests(self):
6160

6261
return end - start
6362

64-
def _execute_in_serial(self):
63+
def _consume_test_result(self, pool_result):
64+
"""Test completion callback for lit.worker.run_one_test
65+
66+
Updates the test result status in the parent process. Each task in the
67+
pool returns the test index and the result, and we use the index to look
68+
up the original test object. Also updates the progress bar as tasks
69+
complete.
70+
"""
71+
# Don't add any more test results after we've hit the maximum failure
72+
# count. Otherwise we're racing with the main thread, which is going
73+
# to terminate the process pool soon.
74+
if self.hit_max_failures:
75+
return
76+
77+
(test_index, test_with_result) = pool_result
78+
# Update the parent process copy of the test. This includes the result,
79+
# XFAILS, REQUIRES, and UNSUPPORTED statuses.
80+
assert self.tests[test_index].file_path == test_with_result.file_path, \
81+
"parent and child disagree on test path"
82+
self.tests[test_index] = test_with_result
83+
self.progress_callback(test_with_result)
84+
85+
# If we've finished all the tests or too many tests have failed, notify
86+
# the main thread that we've stopped testing.
87+
self.failure_count += (test_with_result.result.code == lit.Test.FAIL)
88+
if self.lit_config.maxFailures and \
89+
self.failure_count == self.lit_config.maxFailures:
90+
self.hit_max_failures = True
91+
92+
class SerialRun(Run):
93+
def __init__(self, tests, lit_config, progress_callback, max_time):
94+
super(SerialRun, self).__init__(tests, lit_config, progress_callback, max_time)
95+
96+
def _execute(self):
6597
# TODO(yln): ignores max_time
6698
for test_index, test in enumerate(self.tests):
6799
lit.worker._execute_test(test, self.lit_config)
68100
self._consume_test_result((test_index, test))
69101
if self.hit_max_failures:
70102
break
71103

72-
def _execute_in_parallel(self):
104+
class ParallelRun(Run):
105+
def __init__(self, tests, lit_config, progress_callback, max_time, workers):
106+
super(ParallelRun, self).__init__(tests, lit_config, progress_callback, max_time)
107+
self.workers = workers
108+
109+
def _execute(self):
73110
# We need to issue many wait calls, so compute the final deadline and
74111
# subtract time.time() from that as we go along.
75112
deadline = None
@@ -128,32 +165,3 @@ def console_ctrl_handler(type):
128165
raise
129166
finally:
130167
pool.join()
131-
132-
def _consume_test_result(self, pool_result):
133-
"""Test completion callback for lit.worker.run_one_test
134-
135-
Updates the test result status in the parent process. Each task in the
136-
pool returns the test index and the result, and we use the index to look
137-
up the original test object. Also updates the progress bar as tasks
138-
complete.
139-
"""
140-
# Don't add any more test results after we've hit the maximum failure
141-
# count. Otherwise we're racing with the main thread, which is going
142-
# to terminate the process pool soon.
143-
if self.hit_max_failures:
144-
return
145-
146-
(test_index, test_with_result) = pool_result
147-
# Update the parent process copy of the test. This includes the result,
148-
# XFAILS, REQUIRES, and UNSUPPORTED statuses.
149-
assert self.tests[test_index].file_path == test_with_result.file_path, \
150-
"parent and child disagree on test path"
151-
self.tests[test_index] = test_with_result
152-
self.progress_callback(test_with_result)
153-
154-
# If we've finished all the tests or too many tests have failed, notify
155-
# the main thread that we've stopped testing.
156-
self.failure_count += (test_with_result.result.code == lit.Test.FAIL)
157-
if self.lit_config.maxFailures and \
158-
self.failure_count == self.lit_config.maxFailures:
159-
self.hit_max_failures = True

0 commit comments

Comments
 (0)