Skip to content

Commit f32c6c3

Browse files
committedAug 3, 2023
Bug 1846617 - Use mozprocess.run_and_wait in cppunittests r=hneiva
Differential Revision: https://phabricator.services.mozilla.com/D185248
1 parent c5ec7c0 commit f32c6c3

File tree

2 files changed

+58
-40
lines changed

2 files changed

+58
-40
lines changed
 

‎testing/remotecppunittests.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ def build_environment(self):
119119
return env
120120

121121
def run_one_test(
122-
self, prog, env, symbols_path=None, interactive=False, timeout_factor=1
122+
self,
123+
prog,
124+
env,
125+
symbols_path=None,
126+
utility_path=None,
127+
interactive=False,
128+
timeout_factor=1,
123129
):
124130
"""
125131
Run a single C++ unit test program remotely.

‎testing/runcppunittests.py

+51-39
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ class CPPUnitTests(object):
3030
TEST_PROC_NO_OUTPUT_TIMEOUT = 300
3131

3232
def run_one_test(
33-
self, prog, env, symbols_path=None, interactive=False, timeout_factor=1
33+
self,
34+
prog,
35+
env,
36+
symbols_path=None,
37+
utility_path=None,
38+
interactive=False,
39+
timeout_factor=1,
3440
):
3541
"""
3642
Run a single C++ unit test program.
@@ -44,59 +50,65 @@ def run_one_test(
4450
4551
Return True if the program exits with a zero status, False otherwise.
4652
"""
53+
CPPUnitTests.run_one_test.timed_out = False
54+
output = []
55+
56+
def timeout_handler(proc):
57+
CPPUnitTests.run_one_test.timed_out = True
58+
message = "timed out after %d seconds" % CPPUnitTests.TEST_PROC_TIMEOUT
59+
self.log.test_end(
60+
basename, status="TIMEOUT", expected="PASS", message=message
61+
)
62+
mozcrash.kill_and_get_minidump(proc.pid, tempdir, utility_path)
63+
64+
def output_timeout_handler(proc):
65+
CPPUnitTests.run_one_test.timed_out = True
66+
message = (
67+
"timed out after %d seconds without output"
68+
% CPPUnitTests.TEST_PROC_NO_OUTPUT_TIMEOUT
69+
)
70+
self.log.test_end(
71+
basename, status="TIMEOUT", expected="PASS", message=message
72+
)
73+
mozcrash.kill_and_get_minidump(proc.pid, tempdir, utility_path)
74+
75+
def output_line_handler(_, line):
76+
fixed_line = self.fix_stack(line) if self.fix_stack else line
77+
if interactive:
78+
print(fixed_line)
79+
else:
80+
output.append(fixed_line)
81+
4782
basename = os.path.basename(prog)
4883
self.log.test_start(basename)
4984
with mozfile.TemporaryDirectory() as tempdir:
50-
if interactive:
51-
# For tests run locally, via mach, print output directly
52-
proc = mozprocess.ProcessHandler(
53-
[prog],
54-
cwd=tempdir,
55-
env=env,
56-
storeOutput=False,
57-
universal_newlines=True,
58-
)
59-
else:
60-
proc = mozprocess.ProcessHandler(
61-
[prog],
62-
cwd=tempdir,
63-
env=env,
64-
storeOutput=True,
65-
processOutputLine=lambda _: None,
66-
universal_newlines=True,
67-
)
68-
# TODO: After bug 811320 is fixed, don't let .run() kill the process,
69-
# instead use a timeout in .wait() and then kill to get a stack.
7085
test_timeout = CPPUnitTests.TEST_PROC_TIMEOUT * timeout_factor
71-
proc.run(
86+
proc = mozprocess.run_and_wait(
87+
[prog],
88+
cwd=tempdir,
89+
env=env,
90+
output_line_handler=output_line_handler,
7291
timeout=test_timeout,
73-
outputTimeout=CPPUnitTests.TEST_PROC_NO_OUTPUT_TIMEOUT,
92+
timeout_handler=timeout_handler,
93+
output_timeout=CPPUnitTests.TEST_PROC_NO_OUTPUT_TIMEOUT,
94+
output_timeout_handler=output_timeout_handler,
7495
)
75-
proc.wait()
76-
if proc.output:
77-
if self.fix_stack:
78-
procOutput = [self.fix_stack(l) for l in proc.output]
79-
else:
80-
procOutput = proc.output
81-
82-
output = "\n%s" % "\n".join(procOutput)
96+
97+
if output:
98+
output = "\n%s" % "\n".join(output)
8399
self.log.process_output(proc.pid, output, command=[prog])
84-
if proc.timedOut:
85-
message = "timed out after %d seconds" % CPPUnitTests.TEST_PROC_TIMEOUT
86-
self.log.test_end(
87-
basename, status="TIMEOUT", expected="PASS", message=message
88-
)
100+
if CPPUnitTests.run_one_test.timed_out:
89101
return False
90102
if mozcrash.check_for_crashes(tempdir, symbols_path, test_name=basename):
91103
self.log.test_end(basename, status="CRASH", expected="PASS")
92104
return False
93-
result = proc.proc.returncode == 0
105+
result = proc.returncode == 0
94106
if not result:
95107
self.log.test_end(
96108
basename,
97109
status="FAIL",
98110
expected="PASS",
99-
message=("test failed with return code %d" % proc.proc.returncode),
111+
message=("test failed with return code %d" % proc.returncode),
100112
)
101113
else:
102114
self.log.test_end(basename, status="PASS", expected="PASS")
@@ -201,7 +213,7 @@ def run_tests(
201213
test_path = prog[0]
202214
timeout_factor = prog[1]
203215
single_result = self.run_one_test(
204-
test_path, env, symbols_path, interactive, timeout_factor
216+
test_path, env, symbols_path, utility_path, interactive, timeout_factor
205217
)
206218
if single_result:
207219
pass_count += 1

0 commit comments

Comments
 (0)
Failed to load comments.