summaryrefslogtreecommitdiff
path: root/tests/test_concurrency.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-01-30 21:07:25 -0500
committerNed Batchelder <ned@nedbatchelder.com>2015-01-30 21:07:25 -0500
commit3be0790dd8e844ddb597fd09e3d768cbca85ae43 (patch)
tree91a4a613bcab209a232a8a7d7e6759372a7e03db /tests/test_concurrency.py
parent043d07dee3992d3e7381a41c3ff716234e766a1c (diff)
downloadpython-coveragepy-3be0790dd8e844ddb597fd09e3d768cbca85ae43.tar.gz
Wildly experimental multiprocessing support. Covers most of #117.
Diffstat (limited to 'tests/test_concurrency.py')
-rw-r--r--tests/test_concurrency.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py
index 77b8c0e..928f940 100644
--- a/tests/test_concurrency.py
+++ b/tests/test_concurrency.py
@@ -220,6 +220,51 @@ class ConcurrencyTest(CoverageTest):
self.try_some_code(BUG_330, "eventlet", eventlet, "0\n")
+class MultiprocessingTest(CoverageTest):
+ """Test support of the multiprocessing module."""
+
+ def test_multiprocessing(self):
+ self.make_file("multi.py", """\
+ import multiprocessing
+ import os
+ import time
+
+ def func(x):
+ # Need to pause, or the tasks go too quick, and some processes
+ # in the pool don't get any work, and then don't record data.
+ time.sleep(0.01)
+ if x % 2:
+ return os.getpid(), x*x
+ else:
+ return os.getpid(), x*x
+
+ if __name__ == "__main__":
+ pool = multiprocessing.Pool(3)
+ inputs = range(20)
+ outputs = pool.imap_unordered(func, inputs)
+ pids = set()
+ total = 0
+ for pid, sq in outputs:
+ pids.add(pid)
+ total += sq
+ print("%d pids, total = %d" % (len(pids), total))
+ pool.close()
+ pool.join()
+ """)
+
+ out = self.run_command(
+ "coverage run --concurrency=multiprocessing multi.py"
+ )
+ os.system("cp .cov* /tmp")
+ total = sum(x*x for x in range(20))
+ self.assertEqual(out.rstrip(), "3 pids, total = %d" % total)
+
+ self.run_command("coverage combine")
+ out = self.run_command("coverage report -m")
+ last_line = self.squeezed_lines(out)[-1]
+ self.assertEqual(last_line, "multi.py 20 0 100%")
+
+
def print_simple_annotation(code, linenos):
"""Print the lines in `code` with X for each line number in `linenos`."""
for lineno, line in enumerate(code.splitlines(), start=1):