Make runner support single threaded mode

pull/20771/head
Lidi Zheng 5 years ago
parent 58cdbe7a83
commit e2dcb2883d
  1. 2
      src/python/grpcio_tests/commands.py
  2. 49
      src/python/grpcio_tests/tests/_runner.py

@ -127,7 +127,7 @@ class TestAio(setuptools.Command):
import tests import tests
loader = tests.Loader() loader = tests.Loader()
loader.loadTestsFromNames(['tests_aio']) loader.loadTestsFromNames(['tests_aio'])
runner = tests.Runner() runner = tests.Runner(dedicated_threads=False)
result = runner.run(loader.suite) result = runner.run(loader.suite)
if not result.wasSuccessful(): if not result.wasSuccessful():
sys.exit('Test failure') sys.exit('Test failure')

@ -117,8 +117,15 @@ class AugmentedCase(collections.namedtuple('AugmentedCase', ['case', 'id'])):
class Runner(object): class Runner(object):
def __init__(self): def __init__(self, dedicated_threads=True):
"""Constructs the Runner object.
Args:
dedicated_threads: A bool indicates whether to spawn each unit test
in separate thread or not.
"""
self._skipped_tests = [] self._skipped_tests = []
self._dedicated_threads = dedicated_threads
def skip_tests(self, tests): def skip_tests(self, tests):
self._skipped_tests = tests self._skipped_tests = tests
@ -194,24 +201,27 @@ class Runner(object):
sys.stdout.write('Running {}\n'.format( sys.stdout.write('Running {}\n'.format(
augmented_case.case.id())) augmented_case.case.id()))
sys.stdout.flush() sys.stdout.flush()
case_thread = threading.Thread( if self._dedicated_threads:
target=augmented_case.case.run, args=(result,)) case_thread = threading.Thread(
try: target=augmented_case.case.run, args=(result,))
with stdout_pipe, stderr_pipe: try:
case_thread.start() with stdout_pipe, stderr_pipe:
while case_thread.is_alive(): case_thread.start()
check_kill_self() while case_thread.is_alive():
time.sleep(0) check_kill_self()
case_thread.join() time.sleep(0)
except: # pylint: disable=try-except-raise case_thread.join()
# re-raise the exception after forcing the with-block to end except: # pylint: disable=try-except-raise
raise # re-raise the exception after forcing the with-block to end
result.set_output(augmented_case.case, stdout_pipe.output(), raise
stderr_pipe.output()) result.set_output(augmented_case.case, stdout_pipe.output(),
sys.stdout.write(result_out.getvalue()) stderr_pipe.output())
sys.stdout.flush() sys.stdout.write(result_out.getvalue())
result_out.truncate(0) sys.stdout.flush()
check_kill_self() result_out.truncate(0)
check_kill_self()
else:
augmented_case.case.run(result)
result.stopTestRun() result.stopTestRun()
stdout_pipe.close() stdout_pipe.close()
stderr_pipe.close() stderr_pipe.close()
@ -223,3 +233,4 @@ class Runner(object):
with open('report.xml', 'wb') as report_xml_file: with open('report.xml', 'wb') as report_xml_file:
_result.jenkins_junit_xml(result).write(report_xml_file) _result.jenkins_junit_xml(result).write(report_xml_file)
return result return result

Loading…
Cancel
Save