Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1 | import test.support |
| 2 | |
| 3 | # Skip tests if _multiprocessing wasn't built. |
| 4 | test.support.import_module('_multiprocessing') |
| 5 | # Skip tests if sem_open implementation is broken. |
| 6 | test.support.import_module('multiprocessing.synchronize') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 7 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 8 | from test.support.script_helper import assert_python_ok |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 9 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 10 | import contextlib |
Ćukasz Langa | 574562c | 2017-09-29 14:33:34 -0700 | [diff] [blame] | 11 | import itertools |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 12 | import logging |
| 13 | from logging.handlers import QueueHandler |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 14 | import os |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 15 | import queue |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 16 | import sys |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 17 | import threading |
| 18 | import time |
| 19 | import unittest |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 20 | import weakref |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 21 | from pickle import PicklingError |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 22 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 23 | from concurrent import futures |
| 24 | from concurrent.futures._base import ( |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 25 | PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, |
| 26 | BrokenExecutor) |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 27 | from concurrent.futures.process import BrokenProcessPool |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 28 | from multiprocessing import get_context |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 29 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 30 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 31 | def create_future(state=PENDING, exception=None, result=None): |
| 32 | f = Future() |
| 33 | f._state = state |
| 34 | f._exception = exception |
| 35 | f._result = result |
| 36 | return f |
| 37 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 38 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 39 | PENDING_FUTURE = create_future(state=PENDING) |
| 40 | RUNNING_FUTURE = create_future(state=RUNNING) |
| 41 | CANCELLED_FUTURE = create_future(state=CANCELLED) |
| 42 | CANCELLED_AND_NOTIFIED_FUTURE = create_future(state=CANCELLED_AND_NOTIFIED) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 43 | EXCEPTION_FUTURE = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 44 | SUCCESSFUL_FUTURE = create_future(state=FINISHED, result=42) |
| 45 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 46 | INITIALIZER_STATUS = 'uninitialized' |
| 47 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 48 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 49 | def mul(x, y): |
| 50 | return x * y |
| 51 | |
Serhiy Storchaka | 42a139e | 2019-04-01 09:16:35 +0300 | [diff] [blame] | 52 | def capture(*args, **kwargs): |
| 53 | return args, kwargs |
| 54 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 55 | def sleep_and_raise(t): |
| 56 | time.sleep(t) |
| 57 | raise Exception('this is an exception') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 58 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 59 | def sleep_and_print(t, msg): |
| 60 | time.sleep(t) |
| 61 | print(msg) |
| 62 | sys.stdout.flush() |
| 63 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 64 | def init(x): |
| 65 | global INITIALIZER_STATUS |
| 66 | INITIALIZER_STATUS = x |
| 67 | |
| 68 | def get_init_status(): |
| 69 | return INITIALIZER_STATUS |
| 70 | |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 71 | def init_fail(log_queue=None): |
| 72 | if log_queue is not None: |
| 73 | logger = logging.getLogger('concurrent.futures') |
| 74 | logger.addHandler(QueueHandler(log_queue)) |
| 75 | logger.setLevel('CRITICAL') |
| 76 | logger.propagate = False |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 77 | time.sleep(0.1) # let some futures be scheduled |
| 78 | raise ValueError('error in initializer') |
| 79 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 80 | |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 81 | class MyObject(object): |
| 82 | def my_method(self): |
| 83 | pass |
| 84 | |
| 85 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 86 | class EventfulGCObj(): |
| 87 | def __init__(self, ctx): |
| 88 | mgr = get_context(ctx).Manager() |
| 89 | self.event = mgr.Event() |
| 90 | |
| 91 | def __del__(self): |
| 92 | self.event.set() |
| 93 | |
| 94 | |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 95 | def make_dummy_object(_): |
| 96 | return MyObject() |
| 97 | |
| 98 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 99 | class BaseTestCase(unittest.TestCase): |
| 100 | def setUp(self): |
| 101 | self._thread_key = test.support.threading_setup() |
| 102 | |
| 103 | def tearDown(self): |
| 104 | test.support.reap_children() |
| 105 | test.support.threading_cleanup(*self._thread_key) |
| 106 | |
| 107 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 108 | class ExecutorMixin: |
| 109 | worker_count = 5 |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 110 | executor_kwargs = {} |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 111 | |
| 112 | def setUp(self): |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 113 | super().setUp() |
Victor Stinner | 3df9dec | 2017-07-04 13:14:04 +0200 | [diff] [blame] | 114 | |
Pablo Galindo | 3ad8dec | 2018-06-21 12:30:37 +0100 | [diff] [blame] | 115 | self.t1 = time.monotonic() |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 116 | if hasattr(self, "ctx"): |
| 117 | self.executor = self.executor_type( |
| 118 | max_workers=self.worker_count, |
| 119 | mp_context=self.get_context(), |
| 120 | **self.executor_kwargs) |
| 121 | else: |
| 122 | self.executor = self.executor_type( |
| 123 | max_workers=self.worker_count, |
| 124 | **self.executor_kwargs) |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 125 | self._prime_executor() |
| 126 | |
| 127 | def tearDown(self): |
| 128 | self.executor.shutdown(wait=True) |
Victor Stinner | 3df9dec | 2017-07-04 13:14:04 +0200 | [diff] [blame] | 129 | self.executor = None |
| 130 | |
Pablo Galindo | 3ad8dec | 2018-06-21 12:30:37 +0100 | [diff] [blame] | 131 | dt = time.monotonic() - self.t1 |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 132 | if test.support.verbose: |
| 133 | print("%.2fs" % dt, end=' ') |
Pablo Galindo | 3ad8dec | 2018-06-21 12:30:37 +0100 | [diff] [blame] | 134 | self.assertLess(dt, 300, "synchronization issue: test lasted too long") |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 135 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 136 | super().tearDown() |
Victor Stinner | 3df9dec | 2017-07-04 13:14:04 +0200 | [diff] [blame] | 137 | |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 138 | def get_context(self): |
| 139 | return get_context(self.ctx) |
| 140 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 141 | def _prime_executor(self): |
| 142 | # Make sure that the executor is ready to do work before running the |
| 143 | # tests. This should reduce the probability of timeouts in the tests. |
| 144 | futures = [self.executor.submit(time.sleep, 0.1) |
| 145 | for _ in range(self.worker_count)] |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 146 | for f in futures: |
| 147 | f.result() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 148 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 149 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 150 | class ThreadPoolMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 151 | executor_type = futures.ThreadPoolExecutor |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 152 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 153 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 154 | class ProcessPoolForkMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 155 | executor_type = futures.ProcessPoolExecutor |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 156 | ctx = "fork" |
| 157 | |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 158 | def get_context(self): |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 159 | if sys.platform == "win32": |
| 160 | self.skipTest("require unix system") |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 161 | return super().get_context() |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 162 | |
| 163 | |
| 164 | class ProcessPoolSpawnMixin(ExecutorMixin): |
| 165 | executor_type = futures.ProcessPoolExecutor |
| 166 | ctx = "spawn" |
| 167 | |
| 168 | |
| 169 | class ProcessPoolForkserverMixin(ExecutorMixin): |
| 170 | executor_type = futures.ProcessPoolExecutor |
| 171 | ctx = "forkserver" |
| 172 | |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 173 | def get_context(self): |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 174 | if sys.platform == "win32": |
| 175 | self.skipTest("require unix system") |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 176 | return super().get_context() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 177 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 178 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 179 | def create_executor_tests(mixin, bases=(BaseTestCase,), |
| 180 | executor_mixins=(ThreadPoolMixin, |
| 181 | ProcessPoolForkMixin, |
| 182 | ProcessPoolForkserverMixin, |
| 183 | ProcessPoolSpawnMixin)): |
| 184 | def strip_mixin(name): |
| 185 | if name.endswith(('Mixin', 'Tests')): |
| 186 | return name[:-5] |
| 187 | elif name.endswith('Test'): |
| 188 | return name[:-4] |
| 189 | else: |
| 190 | return name |
| 191 | |
| 192 | for exe in executor_mixins: |
| 193 | name = ("%s%sTest" |
| 194 | % (strip_mixin(exe.__name__), strip_mixin(mixin.__name__))) |
| 195 | cls = type(name, (mixin,) + (exe,) + bases, {}) |
| 196 | globals()[name] = cls |
| 197 | |
| 198 | |
| 199 | class InitializerMixin(ExecutorMixin): |
| 200 | worker_count = 2 |
| 201 | |
| 202 | def setUp(self): |
| 203 | global INITIALIZER_STATUS |
| 204 | INITIALIZER_STATUS = 'uninitialized' |
| 205 | self.executor_kwargs = dict(initializer=init, |
| 206 | initargs=('initialized',)) |
| 207 | super().setUp() |
| 208 | |
| 209 | def test_initializer(self): |
| 210 | futures = [self.executor.submit(get_init_status) |
| 211 | for _ in range(self.worker_count)] |
| 212 | |
| 213 | for f in futures: |
| 214 | self.assertEqual(f.result(), 'initialized') |
| 215 | |
| 216 | |
| 217 | class FailingInitializerMixin(ExecutorMixin): |
| 218 | worker_count = 2 |
| 219 | |
| 220 | def setUp(self): |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 221 | if hasattr(self, "ctx"): |
| 222 | # Pass a queue to redirect the child's logging output |
| 223 | self.mp_context = self.get_context() |
| 224 | self.log_queue = self.mp_context.Queue() |
| 225 | self.executor_kwargs = dict(initializer=init_fail, |
| 226 | initargs=(self.log_queue,)) |
| 227 | else: |
| 228 | # In a thread pool, the child shares our logging setup |
| 229 | # (see _assert_logged()) |
| 230 | self.mp_context = None |
| 231 | self.log_queue = None |
| 232 | self.executor_kwargs = dict(initializer=init_fail) |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 233 | super().setUp() |
| 234 | |
| 235 | def test_initializer(self): |
| 236 | with self._assert_logged('ValueError: error in initializer'): |
| 237 | try: |
| 238 | future = self.executor.submit(get_init_status) |
| 239 | except BrokenExecutor: |
| 240 | # Perhaps the executor is already broken |
| 241 | pass |
| 242 | else: |
| 243 | with self.assertRaises(BrokenExecutor): |
| 244 | future.result() |
| 245 | # At some point, the executor should break |
Pablo Galindo | 3ad8dec | 2018-06-21 12:30:37 +0100 | [diff] [blame] | 246 | t1 = time.monotonic() |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 247 | while not self.executor._broken: |
Pablo Galindo | 3ad8dec | 2018-06-21 12:30:37 +0100 | [diff] [blame] | 248 | if time.monotonic() - t1 > 5: |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 249 | self.fail("executor not broken after 5 s.") |
| 250 | time.sleep(0.01) |
| 251 | # ... and from this point submit() is guaranteed to fail |
| 252 | with self.assertRaises(BrokenExecutor): |
| 253 | self.executor.submit(get_init_status) |
| 254 | |
| 255 | def _prime_executor(self): |
| 256 | pass |
| 257 | |
| 258 | @contextlib.contextmanager |
| 259 | def _assert_logged(self, msg): |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 260 | if self.log_queue is not None: |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 261 | yield |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 262 | output = [] |
| 263 | try: |
| 264 | while True: |
| 265 | output.append(self.log_queue.get_nowait().getMessage()) |
| 266 | except queue.Empty: |
| 267 | pass |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 268 | else: |
| 269 | with self.assertLogs('concurrent.futures', 'CRITICAL') as cm: |
| 270 | yield |
Antoine Pitrou | 0a2ff23 | 2017-11-09 15:33:43 +0100 | [diff] [blame] | 271 | output = cm.output |
| 272 | self.assertTrue(any(msg in line for line in output), |
| 273 | output) |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 274 | |
| 275 | |
| 276 | create_executor_tests(InitializerMixin) |
| 277 | create_executor_tests(FailingInitializerMixin) |
| 278 | |
| 279 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 280 | class ExecutorShutdownTest: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 281 | def test_run_after_shutdown(self): |
| 282 | self.executor.shutdown() |
| 283 | self.assertRaises(RuntimeError, |
| 284 | self.executor.submit, |
| 285 | pow, 2, 5) |
| 286 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 287 | def test_interpreter_shutdown(self): |
| 288 | # Test the atexit hook for shutdown of worker threads and processes |
| 289 | rc, out, err = assert_python_ok('-c', """if 1: |
| 290 | from concurrent.futures import {executor_type} |
| 291 | from time import sleep |
| 292 | from test.test_concurrent_futures import sleep_and_print |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 293 | if __name__ == "__main__": |
| 294 | context = '{context}' |
| 295 | if context == "": |
| 296 | t = {executor_type}(5) |
| 297 | else: |
| 298 | from multiprocessing import get_context |
| 299 | context = get_context(context) |
| 300 | t = {executor_type}(5, mp_context=context) |
| 301 | t.submit(sleep_and_print, 1.0, "apple") |
| 302 | """.format(executor_type=self.executor_type.__name__, |
| 303 | context=getattr(self, "ctx", ""))) |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 304 | # Errors in atexit hooks don't change the process exit code, check |
| 305 | # stderr manually. |
| 306 | self.assertFalse(err) |
| 307 | self.assertEqual(out.strip(), b"apple") |
| 308 | |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 309 | def test_submit_after_interpreter_shutdown(self): |
| 310 | # Test the atexit hook for shutdown of worker threads and processes |
| 311 | rc, out, err = assert_python_ok('-c', """if 1: |
| 312 | import atexit |
| 313 | @atexit.register |
| 314 | def run_last(): |
| 315 | try: |
| 316 | t.submit(id, None) |
| 317 | except RuntimeError: |
| 318 | print("runtime-error") |
| 319 | raise |
| 320 | from concurrent.futures import {executor_type} |
| 321 | if __name__ == "__main__": |
| 322 | context = '{context}' |
| 323 | if not context: |
| 324 | t = {executor_type}(5) |
| 325 | else: |
| 326 | from multiprocessing import get_context |
| 327 | context = get_context(context) |
| 328 | t = {executor_type}(5, mp_context=context) |
| 329 | t.submit(id, 42).result() |
| 330 | """.format(executor_type=self.executor_type.__name__, |
| 331 | context=getattr(self, "ctx", ""))) |
| 332 | # Errors in atexit hooks don't change the process exit code, check |
| 333 | # stderr manually. |
| 334 | self.assertIn("RuntimeError: cannot schedule new futures", err.decode()) |
| 335 | self.assertEqual(out.strip(), b"runtime-error") |
| 336 | |
Ross Lagerwall | 66e2fb6 | 2012-01-08 08:29:40 +0200 | [diff] [blame] | 337 | def test_hang_issue12364(self): |
| 338 | fs = [self.executor.submit(time.sleep, 0.1) for _ in range(50)] |
| 339 | self.executor.shutdown() |
| 340 | for f in fs: |
| 341 | f.result() |
| 342 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 343 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 344 | class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 345 | def _prime_executor(self): |
| 346 | pass |
| 347 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 348 | def test_threads_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 349 | self.executor.submit(mul, 21, 2) |
| 350 | self.executor.submit(mul, 6, 7) |
| 351 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 352 | self.assertEqual(len(self.executor._threads), 3) |
| 353 | self.executor.shutdown() |
| 354 | for t in self.executor._threads: |
| 355 | t.join() |
| 356 | |
| 357 | def test_context_manager_shutdown(self): |
| 358 | with futures.ThreadPoolExecutor(max_workers=5) as e: |
| 359 | executor = e |
| 360 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 361 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 362 | |
| 363 | for t in executor._threads: |
| 364 | t.join() |
| 365 | |
| 366 | def test_del_shutdown(self): |
| 367 | executor = futures.ThreadPoolExecutor(max_workers=5) |
| 368 | executor.map(abs, range(-5, 5)) |
| 369 | threads = executor._threads |
| 370 | del executor |
| 371 | |
| 372 | for t in threads: |
| 373 | t.join() |
| 374 | |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 375 | def test_thread_names_assigned(self): |
| 376 | executor = futures.ThreadPoolExecutor( |
| 377 | max_workers=5, thread_name_prefix='SpecialPool') |
| 378 | executor.map(abs, range(-5, 5)) |
| 379 | threads = executor._threads |
| 380 | del executor |
| 381 | |
| 382 | for t in threads: |
| 383 | self.assertRegex(t.name, r'^SpecialPool_[0-4]$') |
| 384 | t.join() |
| 385 | |
| 386 | def test_thread_names_default(self): |
| 387 | executor = futures.ThreadPoolExecutor(max_workers=5) |
| 388 | executor.map(abs, range(-5, 5)) |
| 389 | threads = executor._threads |
| 390 | del executor |
| 391 | |
| 392 | for t in threads: |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 393 | # Ensure that our default name is reasonably sane and unique when |
| 394 | # no thread_name_prefix was supplied. |
| 395 | self.assertRegex(t.name, r'ThreadPoolExecutor-\d+_[0-4]$') |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 396 | t.join() |
| 397 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 398 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 399 | class ProcessPoolShutdownTest(ExecutorShutdownTest): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 400 | def _prime_executor(self): |
| 401 | pass |
| 402 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 403 | def test_processes_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 404 | self.executor.submit(mul, 21, 2) |
| 405 | self.executor.submit(mul, 6, 7) |
| 406 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 407 | self.assertEqual(len(self.executor._processes), 5) |
| 408 | processes = self.executor._processes |
| 409 | self.executor.shutdown() |
| 410 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 411 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 412 | p.join() |
| 413 | |
| 414 | def test_context_manager_shutdown(self): |
| 415 | with futures.ProcessPoolExecutor(max_workers=5) as e: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 416 | processes = e._processes |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 417 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 418 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 419 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 420 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 421 | p.join() |
| 422 | |
| 423 | def test_del_shutdown(self): |
| 424 | executor = futures.ProcessPoolExecutor(max_workers=5) |
| 425 | list(executor.map(abs, range(-5, 5))) |
| 426 | queue_management_thread = executor._queue_management_thread |
| 427 | processes = executor._processes |
Victor Stinner | 3bcf157 | 2017-09-12 17:05:53 -0700 | [diff] [blame] | 428 | call_queue = executor._call_queue |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 429 | queue_management_thread = executor._queue_management_thread |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 430 | del executor |
| 431 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 432 | # Make sure that all the executor resources were properly cleaned by |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 433 | # the shutdown process |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 434 | queue_management_thread.join() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 435 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 436 | p.join() |
Victor Stinner | 3bcf157 | 2017-09-12 17:05:53 -0700 | [diff] [blame] | 437 | call_queue.join_thread() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 439 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 440 | create_executor_tests(ProcessPoolShutdownTest, |
| 441 | executor_mixins=(ProcessPoolForkMixin, |
| 442 | ProcessPoolForkserverMixin, |
| 443 | ProcessPoolSpawnMixin)) |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 444 | |
| 445 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 446 | class WaitTests: |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 447 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 448 | def test_first_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 449 | future1 = self.executor.submit(mul, 21, 2) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 450 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 451 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 452 | done, not_done = futures.wait( |
| 453 | [CANCELLED_FUTURE, future1, future2], |
| 454 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 455 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 456 | self.assertEqual(set([future1]), done) |
| 457 | self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 458 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 459 | def test_first_completed_some_already_completed(self): |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 460 | future1 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 461 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 462 | finished, pending = futures.wait( |
| 463 | [CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], |
| 464 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 465 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 466 | self.assertEqual( |
| 467 | set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), |
| 468 | finished) |
| 469 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 470 | |
| 471 | def test_first_exception(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 472 | future1 = self.executor.submit(mul, 2, 21) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 473 | future2 = self.executor.submit(sleep_and_raise, 1.5) |
| 474 | future3 = self.executor.submit(time.sleep, 3) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 475 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 476 | finished, pending = futures.wait( |
| 477 | [future1, future2, future3], |
| 478 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 479 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 480 | self.assertEqual(set([future1, future2]), finished) |
| 481 | self.assertEqual(set([future3]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 482 | |
| 483 | def test_first_exception_some_already_complete(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 484 | future1 = self.executor.submit(divmod, 21, 0) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 485 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 486 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 487 | finished, pending = futures.wait( |
| 488 | [SUCCESSFUL_FUTURE, |
| 489 | CANCELLED_FUTURE, |
| 490 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 491 | future1, future2], |
| 492 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 493 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 494 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 495 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 496 | future1]), finished) |
| 497 | self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 498 | |
| 499 | def test_first_exception_one_already_failed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 500 | future1 = self.executor.submit(time.sleep, 2) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 501 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 502 | finished, pending = futures.wait( |
| 503 | [EXCEPTION_FUTURE, future1], |
| 504 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 505 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 506 | self.assertEqual(set([EXCEPTION_FUTURE]), finished) |
| 507 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 508 | |
| 509 | def test_all_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 510 | future1 = self.executor.submit(divmod, 2, 0) |
| 511 | future2 = self.executor.submit(mul, 2, 21) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 512 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 513 | finished, pending = futures.wait( |
| 514 | [SUCCESSFUL_FUTURE, |
| 515 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 516 | EXCEPTION_FUTURE, |
| 517 | future1, |
| 518 | future2], |
| 519 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 520 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 521 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 522 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 523 | EXCEPTION_FUTURE, |
| 524 | future1, |
| 525 | future2]), finished) |
| 526 | self.assertEqual(set(), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 527 | |
| 528 | def test_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 529 | future1 = self.executor.submit(mul, 6, 7) |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 530 | future2 = self.executor.submit(time.sleep, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 531 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 532 | finished, pending = futures.wait( |
| 533 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 534 | EXCEPTION_FUTURE, |
| 535 | SUCCESSFUL_FUTURE, |
| 536 | future1, future2], |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 537 | timeout=5, |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 538 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 539 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 540 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 541 | EXCEPTION_FUTURE, |
| 542 | SUCCESSFUL_FUTURE, |
| 543 | future1]), finished) |
| 544 | self.assertEqual(set([future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 545 | |
| 546 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 547 | class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, BaseTestCase): |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 548 | |
| 549 | def test_pending_calls_race(self): |
| 550 | # Issue #14406: multi-threaded race condition when waiting on all |
| 551 | # futures. |
| 552 | event = threading.Event() |
| 553 | def future_func(): |
| 554 | event.wait() |
| 555 | oldswitchinterval = sys.getswitchinterval() |
| 556 | sys.setswitchinterval(1e-6) |
| 557 | try: |
| 558 | fs = {self.executor.submit(future_func) for i in range(100)} |
| 559 | event.set() |
| 560 | futures.wait(fs, return_when=futures.ALL_COMPLETED) |
| 561 | finally: |
| 562 | sys.setswitchinterval(oldswitchinterval) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 563 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 565 | create_executor_tests(WaitTests, |
| 566 | executor_mixins=(ProcessPoolForkMixin, |
| 567 | ProcessPoolForkserverMixin, |
| 568 | ProcessPoolSpawnMixin)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 569 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 571 | class AsCompletedTests: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 572 | # TODO(brian@sweetapp.com): Should have a test with a non-zero timeout. |
| 573 | def test_no_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 574 | future1 = self.executor.submit(mul, 2, 21) |
| 575 | future2 = self.executor.submit(mul, 7, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 576 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 577 | completed = set(futures.as_completed( |
| 578 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 579 | EXCEPTION_FUTURE, |
| 580 | SUCCESSFUL_FUTURE, |
| 581 | future1, future2])) |
| 582 | self.assertEqual(set( |
| 583 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 584 | EXCEPTION_FUTURE, |
| 585 | SUCCESSFUL_FUTURE, |
| 586 | future1, future2]), |
| 587 | completed) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 588 | |
| 589 | def test_zero_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 590 | future1 = self.executor.submit(time.sleep, 2) |
| 591 | completed_futures = set() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 592 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 593 | for future in futures.as_completed( |
| 594 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 595 | EXCEPTION_FUTURE, |
| 596 | SUCCESSFUL_FUTURE, |
| 597 | future1], |
| 598 | timeout=0): |
| 599 | completed_futures.add(future) |
| 600 | except futures.TimeoutError: |
| 601 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 602 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 603 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 604 | EXCEPTION_FUTURE, |
| 605 | SUCCESSFUL_FUTURE]), |
| 606 | completed_futures) |
| 607 | |
Guido van Rossum | e6994ff | 2014-01-26 09:57:51 -0800 | [diff] [blame] | 608 | def test_duplicate_futures(self): |
| 609 | # Issue 20367. Duplicate futures should not raise exceptions or give |
| 610 | # duplicate responses. |
Ćukasz Langa | 574562c | 2017-09-29 14:33:34 -0700 | [diff] [blame] | 611 | # Issue #31641: accept arbitrary iterables. |
Guido van Rossum | e6994ff | 2014-01-26 09:57:51 -0800 | [diff] [blame] | 612 | future1 = self.executor.submit(time.sleep, 2) |
Ćukasz Langa | 574562c | 2017-09-29 14:33:34 -0700 | [diff] [blame] | 613 | completed = [ |
| 614 | f for f in futures.as_completed(itertools.repeat(future1, 3)) |
| 615 | ] |
Guido van Rossum | e6994ff | 2014-01-26 09:57:51 -0800 | [diff] [blame] | 616 | self.assertEqual(len(completed), 1) |
| 617 | |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 618 | def test_free_reference_yielded_future(self): |
| 619 | # Issue #14406: Generator should not keep references |
| 620 | # to finished futures. |
| 621 | futures_list = [Future() for _ in range(8)] |
| 622 | futures_list.append(create_future(state=CANCELLED_AND_NOTIFIED)) |
Antoine Pitrou | 2ef3760 | 2017-09-03 15:09:23 +0200 | [diff] [blame] | 623 | futures_list.append(create_future(state=FINISHED, result=42)) |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 624 | |
| 625 | with self.assertRaises(futures.TimeoutError): |
| 626 | for future in futures.as_completed(futures_list, timeout=0): |
| 627 | futures_list.remove(future) |
| 628 | wr = weakref.ref(future) |
| 629 | del future |
| 630 | self.assertIsNone(wr()) |
| 631 | |
| 632 | futures_list[0].set_result("test") |
| 633 | for future in futures.as_completed(futures_list): |
| 634 | futures_list.remove(future) |
| 635 | wr = weakref.ref(future) |
| 636 | del future |
| 637 | self.assertIsNone(wr()) |
| 638 | if futures_list: |
| 639 | futures_list[0].set_result("test") |
| 640 | |
| 641 | def test_correct_timeout_exception_msg(self): |
| 642 | futures_list = [CANCELLED_AND_NOTIFIED_FUTURE, PENDING_FUTURE, |
| 643 | RUNNING_FUTURE, SUCCESSFUL_FUTURE] |
| 644 | |
| 645 | with self.assertRaises(futures.TimeoutError) as cm: |
| 646 | list(futures.as_completed(futures_list, timeout=0)) |
| 647 | |
| 648 | self.assertEqual(str(cm.exception), '2 (of 4) futures unfinished') |
| 649 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 651 | create_executor_tests(AsCompletedTests) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 652 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 654 | class ExecutorTest: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 655 | # Executor.shutdown() and context manager usage is tested by |
| 656 | # ExecutorShutdownTest. |
| 657 | def test_submit(self): |
| 658 | future = self.executor.submit(pow, 2, 8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 659 | self.assertEqual(256, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 660 | |
| 661 | def test_submit_keyword(self): |
| 662 | future = self.executor.submit(mul, 2, y=8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 663 | self.assertEqual(16, future.result()) |
Serhiy Storchaka | 42a139e | 2019-04-01 09:16:35 +0300 | [diff] [blame] | 664 | future = self.executor.submit(capture, 1, self=2, fn=3) |
| 665 | self.assertEqual(future.result(), ((1,), {'self': 2, 'fn': 3})) |
| 666 | with self.assertWarns(DeprecationWarning): |
| 667 | future = self.executor.submit(fn=capture, arg=1) |
| 668 | self.assertEqual(future.result(), ((), {'arg': 1})) |
| 669 | with self.assertRaises(TypeError): |
| 670 | self.executor.submit(arg=1) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 671 | |
| 672 | def test_map(self): |
| 673 | self.assertEqual( |
| 674 | list(self.executor.map(pow, range(10), range(10))), |
| 675 | list(map(pow, range(10), range(10)))) |
| 676 | |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 677 | self.assertEqual( |
| 678 | list(self.executor.map(pow, range(10), range(10), chunksize=3)), |
| 679 | list(map(pow, range(10), range(10)))) |
| 680 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 681 | def test_map_exception(self): |
| 682 | i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) |
| 683 | self.assertEqual(i.__next__(), (0, 1)) |
| 684 | self.assertEqual(i.__next__(), (0, 1)) |
| 685 | self.assertRaises(ZeroDivisionError, i.__next__) |
| 686 | |
| 687 | def test_map_timeout(self): |
| 688 | results = [] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 689 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 690 | for i in self.executor.map(time.sleep, |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 691 | [0, 0, 6], |
| 692 | timeout=5): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 693 | results.append(i) |
| 694 | except futures.TimeoutError: |
| 695 | pass |
| 696 | else: |
| 697 | self.fail('expected TimeoutError') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 698 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 699 | self.assertEqual([None, None], results) |
| 700 | |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 701 | def test_shutdown_race_issue12456(self): |
| 702 | # Issue #12456: race condition at shutdown where trying to post a |
| 703 | # sentinel in the call queue blocks (the queue is full while processes |
| 704 | # have exited). |
| 705 | self.executor.map(str, [2] * (self.worker_count + 1)) |
| 706 | self.executor.shutdown() |
| 707 | |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 708 | @test.support.cpython_only |
| 709 | def test_no_stale_references(self): |
| 710 | # Issue #16284: check that the executors don't unnecessarily hang onto |
| 711 | # references. |
| 712 | my_object = MyObject() |
| 713 | my_object_collected = threading.Event() |
| 714 | my_object_callback = weakref.ref( |
| 715 | my_object, lambda obj: my_object_collected.set()) |
| 716 | # Deliberately discarding the future. |
| 717 | self.executor.submit(my_object.my_method) |
| 718 | del my_object |
| 719 | |
| 720 | collected = my_object_collected.wait(timeout=5.0) |
| 721 | self.assertTrue(collected, |
| 722 | "Stale reference not collected within timeout.") |
| 723 | |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 724 | def test_max_workers_negative(self): |
| 725 | for number in (0, -1): |
R David Murray | 475a476 | 2014-06-11 16:25:05 -0400 | [diff] [blame] | 726 | with self.assertRaisesRegex(ValueError, |
| 727 | "max_workers must be greater " |
| 728 | "than 0"): |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 729 | self.executor_type(max_workers=number) |
| 730 | |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 731 | def test_free_reference(self): |
| 732 | # Issue #14406: Result iterator should not keep an internal |
| 733 | # reference to result objects. |
| 734 | for obj in self.executor.map(make_dummy_object, range(10)): |
| 735 | wr = weakref.ref(obj) |
| 736 | del obj |
| 737 | self.assertIsNone(wr()) |
| 738 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 739 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 740 | class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, BaseTestCase): |
Brian Quinlan | f007876 | 2011-04-08 08:19:33 +1000 | [diff] [blame] | 741 | def test_map_submits_without_iteration(self): |
| 742 | """Tests verifying issue 11777.""" |
| 743 | finished = [] |
| 744 | def record_finished(n): |
| 745 | finished.append(n) |
| 746 | |
| 747 | self.executor.map(record_finished, range(10)) |
| 748 | self.executor.shutdown(wait=True) |
| 749 | self.assertCountEqual(finished, range(10)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 750 | |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 751 | def test_default_workers(self): |
| 752 | executor = self.executor_type() |
| 753 | self.assertEqual(executor._max_workers, |
| 754 | (os.cpu_count() or 1) * 5) |
| 755 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 756 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 757 | class ProcessPoolExecutorTest(ExecutorTest): |
Brian Quinlan | 3988986 | 2019-05-08 14:04:53 -0400 | [diff] [blame] | 758 | |
| 759 | @unittest.skipUnless(sys.platform=='win32', 'Windows-only process limit') |
| 760 | def test_max_workers_too_large(self): |
| 761 | with self.assertRaisesRegex(ValueError, |
| 762 | "max_workers must be <= 61"): |
| 763 | futures.ProcessPoolExecutor(max_workers=62) |
| 764 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 765 | def test_killed_child(self): |
| 766 | # When a child process is abruptly terminated, the whole pool gets |
| 767 | # "broken". |
| 768 | futures = [self.executor.submit(time.sleep, 3)] |
| 769 | # Get one of the processes, and terminate (kill) it |
| 770 | p = next(iter(self.executor._processes.values())) |
| 771 | p.terminate() |
| 772 | for fut in futures: |
| 773 | self.assertRaises(BrokenProcessPool, fut.result) |
| 774 | # Submitting other jobs fails as well. |
| 775 | self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 777 | def test_map_chunksize(self): |
| 778 | def bad_map(): |
| 779 | list(self.executor.map(pow, range(40), range(40), chunksize=-1)) |
| 780 | |
| 781 | ref = list(map(pow, range(40), range(40))) |
| 782 | self.assertEqual( |
| 783 | list(self.executor.map(pow, range(40), range(40), chunksize=6)), |
| 784 | ref) |
| 785 | self.assertEqual( |
| 786 | list(self.executor.map(pow, range(40), range(40), chunksize=50)), |
| 787 | ref) |
| 788 | self.assertEqual( |
| 789 | list(self.executor.map(pow, range(40), range(40), chunksize=40)), |
| 790 | ref) |
| 791 | self.assertRaises(ValueError, bad_map) |
| 792 | |
Antoine Pitrou | 1285c9b | 2015-01-17 20:02:14 +0100 | [diff] [blame] | 793 | @classmethod |
| 794 | def _test_traceback(cls): |
| 795 | raise RuntimeError(123) # some comment |
| 796 | |
| 797 | def test_traceback(self): |
| 798 | # We want ensure that the traceback from the child process is |
| 799 | # contained in the traceback raised in the main process. |
| 800 | future = self.executor.submit(self._test_traceback) |
| 801 | with self.assertRaises(Exception) as cm: |
| 802 | future.result() |
| 803 | |
| 804 | exc = cm.exception |
| 805 | self.assertIs(type(exc), RuntimeError) |
| 806 | self.assertEqual(exc.args, (123,)) |
| 807 | cause = exc.__cause__ |
| 808 | self.assertIs(type(cause), futures.process._RemoteTraceback) |
| 809 | self.assertIn('raise RuntimeError(123) # some comment', cause.tb) |
| 810 | |
| 811 | with test.support.captured_stderr() as f1: |
| 812 | try: |
| 813 | raise exc |
| 814 | except RuntimeError: |
| 815 | sys.excepthook(*sys.exc_info()) |
| 816 | self.assertIn('raise RuntimeError(123) # some comment', |
| 817 | f1.getvalue()) |
| 818 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 819 | def test_ressources_gced_in_workers(self): |
| 820 | # Ensure that argument for a job are correctly gc-ed after the job |
| 821 | # is finished |
| 822 | obj = EventfulGCObj(self.ctx) |
| 823 | future = self.executor.submit(id, obj) |
| 824 | future.result() |
| 825 | |
| 826 | self.assertTrue(obj.event.wait(timeout=1)) |
| 827 | |
| 828 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 829 | create_executor_tests(ProcessPoolExecutorTest, |
| 830 | executor_mixins=(ProcessPoolForkMixin, |
| 831 | ProcessPoolForkserverMixin, |
| 832 | ProcessPoolSpawnMixin)) |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 833 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 834 | def hide_process_stderr(): |
| 835 | import io |
| 836 | sys.stderr = io.StringIO() |
| 837 | |
| 838 | |
| 839 | def _crash(delay=None): |
| 840 | """Induces a segfault.""" |
| 841 | if delay: |
| 842 | time.sleep(delay) |
| 843 | import faulthandler |
| 844 | faulthandler.disable() |
| 845 | faulthandler._sigsegv() |
| 846 | |
| 847 | |
| 848 | def _exit(): |
| 849 | """Induces a sys exit with exitcode 1.""" |
| 850 | sys.exit(1) |
| 851 | |
| 852 | |
| 853 | def _raise_error(Err): |
| 854 | """Function that raises an Exception in process.""" |
| 855 | hide_process_stderr() |
| 856 | raise Err() |
| 857 | |
| 858 | |
| 859 | def _return_instance(cls): |
| 860 | """Function that returns a instance of cls.""" |
| 861 | hide_process_stderr() |
| 862 | return cls() |
| 863 | |
| 864 | |
| 865 | class CrashAtPickle(object): |
| 866 | """Bad object that triggers a segfault at pickling time.""" |
| 867 | def __reduce__(self): |
| 868 | _crash() |
| 869 | |
| 870 | |
| 871 | class CrashAtUnpickle(object): |
| 872 | """Bad object that triggers a segfault at unpickling time.""" |
| 873 | def __reduce__(self): |
| 874 | return _crash, () |
| 875 | |
| 876 | |
| 877 | class ExitAtPickle(object): |
| 878 | """Bad object that triggers a process exit at pickling time.""" |
| 879 | def __reduce__(self): |
| 880 | _exit() |
| 881 | |
| 882 | |
| 883 | class ExitAtUnpickle(object): |
| 884 | """Bad object that triggers a process exit at unpickling time.""" |
| 885 | def __reduce__(self): |
| 886 | return _exit, () |
| 887 | |
| 888 | |
| 889 | class ErrorAtPickle(object): |
| 890 | """Bad object that triggers an error at pickling time.""" |
| 891 | def __reduce__(self): |
| 892 | from pickle import PicklingError |
| 893 | raise PicklingError("Error in pickle") |
| 894 | |
| 895 | |
| 896 | class ErrorAtUnpickle(object): |
| 897 | """Bad object that triggers an error at unpickling time.""" |
| 898 | def __reduce__(self): |
| 899 | from pickle import UnpicklingError |
| 900 | return _raise_error, (UnpicklingError, ) |
| 901 | |
| 902 | |
| 903 | class ExecutorDeadlockTest: |
| 904 | TIMEOUT = 15 |
| 905 | |
| 906 | @classmethod |
| 907 | def _sleep_id(cls, x, delay): |
| 908 | time.sleep(delay) |
| 909 | return x |
| 910 | |
| 911 | def _fail_on_deadlock(self, executor): |
| 912 | # If we did not recover before TIMEOUT seconds, consider that the |
| 913 | # executor is in a deadlock state and forcefully clean all its |
| 914 | # composants. |
| 915 | import faulthandler |
| 916 | from tempfile import TemporaryFile |
| 917 | with TemporaryFile(mode="w+") as f: |
| 918 | faulthandler.dump_traceback(file=f) |
| 919 | f.seek(0) |
| 920 | tb = f.read() |
| 921 | for p in executor._processes.values(): |
| 922 | p.terminate() |
| 923 | # This should be safe to call executor.shutdown here as all possible |
| 924 | # deadlocks should have been broken. |
| 925 | executor.shutdown(wait=True) |
| 926 | print(f"\nTraceback:\n {tb}", file=sys.__stderr__) |
| 927 | self.fail(f"Executor deadlock:\n\n{tb}") |
| 928 | |
| 929 | |
| 930 | def test_crash(self): |
| 931 | # extensive testing for deadlock caused by crashes in a pool. |
| 932 | self.executor.shutdown(wait=True) |
| 933 | crash_cases = [ |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 934 | # Check problem occurring while pickling a task in |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 935 | # the task_handler thread |
| 936 | (id, (ErrorAtPickle(),), PicklingError, "error at task pickle"), |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 937 | # Check problem occurring while unpickling a task on workers |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 938 | (id, (ExitAtUnpickle(),), BrokenProcessPool, |
| 939 | "exit at task unpickle"), |
| 940 | (id, (ErrorAtUnpickle(),), BrokenProcessPool, |
| 941 | "error at task unpickle"), |
| 942 | (id, (CrashAtUnpickle(),), BrokenProcessPool, |
| 943 | "crash at task unpickle"), |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 944 | # Check problem occurring during func execution on workers |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 945 | (_crash, (), BrokenProcessPool, |
| 946 | "crash during func execution on worker"), |
| 947 | (_exit, (), SystemExit, |
| 948 | "exit during func execution on worker"), |
| 949 | (_raise_error, (RuntimeError, ), RuntimeError, |
| 950 | "error during func execution on worker"), |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 951 | # Check problem occurring while pickling a task result |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 952 | # on workers |
| 953 | (_return_instance, (CrashAtPickle,), BrokenProcessPool, |
| 954 | "crash during result pickle on worker"), |
| 955 | (_return_instance, (ExitAtPickle,), SystemExit, |
| 956 | "exit during result pickle on worker"), |
| 957 | (_return_instance, (ErrorAtPickle,), PicklingError, |
| 958 | "error during result pickle on worker"), |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 959 | # Check problem occurring while unpickling a task in |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 960 | # the result_handler thread |
| 961 | (_return_instance, (ErrorAtUnpickle,), BrokenProcessPool, |
| 962 | "error during result unpickle in result_handler"), |
| 963 | (_return_instance, (ExitAtUnpickle,), BrokenProcessPool, |
| 964 | "exit during result unpickle in result_handler") |
| 965 | ] |
| 966 | for func, args, error, name in crash_cases: |
| 967 | with self.subTest(name): |
| 968 | # The captured_stderr reduces the noise in the test report |
| 969 | with test.support.captured_stderr(): |
| 970 | executor = self.executor_type( |
| 971 | max_workers=2, mp_context=get_context(self.ctx)) |
| 972 | res = executor.submit(func, *args) |
| 973 | with self.assertRaises(error): |
| 974 | try: |
| 975 | res.result(timeout=self.TIMEOUT) |
| 976 | except futures.TimeoutError: |
| 977 | # If we did not recover before TIMEOUT seconds, |
| 978 | # consider that the executor is in a deadlock state |
| 979 | self._fail_on_deadlock(executor) |
| 980 | executor.shutdown(wait=True) |
| 981 | |
| 982 | def test_shutdown_deadlock(self): |
| 983 | # Test that the pool calling shutdown do not cause deadlock |
| 984 | # if a worker fails after the shutdown call. |
| 985 | self.executor.shutdown(wait=True) |
| 986 | with self.executor_type(max_workers=2, |
| 987 | mp_context=get_context(self.ctx)) as executor: |
| 988 | self.executor = executor # Allow clean up in fail_on_deadlock |
| 989 | f = executor.submit(_crash, delay=.1) |
| 990 | executor.shutdown(wait=True) |
| 991 | with self.assertRaises(BrokenProcessPool): |
| 992 | f.result() |
| 993 | |
| 994 | |
| 995 | create_executor_tests(ExecutorDeadlockTest, |
| 996 | executor_mixins=(ProcessPoolForkMixin, |
| 997 | ProcessPoolForkserverMixin, |
| 998 | ProcessPoolSpawnMixin)) |
| 999 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 1000 | |
Victor Stinner | 489d91c | 2017-08-21 23:24:24 +0200 | [diff] [blame] | 1001 | class FutureTests(BaseTestCase): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1002 | def test_done_callback_with_result(self): |
| 1003 | callback_result = None |
| 1004 | def fn(callback_future): |
| 1005 | nonlocal callback_result |
| 1006 | callback_result = callback_future.result() |
| 1007 | |
| 1008 | f = Future() |
| 1009 | f.add_done_callback(fn) |
| 1010 | f.set_result(5) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1011 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1012 | |
| 1013 | def test_done_callback_with_exception(self): |
| 1014 | callback_exception = None |
| 1015 | def fn(callback_future): |
| 1016 | nonlocal callback_exception |
| 1017 | callback_exception = callback_future.exception() |
| 1018 | |
| 1019 | f = Future() |
| 1020 | f.add_done_callback(fn) |
| 1021 | f.set_exception(Exception('test')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1022 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1023 | |
| 1024 | def test_done_callback_with_cancel(self): |
| 1025 | was_cancelled = None |
| 1026 | def fn(callback_future): |
| 1027 | nonlocal was_cancelled |
| 1028 | was_cancelled = callback_future.cancelled() |
| 1029 | |
| 1030 | f = Future() |
| 1031 | f.add_done_callback(fn) |
| 1032 | self.assertTrue(f.cancel()) |
| 1033 | self.assertTrue(was_cancelled) |
| 1034 | |
| 1035 | def test_done_callback_raises(self): |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 1036 | with test.support.captured_stderr() as stderr: |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 1037 | raising_was_called = False |
| 1038 | fn_was_called = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1039 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 1040 | def raising_fn(callback_future): |
| 1041 | nonlocal raising_was_called |
| 1042 | raising_was_called = True |
| 1043 | raise Exception('doh!') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1044 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 1045 | def fn(callback_future): |
| 1046 | nonlocal fn_was_called |
| 1047 | fn_was_called = True |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1048 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 1049 | f = Future() |
| 1050 | f.add_done_callback(raising_fn) |
| 1051 | f.add_done_callback(fn) |
| 1052 | f.set_result(5) |
| 1053 | self.assertTrue(raising_was_called) |
| 1054 | self.assertTrue(fn_was_called) |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 1055 | self.assertIn('Exception: doh!', stderr.getvalue()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1056 | |
| 1057 | def test_done_callback_already_successful(self): |
| 1058 | callback_result = None |
| 1059 | def fn(callback_future): |
| 1060 | nonlocal callback_result |
| 1061 | callback_result = callback_future.result() |
| 1062 | |
| 1063 | f = Future() |
| 1064 | f.set_result(5) |
| 1065 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1066 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1067 | |
| 1068 | def test_done_callback_already_failed(self): |
| 1069 | callback_exception = None |
| 1070 | def fn(callback_future): |
| 1071 | nonlocal callback_exception |
| 1072 | callback_exception = callback_future.exception() |
| 1073 | |
| 1074 | f = Future() |
| 1075 | f.set_exception(Exception('test')) |
| 1076 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1077 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1078 | |
| 1079 | def test_done_callback_already_cancelled(self): |
| 1080 | was_cancelled = None |
| 1081 | def fn(callback_future): |
| 1082 | nonlocal was_cancelled |
| 1083 | was_cancelled = callback_future.cancelled() |
| 1084 | |
| 1085 | f = Future() |
| 1086 | self.assertTrue(f.cancel()) |
| 1087 | f.add_done_callback(fn) |
| 1088 | self.assertTrue(was_cancelled) |
| 1089 | |
| 1090 | def test_repr(self): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1091 | self.assertRegex(repr(PENDING_FUTURE), |
| 1092 | '<Future at 0x[0-9a-f]+ state=pending>') |
| 1093 | self.assertRegex(repr(RUNNING_FUTURE), |
| 1094 | '<Future at 0x[0-9a-f]+ state=running>') |
| 1095 | self.assertRegex(repr(CANCELLED_FUTURE), |
| 1096 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 1097 | self.assertRegex(repr(CANCELLED_AND_NOTIFIED_FUTURE), |
| 1098 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 1099 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1100 | repr(EXCEPTION_FUTURE), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1101 | '<Future at 0x[0-9a-f]+ state=finished raised OSError>') |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 1102 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1103 | repr(SUCCESSFUL_FUTURE), |
| 1104 | '<Future at 0x[0-9a-f]+ state=finished returned int>') |
| 1105 | |
| 1106 | |
| 1107 | def test_cancel(self): |
| 1108 | f1 = create_future(state=PENDING) |
| 1109 | f2 = create_future(state=RUNNING) |
| 1110 | f3 = create_future(state=CANCELLED) |
| 1111 | f4 = create_future(state=CANCELLED_AND_NOTIFIED) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1112 | f5 = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1113 | f6 = create_future(state=FINISHED, result=5) |
| 1114 | |
| 1115 | self.assertTrue(f1.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1116 | self.assertEqual(f1._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1117 | |
| 1118 | self.assertFalse(f2.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1119 | self.assertEqual(f2._state, RUNNING) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1120 | |
| 1121 | self.assertTrue(f3.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1122 | self.assertEqual(f3._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1123 | |
| 1124 | self.assertTrue(f4.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1125 | self.assertEqual(f4._state, CANCELLED_AND_NOTIFIED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1126 | |
| 1127 | self.assertFalse(f5.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1128 | self.assertEqual(f5._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1129 | |
| 1130 | self.assertFalse(f6.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1131 | self.assertEqual(f6._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1132 | |
| 1133 | def test_cancelled(self): |
| 1134 | self.assertFalse(PENDING_FUTURE.cancelled()) |
| 1135 | self.assertFalse(RUNNING_FUTURE.cancelled()) |
| 1136 | self.assertTrue(CANCELLED_FUTURE.cancelled()) |
| 1137 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.cancelled()) |
| 1138 | self.assertFalse(EXCEPTION_FUTURE.cancelled()) |
| 1139 | self.assertFalse(SUCCESSFUL_FUTURE.cancelled()) |
| 1140 | |
| 1141 | def test_done(self): |
| 1142 | self.assertFalse(PENDING_FUTURE.done()) |
| 1143 | self.assertFalse(RUNNING_FUTURE.done()) |
| 1144 | self.assertTrue(CANCELLED_FUTURE.done()) |
| 1145 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.done()) |
| 1146 | self.assertTrue(EXCEPTION_FUTURE.done()) |
| 1147 | self.assertTrue(SUCCESSFUL_FUTURE.done()) |
| 1148 | |
| 1149 | def test_running(self): |
| 1150 | self.assertFalse(PENDING_FUTURE.running()) |
| 1151 | self.assertTrue(RUNNING_FUTURE.running()) |
| 1152 | self.assertFalse(CANCELLED_FUTURE.running()) |
| 1153 | self.assertFalse(CANCELLED_AND_NOTIFIED_FUTURE.running()) |
| 1154 | self.assertFalse(EXCEPTION_FUTURE.running()) |
| 1155 | self.assertFalse(SUCCESSFUL_FUTURE.running()) |
| 1156 | |
| 1157 | def test_result_with_timeout(self): |
| 1158 | self.assertRaises(futures.TimeoutError, |
| 1159 | PENDING_FUTURE.result, timeout=0) |
| 1160 | self.assertRaises(futures.TimeoutError, |
| 1161 | RUNNING_FUTURE.result, timeout=0) |
| 1162 | self.assertRaises(futures.CancelledError, |
| 1163 | CANCELLED_FUTURE.result, timeout=0) |
| 1164 | self.assertRaises(futures.CancelledError, |
| 1165 | CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1166 | self.assertRaises(OSError, EXCEPTION_FUTURE.result, timeout=0) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1167 | self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42) |
| 1168 | |
| 1169 | def test_result_with_success(self): |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1170 | # TODO(brian@sweetapp.com): This test is timing dependent. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1171 | def notification(): |
| 1172 | # Wait until the main thread is waiting for the result. |
| 1173 | time.sleep(1) |
| 1174 | f1.set_result(42) |
| 1175 | |
| 1176 | f1 = create_future(state=PENDING) |
| 1177 | t = threading.Thread(target=notification) |
| 1178 | t.start() |
| 1179 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1180 | self.assertEqual(f1.result(timeout=5), 42) |
Victor Stinner | 18e95b4 | 2017-09-14 08:43:04 -0700 | [diff] [blame] | 1181 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1182 | |
| 1183 | def test_result_with_cancel(self): |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1184 | # TODO(brian@sweetapp.com): This test is timing dependent. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1185 | def notification(): |
| 1186 | # Wait until the main thread is waiting for the result. |
| 1187 | time.sleep(1) |
| 1188 | f1.cancel() |
| 1189 | |
| 1190 | f1 = create_future(state=PENDING) |
| 1191 | t = threading.Thread(target=notification) |
| 1192 | t.start() |
| 1193 | |
| 1194 | self.assertRaises(futures.CancelledError, f1.result, timeout=5) |
Victor Stinner | 18e95b4 | 2017-09-14 08:43:04 -0700 | [diff] [blame] | 1195 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1196 | |
| 1197 | def test_exception_with_timeout(self): |
| 1198 | self.assertRaises(futures.TimeoutError, |
| 1199 | PENDING_FUTURE.exception, timeout=0) |
| 1200 | self.assertRaises(futures.TimeoutError, |
| 1201 | RUNNING_FUTURE.exception, timeout=0) |
| 1202 | self.assertRaises(futures.CancelledError, |
| 1203 | CANCELLED_FUTURE.exception, timeout=0) |
| 1204 | self.assertRaises(futures.CancelledError, |
| 1205 | CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) |
| 1206 | self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1207 | OSError)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1208 | self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None) |
| 1209 | |
| 1210 | def test_exception_with_success(self): |
| 1211 | def notification(): |
| 1212 | # Wait until the main thread is waiting for the exception. |
| 1213 | time.sleep(1) |
| 1214 | with f1._condition: |
| 1215 | f1._state = FINISHED |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1216 | f1._exception = OSError() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1217 | f1._condition.notify_all() |
| 1218 | |
| 1219 | f1 = create_future(state=PENDING) |
| 1220 | t = threading.Thread(target=notification) |
| 1221 | t.start() |
| 1222 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1223 | self.assertTrue(isinstance(f1.exception(timeout=5), OSError)) |
Victor Stinner | 18e95b4 | 2017-09-14 08:43:04 -0700 | [diff] [blame] | 1224 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1225 | |
jhaydaman | 0a28c0d | 2018-05-30 02:15:06 -0500 | [diff] [blame] | 1226 | def test_multiple_set_result(self): |
| 1227 | f = create_future(state=PENDING) |
| 1228 | f.set_result(1) |
| 1229 | |
| 1230 | with self.assertRaisesRegex( |
| 1231 | futures.InvalidStateError, |
| 1232 | 'FINISHED: <Future at 0x[0-9a-f]+ ' |
| 1233 | 'state=finished returned int>' |
| 1234 | ): |
| 1235 | f.set_result(2) |
| 1236 | |
| 1237 | self.assertTrue(f.done()) |
| 1238 | self.assertEqual(f.result(), 1) |
| 1239 | |
| 1240 | def test_multiple_set_exception(self): |
| 1241 | f = create_future(state=PENDING) |
| 1242 | e = ValueError() |
| 1243 | f.set_exception(e) |
| 1244 | |
| 1245 | with self.assertRaisesRegex( |
| 1246 | futures.InvalidStateError, |
| 1247 | 'FINISHED: <Future at 0x[0-9a-f]+ ' |
| 1248 | 'state=finished raised ValueError>' |
| 1249 | ): |
| 1250 | f.set_exception(Exception()) |
| 1251 | |
| 1252 | self.assertEqual(f.exception(), e) |
| 1253 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 1254 | |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 1255 | @test.support.reap_threads |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1256 | def test_main(): |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 1257 | try: |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 1258 | test.support.run_unittest(__name__) |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 1259 | finally: |
| 1260 | test.support.reap_children() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1261 | |
| 1262 | if __name__ == "__main__": |
| 1263 | test_main() |