| 1 |
import multiprocessing |
| 2 |
import sys |
| 3 |
import threading |
| 4 |
import time |
| 5 |
import unittest |
| 6 |
from concurrent import futures |
| 7 |
from concurrent.futures._base import ( |
| 8 |
PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, |
| 9 |
) |
| 10 |
from concurrent.futures.process import _check_system_limits |
| 11 |
|
| 12 |
from test import support |
| 13 |
from test.support import threading_helper |
| 14 |
|
| 15 |
|
| 16 |
def create_future(state=PENDING, exception=None, result=None): |
| 17 |
f = Future() |
| 18 |
f._state = state |
| 19 |
f._exception = exception |
| 20 |
f._result = result |
| 21 |
return f |
| 22 |
|
| 23 |
|
| 24 |
PENDING_FUTURE = create_future(state=PENDING) |
| 25 |
RUNNING_FUTURE = create_future(state=RUNNING) |
| 26 |
CANCELLED_FUTURE = create_future(state=CANCELLED) |
| 27 |
CANCELLED_AND_NOTIFIED_FUTURE = create_future(state=CANCELLED_AND_NOTIFIED) |
| 28 |
EXCEPTION_FUTURE = create_future(state=FINISHED, exception=OSError()) |
| 29 |
SUCCESSFUL_FUTURE = create_future(state=FINISHED, result=42) |
| 30 |
|
| 31 |
|
| 32 |
class BaseTestCase(unittest.TestCase): |
| 33 |
def setUp(self): |
| 34 |
self._thread_key = threading_helper.threading_setup() |
| 35 |
|
| 36 |
def tearDown(self): |
| 37 |
support.reap_children() |
| 38 |
threading_helper.threading_cleanup(*self._thread_key) |
| 39 |
|
| 40 |
|
| 41 |
class ExecutorMixin: |
| 42 |
worker_count = 5 |
| 43 |
executor_kwargs = {} |
| 44 |
|
| 45 |
def setUp(self): |
| 46 |
super().setUp() |
| 47 |
|
| 48 |
self.t1 = time.monotonic() |
| 49 |
if hasattr(self, "ctx"): |
| 50 |
self.executor = self.executor_type( |
| 51 |
max_workers=self.worker_count, |
| 52 |
mp_context=self.get_context(), |
| 53 |
**self.executor_kwargs) |
| 54 |
self.manager = self.get_context().Manager() |
| 55 |
else: |
| 56 |
self.executor = self.executor_type( |
| 57 |
max_workers=self.worker_count, |
| 58 |
**self.executor_kwargs) |
| 59 |
self.manager = None |
| 60 |
|
| 61 |
def tearDown(self): |
| 62 |
self.executor.shutdown(wait=True) |
| 63 |
self.executor = None |
| 64 |
if self.manager is not None: |
| 65 |
self.manager.shutdown() |
| 66 |
self.manager = None |
| 67 |
|
| 68 |
dt = time.monotonic() - self.t1 |
| 69 |
if support.verbose: |
| 70 |
print("%.2fs" % dt, end=' ') |
| 71 |
self.assertLess(dt, 300, "synchronization issue: test lasted too long") |
| 72 |
|
| 73 |
super().tearDown() |
| 74 |
|
| 75 |
def get_context(self): |
| 76 |
return multiprocessing.get_context(self.ctx) |
| 77 |
|
| 78 |
|
| 79 |
class ThreadPoolMixin(ExecutorMixin): |
| 80 |
executor_type = futures.ThreadPoolExecutor |
| 81 |
|
| 82 |
def create_event(self): |
| 83 |
return threading.Event() |
| 84 |
|
| 85 |
|
| 86 |
@support.skip_if_sanitizer("gh-129824: data races in InterpreterPool tests", thread=True) |
| 87 |
class InterpreterPoolMixin(ExecutorMixin): |
| 88 |
executor_type = futures.InterpreterPoolExecutor |
| 89 |
|
| 90 |
def create_event(self): |
| 91 |
self.skipTest("InterpreterPoolExecutor doesn't support events") |
| 92 |
|
| 93 |
|
| 94 |
class ProcessPoolForkMixin(ExecutorMixin): |
| 95 |
executor_type = futures.ProcessPoolExecutor |
| 96 |
ctx = "fork" |
| 97 |
|
| 98 |
def get_context(self): |
| 99 |
try: |
| 100 |
_check_system_limits() |
| 101 |
except NotImplementedError: |
| 102 |
self.skipTest("ProcessPoolExecutor unavailable on this system") |
| 103 |
if sys.platform == "win32": |
| 104 |
self.skipTest("require unix system") |
| 105 |
if support.check_sanitizer(thread=True): |
| 106 |
self.skipTest("TSAN doesn't support threads after fork") |
| 107 |
return super().get_context() |
| 108 |
|
| 109 |
def create_event(self): |
| 110 |
return self.manager.Event() |
| 111 |
|
| 112 |
|
| 113 |
class ProcessPoolSpawnMixin(ExecutorMixin): |
| 114 |
executor_type = futures.ProcessPoolExecutor |
| 115 |
ctx = "spawn" |
| 116 |
|
| 117 |
def get_context(self): |
| 118 |
try: |
| 119 |
_check_system_limits() |
| 120 |
except NotImplementedError: |
| 121 |
self.skipTest("ProcessPoolExecutor unavailable on this system") |
| 122 |
return super().get_context() |
| 123 |
|
| 124 |
def create_event(self): |
| 125 |
return self.manager.Event() |
| 126 |
|
| 127 |
|
| 128 |
class ProcessPoolForkserverMixin(ExecutorMixin): |
| 129 |
executor_type = futures.ProcessPoolExecutor |
| 130 |
ctx = "forkserver" |
| 131 |
|
| 132 |
def get_context(self): |
| 133 |
try: |
| 134 |
_check_system_limits() |
| 135 |
except NotImplementedError: |
| 136 |
self.skipTest("ProcessPoolExecutor unavailable on this system") |
| 137 |
if sys.platform == "win32": |
| 138 |
self.skipTest("require unix system") |
| 139 |
if support.check_sanitizer(thread=True): |
| 140 |
self.skipTest("TSAN doesn't support threads after fork") |
| 141 |
return super().get_context() |
| 142 |
|
| 143 |
def create_event(self): |
| 144 |
return self.manager.Event() |
| 145 |
|
| 146 |
|
| 147 |
def create_executor_tests(remote_globals, mixin, bases=(BaseTestCase,), |
| 148 |
executor_mixins=(ThreadPoolMixin, |
| 149 |
InterpreterPoolMixin, |
| 150 |
ProcessPoolForkMixin, |
| 151 |
ProcessPoolForkserverMixin, |
| 152 |
ProcessPoolSpawnMixin)): |
| 153 |
def strip_mixin(name): |
| 154 |
if name.endswith(('Mixin', 'Tests')): |
| 155 |
return name[:-5] |
| 156 |
elif name.endswith('Test'): |
| 157 |
return name[:-4] |
| 158 |
else: |
| 159 |
return name |
| 160 |
|
| 161 |
module = remote_globals['__name__'] |
| 162 |
for exe in executor_mixins: |
| 163 |
name = ("%s%sTest" |
| 164 |
% (strip_mixin(exe.__name__), strip_mixin(mixin.__name__))) |
| 165 |
cls = type(name, (mixin,) + (exe,) + bases, {'__module__': module}) |
| 166 |
remote_globals[name] = cls |
| 167 |
|
| 168 |
|
| 169 |
def setup_module(): |
| 170 |
try: |
| 171 |
_check_system_limits() |
| 172 |
except NotImplementedError: |
| 173 |
pass |
| 174 |
else: |
| 175 |
unittest.addModuleCleanup(multiprocessing.util._cleanup_tests) |
| 176 |
|
| 177 |
thread_info = threading_helper.threading_setup() |
| 178 |
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info) |