2 # SPDX-License-Identifier: GPL-2.0+
4 # Modified by: Corey Goldberg, 2013
7 # Bazaar (bzrlib.tests.__init__.py, v2.6, copied Jun 01 2013)
8 # Copyright (C) 2005-2011 Canonical Ltd
10 """Python testtools extension for running unittest suites concurrently.
12 The `testtools` project provides a ConcurrentTestSuite class, but does
13 not provide a `make_tests` implementation needed to use it.
15 This allows you to parallelize a test run across a configurable number
16 of worker processes. While this can speed up CPU-bound test runs, it is
17 mainly useful for IO-bound tests that spend most of their time waiting for
18 data to arrive from someplace else and can benefit from cocncurrency.
27 from itertools import cycle
28 from multiprocessing import cpu_count
30 from subunit import ProtocolTestCase, TestProtocolClient
31 from subunit.test_results import AutoTimingTestResultDecorator
33 from testtools import ConcurrentTestSuite, iterate_tests
37 'ConcurrentTestSuite',
43 CPU_COUNT = cpu_count()
46 def fork_for_tests(concurrency_num=CPU_COUNT):
47 """Implementation of `make_tests` used to construct `ConcurrentTestSuite`.
49 :param concurrency_num: number of processes to use.
52 """Take suite and start up multiple runners by forking (Unix only).
54 :param suite: TestSuite object.
56 :return: An iterable of TestCase-like objects which can each have
57 run(result) called on them to feed tests to result.
60 test_blocks = partition_tests(suite, concurrency_num)
61 # Clear the tests from the original suite so it doesn't keep them alive
63 for process_tests in test_blocks:
64 process_suite = unittest.TestSuite(process_tests)
65 # Also clear each split list so new suite has only reference
67 c2pread, c2pwrite = os.pipe()
71 stream = os.fdopen(c2pwrite, 'wb', 1)
73 # Leave stderr and stdout open so we can see test noise
74 # Close stdin so that the child goes away if it decides to
75 # read from stdin (otherwise its a roulette to see what
76 # child actually gets keystrokes for pdb etc).
78 subunit_result = AutoTimingTestResultDecorator(
79 TestProtocolClient(stream)
81 process_suite.run(subunit_result)
83 # Try and report traceback on stream, but exit with error
84 # even if stream couldn't be created or something else
85 # goes wrong. The traceback is formatted to a string and
86 # written in one go to avoid interleaving lines from
87 # multiple failing children.
89 stream.write(traceback.format_exc())
95 stream = os.fdopen(c2pread, 'rb', 1)
96 test = ProtocolTestCase(stream)
102 def partition_tests(suite, count):
103 """Partition suite into count lists of tests."""
104 # This just assigns tests in a round-robin fashion. On one hand this
105 # splits up blocks of related tests that might run faster if they shared
106 # resources, but on the other it avoids assigning blocks of slow tests to
107 # just one partition. So the slowest partition shouldn't be much slower
109 partitions = [list() for _ in range(count)]
110 tests = iterate_tests(suite)
111 for partition, test in zip(cycle(partitions), tests):
112 partition.append(test)
116 if __name__ == '__main__':
119 class SampleTestCase(unittest.TestCase):
120 """Dummy tests that sleep for demo."""
134 # Load tests from SampleTestCase defined above
135 suite = unittest.TestLoader().loadTestsFromTestCase(SampleTestCase)
136 runner = unittest.TextTestRunner()
138 # Run tests sequentially
141 # Run same tests across 4 processes
142 suite = unittest.TestLoader().loadTestsFromTestCase(SampleTestCase)
143 concurrent_suite = ConcurrentTestSuite(suite, fork_for_tests(4))
144 runner.run(concurrent_suite)