From f608ea31d6b1962420ee7439a972ac66f729a5d5 Mon Sep 17 00:00:00 2001 From: Peter Zahemszky <29452238+pzahemszky@users.noreply.github.com> Date: Sun, 3 Nov 2019 13:59:17 +0000 Subject: [PATCH 1/3] Add a regression test for scalative/haas#182 --- haas/tests/test_parallel_runner.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/haas/tests/test_parallel_runner.py b/haas/tests/test_parallel_runner.py index 55e689ce..9a2ccff1 100644 --- a/haas/tests/test_parallel_runner.py +++ b/haas/tests/test_parallel_runner.py @@ -1,5 +1,8 @@ +from __future__ import print_function + from argparse import ArgumentParser from datetime import datetime, timedelta +import sys import time from mock import Mock, patch @@ -25,6 +28,7 @@ def wait(self, timeout): def apply_async(func, args=None, kwargs=None, callback=None): + """ Run the given function serially (rather than in parallel) """ if args is None: args = () if kwargs is None: @@ -277,6 +281,35 @@ def test_parallel_runner_constructor_initializer(self, pool_class): pool.close.assert_called_once_with() pool.join.assert_called_once_with() + @patch('haas.plugins.parallel_runner.Pool') + def test_parallel_runner_stdout(self, pool_class): + # Given + pool = Mock() + pool_class.return_value = pool + pool.apply_async.side_effect = apply_async + + def test_method(): + print("STDOUT side effect of `test_method()`") + print("STDERR side effect", file=sys.stderr) + test_case = _test_cases.TestCase('test_method') + test_case.test_method = test_method + test_suite = TestSuite([test_case]) + + result_collector = ResultCollector() + runner = ParallelTestRunner() + + patch_stdout = patch('sys.stdout', new=StringIO()) + patch_stderr = patch('sys.stderr', new=StringIO()) + + # When + with patch_stdout as mock_stdout, patch_stderr as mock_stderr: + runner.run(result_collector, test_suite) + + # Then + self.assertEqual( + mock_stdout.getvalue(), "STDOUT side effect of `test_method()`\n") + self.assertEqual(mock_stderr.getvalue(), "STDERR side effect\n") + class TestParallelRunnerImportError(unittest.TestCase): From 19fd79b12c4de1a6b578e2a32a4ef9709d3dbdc4 Mon Sep 17 00:00:00 2001 From: Peter Zahemszky <29452238+pzahemszky@users.noreply.github.com> Date: Sun, 3 Nov 2019 18:30:15 +0000 Subject: [PATCH 2/3] Use CLI buffer setting for the parallel runner too --- haas/plugins/parallel_runner.py | 10 ++++++---- haas/tests/test_parallel_runner.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/haas/plugins/parallel_runner.py b/haas/plugins/parallel_runner.py index 803e0cb4..6622b54e 100644 --- a/haas/plugins/parallel_runner.py +++ b/haas/plugins/parallel_runner.py @@ -46,9 +46,9 @@ def __call__(self, result): self.results.append(result) -def _run_test_in_process(test_case): +def _run_test_in_process(test_case, buffer): result_handler = ChildResultHandler() - result_collector = ResultCollector(buffer=True) + result_collector = ResultCollector(buffer=buffer) result_collector.add_result_handler(result_handler) runner = BaseTestRunner() runner.run(result_collector, test_case) @@ -130,13 +130,15 @@ def callback(collected_result): if isinstance(test_case, ModuleImportError): error_tests.append(test_case) else: + # print result.buffer, result.failfast call_result = pool.apply_async( - _run_test_in_process, args=(test_case,), + _run_test_in_process, args=(test_case, result.buffer), callback=callback) call_results.append(call_result) for test_case in error_tests: - collected_result = _run_test_in_process(test_case) + collected_result = _run_test_in_process( + test_case, result.buffer) callback(collected_result) finally: pool.close() diff --git a/haas/tests/test_parallel_runner.py b/haas/tests/test_parallel_runner.py index 9a2ccff1..a8c72bbc 100644 --- a/haas/tests/test_parallel_runner.py +++ b/haas/tests/test_parallel_runner.py @@ -295,7 +295,7 @@ def test_method(): test_case.test_method = test_method test_suite = TestSuite([test_case]) - result_collector = ResultCollector() + result_collector = ResultCollector(buffer=False) runner = ParallelTestRunner() patch_stdout = patch('sys.stdout', new=StringIO()) From 5c7726b3b3c66ab771b8ff05beacbdf7597e6bc5 Mon Sep 17 00:00:00 2001 From: Peter Zahemszky <29452238+pzahemszky@users.noreply.github.com> Date: Sun, 3 Nov 2019 22:10:03 +0000 Subject: [PATCH 3/3] Remove a comment --- haas/plugins/parallel_runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/haas/plugins/parallel_runner.py b/haas/plugins/parallel_runner.py index 6622b54e..90f40597 100644 --- a/haas/plugins/parallel_runner.py +++ b/haas/plugins/parallel_runner.py @@ -130,7 +130,6 @@ def callback(collected_result): if isinstance(test_case, ModuleImportError): error_tests.append(test_case) else: - # print result.buffer, result.failfast call_result = pool.apply_async( _run_test_in_process, args=(test_case, result.buffer), callback=callback)