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') |
| 7 | # import threading after _multiprocessing to raise a more revelant error |
| 8 | # message: "No module named _multiprocessing". _multiprocessing is not compiled |
| 9 | # without thread support. |
| 10 | test.support.import_module('threading') |
| 11 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 12 | from test.script_helper import assert_python_ok |
| 13 | |
| 14 | import sys |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 15 | import threading |
| 16 | import time |
| 17 | import unittest |
| 18 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 19 | from concurrent import futures |
| 20 | from concurrent.futures._base import ( |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 21 | PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future) |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 22 | from concurrent.futures.process import BrokenProcessPool |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 23 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 24 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 25 | def create_future(state=PENDING, exception=None, result=None): |
| 26 | f = Future() |
| 27 | f._state = state |
| 28 | f._exception = exception |
| 29 | f._result = result |
| 30 | return f |
| 31 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 32 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 33 | PENDING_FUTURE = create_future(state=PENDING) |
| 34 | RUNNING_FUTURE = create_future(state=RUNNING) |
| 35 | CANCELLED_FUTURE = create_future(state=CANCELLED) |
| 36 | CANCELLED_AND_NOTIFIED_FUTURE = create_future(state=CANCELLED_AND_NOTIFIED) |
| 37 | EXCEPTION_FUTURE = create_future(state=FINISHED, exception=IOError()) |
| 38 | SUCCESSFUL_FUTURE = create_future(state=FINISHED, result=42) |
| 39 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 40 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 41 | def mul(x, y): |
| 42 | return x * y |
| 43 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 44 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 45 | def sleep_and_raise(t): |
| 46 | time.sleep(t) |
| 47 | raise Exception('this is an exception') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 48 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 49 | def sleep_and_print(t, msg): |
| 50 | time.sleep(t) |
| 51 | print(msg) |
| 52 | sys.stdout.flush() |
| 53 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 54 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 55 | class ExecutorMixin: |
| 56 | worker_count = 5 |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 57 | |
| 58 | def setUp(self): |
| 59 | self.t1 = time.time() |
| 60 | try: |
| 61 | self.executor = self.executor_type(max_workers=self.worker_count) |
| 62 | except NotImplementedError as e: |
| 63 | self.skipTest(str(e)) |
| 64 | self._prime_executor() |
| 65 | |
| 66 | def tearDown(self): |
| 67 | self.executor.shutdown(wait=True) |
| 68 | dt = time.time() - self.t1 |
| 69 | if test.support.verbose: |
| 70 | print("%.2fs" % dt, end=' ') |
| 71 | self.assertLess(dt, 60, "synchronization issue: test lasted too long") |
| 72 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 73 | def _prime_executor(self): |
| 74 | # Make sure that the executor is ready to do work before running the |
| 75 | # tests. This should reduce the probability of timeouts in the tests. |
| 76 | futures = [self.executor.submit(time.sleep, 0.1) |
| 77 | for _ in range(self.worker_count)] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 78 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 79 | for f in futures: |
| 80 | f.result() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 81 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 82 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 83 | class ThreadPoolMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 84 | executor_type = futures.ThreadPoolExecutor |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 85 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 86 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 87 | class ProcessPoolMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 88 | executor_type = futures.ProcessPoolExecutor |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 89 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 90 | |
| 91 | class ExecutorShutdownTest(unittest.TestCase): |
| 92 | def test_run_after_shutdown(self): |
| 93 | self.executor.shutdown() |
| 94 | self.assertRaises(RuntimeError, |
| 95 | self.executor.submit, |
| 96 | pow, 2, 5) |
| 97 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 98 | def test_interpreter_shutdown(self): |
| 99 | # Test the atexit hook for shutdown of worker threads and processes |
| 100 | rc, out, err = assert_python_ok('-c', """if 1: |
| 101 | from concurrent.futures import {executor_type} |
| 102 | from time import sleep |
| 103 | from test.test_concurrent_futures import sleep_and_print |
| 104 | t = {executor_type}(5) |
| 105 | t.submit(sleep_and_print, 1.0, "apple") |
| 106 | """.format(executor_type=self.executor_type.__name__)) |
| 107 | # Errors in atexit hooks don't change the process exit code, check |
| 108 | # stderr manually. |
| 109 | self.assertFalse(err) |
| 110 | self.assertEqual(out.strip(), b"apple") |
| 111 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 112 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 113 | class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 114 | def _prime_executor(self): |
| 115 | pass |
| 116 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 117 | def test_threads_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 118 | self.executor.submit(mul, 21, 2) |
| 119 | self.executor.submit(mul, 6, 7) |
| 120 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 121 | self.assertEqual(len(self.executor._threads), 3) |
| 122 | self.executor.shutdown() |
| 123 | for t in self.executor._threads: |
| 124 | t.join() |
| 125 | |
| 126 | def test_context_manager_shutdown(self): |
| 127 | with futures.ThreadPoolExecutor(max_workers=5) as e: |
| 128 | executor = e |
| 129 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 130 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 131 | |
| 132 | for t in executor._threads: |
| 133 | t.join() |
| 134 | |
| 135 | def test_del_shutdown(self): |
| 136 | executor = futures.ThreadPoolExecutor(max_workers=5) |
| 137 | executor.map(abs, range(-5, 5)) |
| 138 | threads = executor._threads |
| 139 | del executor |
| 140 | |
| 141 | for t in threads: |
| 142 | t.join() |
| 143 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 144 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 145 | class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 146 | def _prime_executor(self): |
| 147 | pass |
| 148 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 149 | def test_processes_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 150 | self.executor.submit(mul, 21, 2) |
| 151 | self.executor.submit(mul, 6, 7) |
| 152 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 153 | self.assertEqual(len(self.executor._processes), 5) |
| 154 | processes = self.executor._processes |
| 155 | self.executor.shutdown() |
| 156 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 157 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 158 | p.join() |
| 159 | |
| 160 | def test_context_manager_shutdown(self): |
| 161 | with futures.ProcessPoolExecutor(max_workers=5) as e: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 162 | processes = e._processes |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 163 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 164 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 165 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 166 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 167 | p.join() |
| 168 | |
| 169 | def test_del_shutdown(self): |
| 170 | executor = futures.ProcessPoolExecutor(max_workers=5) |
| 171 | list(executor.map(abs, range(-5, 5))) |
| 172 | queue_management_thread = executor._queue_management_thread |
| 173 | processes = executor._processes |
| 174 | del executor |
| 175 | |
| 176 | queue_management_thread.join() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 177 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 178 | p.join() |
| 179 | |
| 180 | class WaitTests(unittest.TestCase): |
| 181 | def test_first_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 182 | future1 = self.executor.submit(mul, 21, 2) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 183 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 184 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 185 | done, not_done = futures.wait( |
| 186 | [CANCELLED_FUTURE, future1, future2], |
| 187 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 188 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 189 | self.assertEqual(set([future1]), done) |
| 190 | self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 191 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 192 | def test_first_completed_some_already_completed(self): |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 193 | future1 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 194 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 195 | finished, pending = futures.wait( |
| 196 | [CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], |
| 197 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 198 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 199 | self.assertEqual( |
| 200 | set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), |
| 201 | finished) |
| 202 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 203 | |
| 204 | def test_first_exception(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 205 | future1 = self.executor.submit(mul, 2, 21) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 206 | future2 = self.executor.submit(sleep_and_raise, 1.5) |
| 207 | future3 = self.executor.submit(time.sleep, 3) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 208 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 209 | finished, pending = futures.wait( |
| 210 | [future1, future2, future3], |
| 211 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 212 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 213 | self.assertEqual(set([future1, future2]), finished) |
| 214 | self.assertEqual(set([future3]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 215 | |
| 216 | def test_first_exception_some_already_complete(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 217 | future1 = self.executor.submit(divmod, 21, 0) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 218 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 219 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 220 | finished, pending = futures.wait( |
| 221 | [SUCCESSFUL_FUTURE, |
| 222 | CANCELLED_FUTURE, |
| 223 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 224 | future1, future2], |
| 225 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 226 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 227 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 228 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 229 | future1]), finished) |
| 230 | self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 231 | |
| 232 | def test_first_exception_one_already_failed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 233 | future1 = self.executor.submit(time.sleep, 2) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 234 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 235 | finished, pending = futures.wait( |
| 236 | [EXCEPTION_FUTURE, future1], |
| 237 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 238 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 239 | self.assertEqual(set([EXCEPTION_FUTURE]), finished) |
| 240 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 241 | |
| 242 | def test_all_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 243 | future1 = self.executor.submit(divmod, 2, 0) |
| 244 | future2 = self.executor.submit(mul, 2, 21) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 245 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 246 | finished, pending = futures.wait( |
| 247 | [SUCCESSFUL_FUTURE, |
| 248 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 249 | EXCEPTION_FUTURE, |
| 250 | future1, |
| 251 | future2], |
| 252 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 253 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 254 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 255 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 256 | EXCEPTION_FUTURE, |
| 257 | future1, |
| 258 | future2]), finished) |
| 259 | self.assertEqual(set(), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 260 | |
| 261 | def test_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 262 | future1 = self.executor.submit(mul, 6, 7) |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 263 | future2 = self.executor.submit(time.sleep, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 264 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 265 | finished, pending = futures.wait( |
| 266 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 267 | EXCEPTION_FUTURE, |
| 268 | SUCCESSFUL_FUTURE, |
| 269 | future1, future2], |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 270 | timeout=5, |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 271 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 272 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 273 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 274 | EXCEPTION_FUTURE, |
| 275 | SUCCESSFUL_FUTURE, |
| 276 | future1]), finished) |
| 277 | self.assertEqual(set([future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 278 | |
| 279 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 280 | class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests): |
| 281 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 282 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 283 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 284 | class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests): |
| 285 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 286 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 287 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 288 | class AsCompletedTests(unittest.TestCase): |
| 289 | # TODO(brian@sweetapp.com): Should have a test with a non-zero timeout. |
| 290 | def test_no_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 291 | future1 = self.executor.submit(mul, 2, 21) |
| 292 | future2 = self.executor.submit(mul, 7, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 293 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 294 | completed = set(futures.as_completed( |
| 295 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 296 | EXCEPTION_FUTURE, |
| 297 | SUCCESSFUL_FUTURE, |
| 298 | future1, future2])) |
| 299 | self.assertEqual(set( |
| 300 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 301 | EXCEPTION_FUTURE, |
| 302 | SUCCESSFUL_FUTURE, |
| 303 | future1, future2]), |
| 304 | completed) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 305 | |
| 306 | def test_zero_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 307 | future1 = self.executor.submit(time.sleep, 2) |
| 308 | completed_futures = set() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 309 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 310 | for future in futures.as_completed( |
| 311 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 312 | EXCEPTION_FUTURE, |
| 313 | SUCCESSFUL_FUTURE, |
| 314 | future1], |
| 315 | timeout=0): |
| 316 | completed_futures.add(future) |
| 317 | except futures.TimeoutError: |
| 318 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 319 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 320 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 321 | EXCEPTION_FUTURE, |
| 322 | SUCCESSFUL_FUTURE]), |
| 323 | completed_futures) |
| 324 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 326 | class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests): |
| 327 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 328 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 329 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 330 | class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests): |
| 331 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 332 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 333 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 334 | class ExecutorTest(unittest.TestCase): |
| 335 | # Executor.shutdown() and context manager usage is tested by |
| 336 | # ExecutorShutdownTest. |
| 337 | def test_submit(self): |
| 338 | future = self.executor.submit(pow, 2, 8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 339 | self.assertEqual(256, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 340 | |
| 341 | def test_submit_keyword(self): |
| 342 | future = self.executor.submit(mul, 2, y=8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 343 | self.assertEqual(16, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 344 | |
| 345 | def test_map(self): |
| 346 | self.assertEqual( |
| 347 | list(self.executor.map(pow, range(10), range(10))), |
| 348 | list(map(pow, range(10), range(10)))) |
| 349 | |
| 350 | def test_map_exception(self): |
| 351 | i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) |
| 352 | self.assertEqual(i.__next__(), (0, 1)) |
| 353 | self.assertEqual(i.__next__(), (0, 1)) |
| 354 | self.assertRaises(ZeroDivisionError, i.__next__) |
| 355 | |
| 356 | def test_map_timeout(self): |
| 357 | results = [] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 358 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 359 | for i in self.executor.map(time.sleep, |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 360 | [0, 0, 6], |
| 361 | timeout=5): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 362 | results.append(i) |
| 363 | except futures.TimeoutError: |
| 364 | pass |
| 365 | else: |
| 366 | self.fail('expected TimeoutError') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 367 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 368 | self.assertEqual([None, None], results) |
| 369 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 370 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 371 | class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest): |
Brian Quinlan | f007876 | 2011-04-08 08:19:33 +1000 | [diff] [blame] | 372 | def test_map_submits_without_iteration(self): |
| 373 | """Tests verifying issue 11777.""" |
| 374 | finished = [] |
| 375 | def record_finished(n): |
| 376 | finished.append(n) |
| 377 | |
| 378 | self.executor.map(record_finished, range(10)) |
| 379 | self.executor.shutdown(wait=True) |
| 380 | self.assertCountEqual(finished, range(10)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 381 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 382 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 383 | class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest): |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 384 | def test_killed_child(self): |
| 385 | # When a child process is abruptly terminated, the whole pool gets |
| 386 | # "broken". |
| 387 | futures = [self.executor.submit(time.sleep, 3)] |
| 388 | # Get one of the processes, and terminate (kill) it |
| 389 | p = next(iter(self.executor._processes.values())) |
| 390 | p.terminate() |
| 391 | for fut in futures: |
| 392 | self.assertRaises(BrokenProcessPool, fut.result) |
| 393 | # Submitting other jobs fails as well. |
| 394 | self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 395 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 396 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 397 | class FutureTests(unittest.TestCase): |
| 398 | def test_done_callback_with_result(self): |
| 399 | callback_result = None |
| 400 | def fn(callback_future): |
| 401 | nonlocal callback_result |
| 402 | callback_result = callback_future.result() |
| 403 | |
| 404 | f = Future() |
| 405 | f.add_done_callback(fn) |
| 406 | f.set_result(5) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 407 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 408 | |
| 409 | def test_done_callback_with_exception(self): |
| 410 | callback_exception = None |
| 411 | def fn(callback_future): |
| 412 | nonlocal callback_exception |
| 413 | callback_exception = callback_future.exception() |
| 414 | |
| 415 | f = Future() |
| 416 | f.add_done_callback(fn) |
| 417 | f.set_exception(Exception('test')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 418 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 419 | |
| 420 | def test_done_callback_with_cancel(self): |
| 421 | was_cancelled = None |
| 422 | def fn(callback_future): |
| 423 | nonlocal was_cancelled |
| 424 | was_cancelled = callback_future.cancelled() |
| 425 | |
| 426 | f = Future() |
| 427 | f.add_done_callback(fn) |
| 428 | self.assertTrue(f.cancel()) |
| 429 | self.assertTrue(was_cancelled) |
| 430 | |
| 431 | def test_done_callback_raises(self): |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 432 | with test.support.captured_stderr() as stderr: |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 433 | raising_was_called = False |
| 434 | fn_was_called = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 435 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 436 | def raising_fn(callback_future): |
| 437 | nonlocal raising_was_called |
| 438 | raising_was_called = True |
| 439 | raise Exception('doh!') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 440 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 441 | def fn(callback_future): |
| 442 | nonlocal fn_was_called |
| 443 | fn_was_called = True |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 444 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 445 | f = Future() |
| 446 | f.add_done_callback(raising_fn) |
| 447 | f.add_done_callback(fn) |
| 448 | f.set_result(5) |
| 449 | self.assertTrue(raising_was_called) |
| 450 | self.assertTrue(fn_was_called) |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 451 | self.assertIn('Exception: doh!', stderr.getvalue()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 452 | |
| 453 | def test_done_callback_already_successful(self): |
| 454 | callback_result = None |
| 455 | def fn(callback_future): |
| 456 | nonlocal callback_result |
| 457 | callback_result = callback_future.result() |
| 458 | |
| 459 | f = Future() |
| 460 | f.set_result(5) |
| 461 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 462 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 463 | |
| 464 | def test_done_callback_already_failed(self): |
| 465 | callback_exception = None |
| 466 | def fn(callback_future): |
| 467 | nonlocal callback_exception |
| 468 | callback_exception = callback_future.exception() |
| 469 | |
| 470 | f = Future() |
| 471 | f.set_exception(Exception('test')) |
| 472 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 473 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 474 | |
| 475 | def test_done_callback_already_cancelled(self): |
| 476 | was_cancelled = None |
| 477 | def fn(callback_future): |
| 478 | nonlocal was_cancelled |
| 479 | was_cancelled = callback_future.cancelled() |
| 480 | |
| 481 | f = Future() |
| 482 | self.assertTrue(f.cancel()) |
| 483 | f.add_done_callback(fn) |
| 484 | self.assertTrue(was_cancelled) |
| 485 | |
| 486 | def test_repr(self): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 487 | self.assertRegex(repr(PENDING_FUTURE), |
| 488 | '<Future at 0x[0-9a-f]+ state=pending>') |
| 489 | self.assertRegex(repr(RUNNING_FUTURE), |
| 490 | '<Future at 0x[0-9a-f]+ state=running>') |
| 491 | self.assertRegex(repr(CANCELLED_FUTURE), |
| 492 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 493 | self.assertRegex(repr(CANCELLED_AND_NOTIFIED_FUTURE), |
| 494 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 495 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 496 | repr(EXCEPTION_FUTURE), |
| 497 | '<Future at 0x[0-9a-f]+ state=finished raised IOError>') |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 498 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 499 | repr(SUCCESSFUL_FUTURE), |
| 500 | '<Future at 0x[0-9a-f]+ state=finished returned int>') |
| 501 | |
| 502 | |
| 503 | def test_cancel(self): |
| 504 | f1 = create_future(state=PENDING) |
| 505 | f2 = create_future(state=RUNNING) |
| 506 | f3 = create_future(state=CANCELLED) |
| 507 | f4 = create_future(state=CANCELLED_AND_NOTIFIED) |
| 508 | f5 = create_future(state=FINISHED, exception=IOError()) |
| 509 | f6 = create_future(state=FINISHED, result=5) |
| 510 | |
| 511 | self.assertTrue(f1.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 512 | self.assertEqual(f1._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 513 | |
| 514 | self.assertFalse(f2.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 515 | self.assertEqual(f2._state, RUNNING) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 516 | |
| 517 | self.assertTrue(f3.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 518 | self.assertEqual(f3._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 519 | |
| 520 | self.assertTrue(f4.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 521 | self.assertEqual(f4._state, CANCELLED_AND_NOTIFIED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 522 | |
| 523 | self.assertFalse(f5.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 524 | self.assertEqual(f5._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 525 | |
| 526 | self.assertFalse(f6.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 527 | self.assertEqual(f6._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 528 | |
| 529 | def test_cancelled(self): |
| 530 | self.assertFalse(PENDING_FUTURE.cancelled()) |
| 531 | self.assertFalse(RUNNING_FUTURE.cancelled()) |
| 532 | self.assertTrue(CANCELLED_FUTURE.cancelled()) |
| 533 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.cancelled()) |
| 534 | self.assertFalse(EXCEPTION_FUTURE.cancelled()) |
| 535 | self.assertFalse(SUCCESSFUL_FUTURE.cancelled()) |
| 536 | |
| 537 | def test_done(self): |
| 538 | self.assertFalse(PENDING_FUTURE.done()) |
| 539 | self.assertFalse(RUNNING_FUTURE.done()) |
| 540 | self.assertTrue(CANCELLED_FUTURE.done()) |
| 541 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.done()) |
| 542 | self.assertTrue(EXCEPTION_FUTURE.done()) |
| 543 | self.assertTrue(SUCCESSFUL_FUTURE.done()) |
| 544 | |
| 545 | def test_running(self): |
| 546 | self.assertFalse(PENDING_FUTURE.running()) |
| 547 | self.assertTrue(RUNNING_FUTURE.running()) |
| 548 | self.assertFalse(CANCELLED_FUTURE.running()) |
| 549 | self.assertFalse(CANCELLED_AND_NOTIFIED_FUTURE.running()) |
| 550 | self.assertFalse(EXCEPTION_FUTURE.running()) |
| 551 | self.assertFalse(SUCCESSFUL_FUTURE.running()) |
| 552 | |
| 553 | def test_result_with_timeout(self): |
| 554 | self.assertRaises(futures.TimeoutError, |
| 555 | PENDING_FUTURE.result, timeout=0) |
| 556 | self.assertRaises(futures.TimeoutError, |
| 557 | RUNNING_FUTURE.result, timeout=0) |
| 558 | self.assertRaises(futures.CancelledError, |
| 559 | CANCELLED_FUTURE.result, timeout=0) |
| 560 | self.assertRaises(futures.CancelledError, |
| 561 | CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) |
| 562 | self.assertRaises(IOError, EXCEPTION_FUTURE.result, timeout=0) |
| 563 | self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42) |
| 564 | |
| 565 | def test_result_with_success(self): |
| 566 | # TODO(brian@sweetapp.com): This test is timing dependant. |
| 567 | def notification(): |
| 568 | # Wait until the main thread is waiting for the result. |
| 569 | time.sleep(1) |
| 570 | f1.set_result(42) |
| 571 | |
| 572 | f1 = create_future(state=PENDING) |
| 573 | t = threading.Thread(target=notification) |
| 574 | t.start() |
| 575 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 576 | self.assertEqual(f1.result(timeout=5), 42) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 577 | |
| 578 | def test_result_with_cancel(self): |
| 579 | # TODO(brian@sweetapp.com): This test is timing dependant. |
| 580 | def notification(): |
| 581 | # Wait until the main thread is waiting for the result. |
| 582 | time.sleep(1) |
| 583 | f1.cancel() |
| 584 | |
| 585 | f1 = create_future(state=PENDING) |
| 586 | t = threading.Thread(target=notification) |
| 587 | t.start() |
| 588 | |
| 589 | self.assertRaises(futures.CancelledError, f1.result, timeout=5) |
| 590 | |
| 591 | def test_exception_with_timeout(self): |
| 592 | self.assertRaises(futures.TimeoutError, |
| 593 | PENDING_FUTURE.exception, timeout=0) |
| 594 | self.assertRaises(futures.TimeoutError, |
| 595 | RUNNING_FUTURE.exception, timeout=0) |
| 596 | self.assertRaises(futures.CancelledError, |
| 597 | CANCELLED_FUTURE.exception, timeout=0) |
| 598 | self.assertRaises(futures.CancelledError, |
| 599 | CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) |
| 600 | self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), |
| 601 | IOError)) |
| 602 | self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None) |
| 603 | |
| 604 | def test_exception_with_success(self): |
| 605 | def notification(): |
| 606 | # Wait until the main thread is waiting for the exception. |
| 607 | time.sleep(1) |
| 608 | with f1._condition: |
| 609 | f1._state = FINISHED |
| 610 | f1._exception = IOError() |
| 611 | f1._condition.notify_all() |
| 612 | |
| 613 | f1 = create_future(state=PENDING) |
| 614 | t = threading.Thread(target=notification) |
| 615 | t.start() |
| 616 | |
| 617 | self.assertTrue(isinstance(f1.exception(timeout=5), IOError)) |
| 618 | |
| 619 | def test_main(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 620 | test.support.run_unittest(ProcessPoolExecutorTest, |
| 621 | ThreadPoolExecutorTest, |
| 622 | ProcessPoolWaitTests, |
| 623 | ThreadPoolWaitTests, |
| 624 | ProcessPoolAsCompletedTests, |
| 625 | ThreadPoolAsCompletedTests, |
| 626 | FutureTests, |
| 627 | ProcessPoolShutdownTest, |
| 628 | ThreadPoolShutdownTest) |
| 629 | |
| 630 | if __name__ == "__main__": |
| 631 | test_main() |