Skip to content

Commit

Permalink
Add a test to confirm #880 was a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Dec 17, 2019
1 parent 705d4e4 commit f8d5fcb
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tests/test_concurrency.py
Expand Up @@ -3,6 +3,7 @@

"""Tests for concurrency libraries."""

import glob
import os
import random
import sys
Expand Down Expand Up @@ -369,7 +370,7 @@ def setUp(self):
self.skipTest("No multiprocessing in this Python") # pragma: only jython

def try_multiprocessing_code(
self, code, expected_out, the_module, concurrency="multiprocessing"
self, code, expected_out, the_module, nprocs, concurrency="multiprocessing", args=""
):
"""Run code using multiprocessing, it should produce `expected_out`."""
self.make_file("multi.py", code)
Expand All @@ -388,13 +389,14 @@ def try_multiprocessing_code(
if start_method and start_method not in multiprocessing.get_all_start_methods():
continue

out = self.run_command("coverage run multi.py %s" % (start_method,))
out = self.run_command("coverage run %s multi.py %s" % (args, start_method))
expected_cant_trace = cant_trace_msg(concurrency, the_module)

if expected_cant_trace is not None:
self.assertEqual(out, expected_cant_trace)
else:
self.assertEqual(out.rstrip(), expected_out)
self.assertEqual(len(glob.glob(".coverage.*")), nprocs + 1)

out = self.run_command("coverage combine")
self.assertEqual(out, "")
Expand All @@ -409,7 +411,15 @@ def test_multiprocessing(self):
code = (SQUARE_OR_CUBE_WORK + MULTI_CODE).format(NPROCS=nprocs, UPTO=upto)
total = sum(x*x if x%2 else x*x*x for x in range(upto))
expected_out = "{nprocs} pids, total = {total}".format(nprocs=nprocs, total=total)
self.try_multiprocessing_code(code, expected_out, threading)
self.try_multiprocessing_code(code, expected_out, threading, nprocs)

def test_multiprocessing_append(self):
nprocs = 3
upto = 30
code = (SQUARE_OR_CUBE_WORK + MULTI_CODE).format(NPROCS=nprocs, UPTO=upto)
total = sum(x*x if x%2 else x*x*x for x in range(upto))
expected_out = "{nprocs} pids, total = {total}".format(nprocs=nprocs, total=total)
self.try_multiprocessing_code(code, expected_out, threading, nprocs, args="--append")

def test_multiprocessing_and_gevent(self):
nprocs = 3
Expand All @@ -420,7 +430,7 @@ def test_multiprocessing_and_gevent(self):
total = sum(sum(range((x + 1) * 100)) for x in range(upto))
expected_out = "{nprocs} pids, total = {total}".format(nprocs=nprocs, total=total)
self.try_multiprocessing_code(
code, expected_out, eventlet, concurrency="multiprocessing,eventlet"
code, expected_out, eventlet, nprocs, concurrency="multiprocessing,eventlet"
)

def try_multiprocessing_code_with_branching(self, code, expected_out):
Expand Down

0 comments on commit f8d5fcb

Please sign in to comment.