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) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 37 | EXCEPTION_FUTURE = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 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 | |
Ross Lagerwall | 66e2fb6 | 2012-01-08 08:29:40 +0200 | [diff] [blame] | 112 | def test_hang_issue12364(self): |
| 113 | fs = [self.executor.submit(time.sleep, 0.1) for _ in range(50)] |
| 114 | self.executor.shutdown() |
| 115 | for f in fs: |
| 116 | f.result() |
| 117 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 118 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 119 | class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 120 | def _prime_executor(self): |
| 121 | pass |
| 122 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 123 | def test_threads_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 124 | self.executor.submit(mul, 21, 2) |
| 125 | self.executor.submit(mul, 6, 7) |
| 126 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 127 | self.assertEqual(len(self.executor._threads), 3) |
| 128 | self.executor.shutdown() |
| 129 | for t in self.executor._threads: |
| 130 | t.join() |
| 131 | |
| 132 | def test_context_manager_shutdown(self): |
| 133 | with futures.ThreadPoolExecutor(max_workers=5) as e: |
| 134 | executor = e |
| 135 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 136 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 137 | |
| 138 | for t in executor._threads: |
| 139 | t.join() |
| 140 | |
| 141 | def test_del_shutdown(self): |
| 142 | executor = futures.ThreadPoolExecutor(max_workers=5) |
| 143 | executor.map(abs, range(-5, 5)) |
| 144 | threads = executor._threads |
| 145 | del executor |
| 146 | |
| 147 | for t in threads: |
| 148 | t.join() |
| 149 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 150 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 151 | class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 152 | def _prime_executor(self): |
| 153 | pass |
| 154 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 155 | def test_processes_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 156 | self.executor.submit(mul, 21, 2) |
| 157 | self.executor.submit(mul, 6, 7) |
| 158 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 159 | self.assertEqual(len(self.executor._processes), 5) |
| 160 | processes = self.executor._processes |
| 161 | self.executor.shutdown() |
| 162 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 163 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 164 | p.join() |
| 165 | |
| 166 | def test_context_manager_shutdown(self): |
| 167 | with futures.ProcessPoolExecutor(max_workers=5) as e: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 168 | processes = e._processes |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 169 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 170 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 171 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 172 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 173 | p.join() |
| 174 | |
| 175 | def test_del_shutdown(self): |
| 176 | executor = futures.ProcessPoolExecutor(max_workers=5) |
| 177 | list(executor.map(abs, range(-5, 5))) |
| 178 | queue_management_thread = executor._queue_management_thread |
| 179 | processes = executor._processes |
| 180 | del executor |
| 181 | |
| 182 | queue_management_thread.join() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 183 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 184 | p.join() |
| 185 | |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 186 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 187 | class WaitTests(unittest.TestCase): |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 188 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 189 | def test_first_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 190 | future1 = self.executor.submit(mul, 21, 2) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 191 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 192 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 193 | done, not_done = futures.wait( |
| 194 | [CANCELLED_FUTURE, future1, future2], |
| 195 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 196 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 197 | self.assertEqual(set([future1]), done) |
| 198 | self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 199 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 200 | def test_first_completed_some_already_completed(self): |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 201 | future1 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 202 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 203 | finished, pending = futures.wait( |
| 204 | [CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], |
| 205 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 206 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 207 | self.assertEqual( |
| 208 | set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), |
| 209 | finished) |
| 210 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 211 | |
| 212 | def test_first_exception(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 213 | future1 = self.executor.submit(mul, 2, 21) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 214 | future2 = self.executor.submit(sleep_and_raise, 1.5) |
| 215 | future3 = self.executor.submit(time.sleep, 3) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 216 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 217 | finished, pending = futures.wait( |
| 218 | [future1, future2, future3], |
| 219 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 220 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 221 | self.assertEqual(set([future1, future2]), finished) |
| 222 | self.assertEqual(set([future3]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 223 | |
| 224 | def test_first_exception_some_already_complete(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 225 | future1 = self.executor.submit(divmod, 21, 0) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 226 | future2 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 227 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 228 | finished, pending = futures.wait( |
| 229 | [SUCCESSFUL_FUTURE, |
| 230 | CANCELLED_FUTURE, |
| 231 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 232 | future1, future2], |
| 233 | return_when=futures.FIRST_EXCEPTION) |
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 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 236 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 237 | future1]), finished) |
| 238 | self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 239 | |
| 240 | def test_first_exception_one_already_failed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 241 | future1 = self.executor.submit(time.sleep, 2) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 242 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 243 | finished, pending = futures.wait( |
| 244 | [EXCEPTION_FUTURE, future1], |
| 245 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 246 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 247 | self.assertEqual(set([EXCEPTION_FUTURE]), finished) |
| 248 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 249 | |
| 250 | def test_all_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 251 | future1 = self.executor.submit(divmod, 2, 0) |
| 252 | future2 = self.executor.submit(mul, 2, 21) |
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 | finished, pending = futures.wait( |
| 255 | [SUCCESSFUL_FUTURE, |
| 256 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 257 | EXCEPTION_FUTURE, |
| 258 | future1, |
| 259 | future2], |
| 260 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 261 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 262 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 263 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 264 | EXCEPTION_FUTURE, |
| 265 | future1, |
| 266 | future2]), finished) |
| 267 | self.assertEqual(set(), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 268 | |
| 269 | def test_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 270 | future1 = self.executor.submit(mul, 6, 7) |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 271 | future2 = self.executor.submit(time.sleep, 6) |
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 | finished, pending = futures.wait( |
| 274 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 275 | EXCEPTION_FUTURE, |
| 276 | SUCCESSFUL_FUTURE, |
| 277 | future1, future2], |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 278 | timeout=5, |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 279 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 280 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 281 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 282 | EXCEPTION_FUTURE, |
| 283 | SUCCESSFUL_FUTURE, |
| 284 | future1]), finished) |
| 285 | self.assertEqual(set([future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 286 | |
| 287 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 288 | class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests): |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 289 | |
| 290 | def test_pending_calls_race(self): |
| 291 | # Issue #14406: multi-threaded race condition when waiting on all |
| 292 | # futures. |
| 293 | event = threading.Event() |
| 294 | def future_func(): |
| 295 | event.wait() |
| 296 | oldswitchinterval = sys.getswitchinterval() |
| 297 | sys.setswitchinterval(1e-6) |
| 298 | try: |
| 299 | fs = {self.executor.submit(future_func) for i in range(100)} |
| 300 | event.set() |
| 301 | futures.wait(fs, return_when=futures.ALL_COMPLETED) |
| 302 | finally: |
| 303 | sys.setswitchinterval(oldswitchinterval) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 304 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 305 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 306 | class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests): |
| 307 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 308 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 309 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 310 | class AsCompletedTests(unittest.TestCase): |
| 311 | # TODO(brian@sweetapp.com): Should have a test with a non-zero timeout. |
| 312 | def test_no_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 313 | future1 = self.executor.submit(mul, 2, 21) |
| 314 | future2 = self.executor.submit(mul, 7, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 315 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 316 | completed = set(futures.as_completed( |
| 317 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 318 | EXCEPTION_FUTURE, |
| 319 | SUCCESSFUL_FUTURE, |
| 320 | future1, future2])) |
| 321 | self.assertEqual(set( |
| 322 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 323 | EXCEPTION_FUTURE, |
| 324 | SUCCESSFUL_FUTURE, |
| 325 | future1, future2]), |
| 326 | completed) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 327 | |
| 328 | def test_zero_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 329 | future1 = self.executor.submit(time.sleep, 2) |
| 330 | completed_futures = set() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 331 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 332 | for future in futures.as_completed( |
| 333 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 334 | EXCEPTION_FUTURE, |
| 335 | SUCCESSFUL_FUTURE, |
| 336 | future1], |
| 337 | timeout=0): |
| 338 | completed_futures.add(future) |
| 339 | except futures.TimeoutError: |
| 340 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 341 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 342 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 343 | EXCEPTION_FUTURE, |
| 344 | SUCCESSFUL_FUTURE]), |
| 345 | completed_futures) |
| 346 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 347 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 348 | class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests): |
| 349 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 350 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 351 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 352 | class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests): |
| 353 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 354 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 355 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 356 | class ExecutorTest(unittest.TestCase): |
| 357 | # Executor.shutdown() and context manager usage is tested by |
| 358 | # ExecutorShutdownTest. |
| 359 | def test_submit(self): |
| 360 | future = self.executor.submit(pow, 2, 8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 361 | self.assertEqual(256, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 362 | |
| 363 | def test_submit_keyword(self): |
| 364 | future = self.executor.submit(mul, 2, y=8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 365 | self.assertEqual(16, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 366 | |
| 367 | def test_map(self): |
| 368 | self.assertEqual( |
| 369 | list(self.executor.map(pow, range(10), range(10))), |
| 370 | list(map(pow, range(10), range(10)))) |
| 371 | |
| 372 | def test_map_exception(self): |
| 373 | i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) |
| 374 | self.assertEqual(i.__next__(), (0, 1)) |
| 375 | self.assertEqual(i.__next__(), (0, 1)) |
| 376 | self.assertRaises(ZeroDivisionError, i.__next__) |
| 377 | |
| 378 | def test_map_timeout(self): |
| 379 | results = [] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 380 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 381 | for i in self.executor.map(time.sleep, |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 382 | [0, 0, 6], |
| 383 | timeout=5): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 384 | results.append(i) |
| 385 | except futures.TimeoutError: |
| 386 | pass |
| 387 | else: |
| 388 | self.fail('expected TimeoutError') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 389 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 390 | self.assertEqual([None, None], results) |
| 391 | |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 392 | def test_shutdown_race_issue12456(self): |
| 393 | # Issue #12456: race condition at shutdown where trying to post a |
| 394 | # sentinel in the call queue blocks (the queue is full while processes |
| 395 | # have exited). |
| 396 | self.executor.map(str, [2] * (self.worker_count + 1)) |
| 397 | self.executor.shutdown() |
| 398 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 399 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 400 | class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest): |
Brian Quinlan | f007876 | 2011-04-08 08:19:33 +1000 | [diff] [blame] | 401 | def test_map_submits_without_iteration(self): |
| 402 | """Tests verifying issue 11777.""" |
| 403 | finished = [] |
| 404 | def record_finished(n): |
| 405 | finished.append(n) |
| 406 | |
| 407 | self.executor.map(record_finished, range(10)) |
| 408 | self.executor.shutdown(wait=True) |
| 409 | self.assertCountEqual(finished, range(10)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 410 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 411 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 412 | class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest): |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 413 | def test_killed_child(self): |
| 414 | # When a child process is abruptly terminated, the whole pool gets |
| 415 | # "broken". |
| 416 | futures = [self.executor.submit(time.sleep, 3)] |
| 417 | # Get one of the processes, and terminate (kill) it |
| 418 | p = next(iter(self.executor._processes.values())) |
| 419 | p.terminate() |
| 420 | for fut in futures: |
| 421 | self.assertRaises(BrokenProcessPool, fut.result) |
| 422 | # Submitting other jobs fails as well. |
| 423 | self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 424 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 425 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 426 | class FutureTests(unittest.TestCase): |
| 427 | def test_done_callback_with_result(self): |
| 428 | callback_result = None |
| 429 | def fn(callback_future): |
| 430 | nonlocal callback_result |
| 431 | callback_result = callback_future.result() |
| 432 | |
| 433 | f = Future() |
| 434 | f.add_done_callback(fn) |
| 435 | f.set_result(5) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 436 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 437 | |
| 438 | def test_done_callback_with_exception(self): |
| 439 | callback_exception = None |
| 440 | def fn(callback_future): |
| 441 | nonlocal callback_exception |
| 442 | callback_exception = callback_future.exception() |
| 443 | |
| 444 | f = Future() |
| 445 | f.add_done_callback(fn) |
| 446 | f.set_exception(Exception('test')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 447 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 448 | |
| 449 | def test_done_callback_with_cancel(self): |
| 450 | was_cancelled = None |
| 451 | def fn(callback_future): |
| 452 | nonlocal was_cancelled |
| 453 | was_cancelled = callback_future.cancelled() |
| 454 | |
| 455 | f = Future() |
| 456 | f.add_done_callback(fn) |
| 457 | self.assertTrue(f.cancel()) |
| 458 | self.assertTrue(was_cancelled) |
| 459 | |
| 460 | def test_done_callback_raises(self): |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 461 | with test.support.captured_stderr() as stderr: |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 462 | raising_was_called = False |
| 463 | fn_was_called = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 464 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 465 | def raising_fn(callback_future): |
| 466 | nonlocal raising_was_called |
| 467 | raising_was_called = True |
| 468 | raise Exception('doh!') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 469 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 470 | def fn(callback_future): |
| 471 | nonlocal fn_was_called |
| 472 | fn_was_called = True |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 473 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 474 | f = Future() |
| 475 | f.add_done_callback(raising_fn) |
| 476 | f.add_done_callback(fn) |
| 477 | f.set_result(5) |
| 478 | self.assertTrue(raising_was_called) |
| 479 | self.assertTrue(fn_was_called) |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 480 | self.assertIn('Exception: doh!', stderr.getvalue()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 481 | |
| 482 | def test_done_callback_already_successful(self): |
| 483 | callback_result = None |
| 484 | def fn(callback_future): |
| 485 | nonlocal callback_result |
| 486 | callback_result = callback_future.result() |
| 487 | |
| 488 | f = Future() |
| 489 | f.set_result(5) |
| 490 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 491 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 492 | |
| 493 | def test_done_callback_already_failed(self): |
| 494 | callback_exception = None |
| 495 | def fn(callback_future): |
| 496 | nonlocal callback_exception |
| 497 | callback_exception = callback_future.exception() |
| 498 | |
| 499 | f = Future() |
| 500 | f.set_exception(Exception('test')) |
| 501 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 502 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 503 | |
| 504 | def test_done_callback_already_cancelled(self): |
| 505 | was_cancelled = None |
| 506 | def fn(callback_future): |
| 507 | nonlocal was_cancelled |
| 508 | was_cancelled = callback_future.cancelled() |
| 509 | |
| 510 | f = Future() |
| 511 | self.assertTrue(f.cancel()) |
| 512 | f.add_done_callback(fn) |
| 513 | self.assertTrue(was_cancelled) |
| 514 | |
| 515 | def test_repr(self): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 516 | self.assertRegex(repr(PENDING_FUTURE), |
| 517 | '<Future at 0x[0-9a-f]+ state=pending>') |
| 518 | self.assertRegex(repr(RUNNING_FUTURE), |
| 519 | '<Future at 0x[0-9a-f]+ state=running>') |
| 520 | self.assertRegex(repr(CANCELLED_FUTURE), |
| 521 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 522 | self.assertRegex(repr(CANCELLED_AND_NOTIFIED_FUTURE), |
| 523 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 524 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 525 | repr(EXCEPTION_FUTURE), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 526 | '<Future at 0x[0-9a-f]+ state=finished raised OSError>') |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 527 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 528 | repr(SUCCESSFUL_FUTURE), |
| 529 | '<Future at 0x[0-9a-f]+ state=finished returned int>') |
| 530 | |
| 531 | |
| 532 | def test_cancel(self): |
| 533 | f1 = create_future(state=PENDING) |
| 534 | f2 = create_future(state=RUNNING) |
| 535 | f3 = create_future(state=CANCELLED) |
| 536 | f4 = create_future(state=CANCELLED_AND_NOTIFIED) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 537 | f5 = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 538 | f6 = create_future(state=FINISHED, result=5) |
| 539 | |
| 540 | self.assertTrue(f1.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 541 | self.assertEqual(f1._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 542 | |
| 543 | self.assertFalse(f2.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 544 | self.assertEqual(f2._state, RUNNING) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 545 | |
| 546 | self.assertTrue(f3.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 547 | self.assertEqual(f3._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 548 | |
| 549 | self.assertTrue(f4.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 550 | self.assertEqual(f4._state, CANCELLED_AND_NOTIFIED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 551 | |
| 552 | self.assertFalse(f5.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 553 | self.assertEqual(f5._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 554 | |
| 555 | self.assertFalse(f6.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 556 | self.assertEqual(f6._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 557 | |
| 558 | def test_cancelled(self): |
| 559 | self.assertFalse(PENDING_FUTURE.cancelled()) |
| 560 | self.assertFalse(RUNNING_FUTURE.cancelled()) |
| 561 | self.assertTrue(CANCELLED_FUTURE.cancelled()) |
| 562 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.cancelled()) |
| 563 | self.assertFalse(EXCEPTION_FUTURE.cancelled()) |
| 564 | self.assertFalse(SUCCESSFUL_FUTURE.cancelled()) |
| 565 | |
| 566 | def test_done(self): |
| 567 | self.assertFalse(PENDING_FUTURE.done()) |
| 568 | self.assertFalse(RUNNING_FUTURE.done()) |
| 569 | self.assertTrue(CANCELLED_FUTURE.done()) |
| 570 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.done()) |
| 571 | self.assertTrue(EXCEPTION_FUTURE.done()) |
| 572 | self.assertTrue(SUCCESSFUL_FUTURE.done()) |
| 573 | |
| 574 | def test_running(self): |
| 575 | self.assertFalse(PENDING_FUTURE.running()) |
| 576 | self.assertTrue(RUNNING_FUTURE.running()) |
| 577 | self.assertFalse(CANCELLED_FUTURE.running()) |
| 578 | self.assertFalse(CANCELLED_AND_NOTIFIED_FUTURE.running()) |
| 579 | self.assertFalse(EXCEPTION_FUTURE.running()) |
| 580 | self.assertFalse(SUCCESSFUL_FUTURE.running()) |
| 581 | |
| 582 | def test_result_with_timeout(self): |
| 583 | self.assertRaises(futures.TimeoutError, |
| 584 | PENDING_FUTURE.result, timeout=0) |
| 585 | self.assertRaises(futures.TimeoutError, |
| 586 | RUNNING_FUTURE.result, timeout=0) |
| 587 | self.assertRaises(futures.CancelledError, |
| 588 | CANCELLED_FUTURE.result, timeout=0) |
| 589 | self.assertRaises(futures.CancelledError, |
| 590 | CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 591 | self.assertRaises(OSError, EXCEPTION_FUTURE.result, timeout=0) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 592 | self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42) |
| 593 | |
| 594 | def test_result_with_success(self): |
| 595 | # TODO(brian@sweetapp.com): This test is timing dependant. |
| 596 | def notification(): |
| 597 | # Wait until the main thread is waiting for the result. |
| 598 | time.sleep(1) |
| 599 | f1.set_result(42) |
| 600 | |
| 601 | f1 = create_future(state=PENDING) |
| 602 | t = threading.Thread(target=notification) |
| 603 | t.start() |
| 604 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 605 | self.assertEqual(f1.result(timeout=5), 42) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 606 | |
| 607 | def test_result_with_cancel(self): |
| 608 | # TODO(brian@sweetapp.com): This test is timing dependant. |
| 609 | def notification(): |
| 610 | # Wait until the main thread is waiting for the result. |
| 611 | time.sleep(1) |
| 612 | f1.cancel() |
| 613 | |
| 614 | f1 = create_future(state=PENDING) |
| 615 | t = threading.Thread(target=notification) |
| 616 | t.start() |
| 617 | |
| 618 | self.assertRaises(futures.CancelledError, f1.result, timeout=5) |
| 619 | |
| 620 | def test_exception_with_timeout(self): |
| 621 | self.assertRaises(futures.TimeoutError, |
| 622 | PENDING_FUTURE.exception, timeout=0) |
| 623 | self.assertRaises(futures.TimeoutError, |
| 624 | RUNNING_FUTURE.exception, timeout=0) |
| 625 | self.assertRaises(futures.CancelledError, |
| 626 | CANCELLED_FUTURE.exception, timeout=0) |
| 627 | self.assertRaises(futures.CancelledError, |
| 628 | CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) |
| 629 | self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 630 | OSError)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 631 | self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None) |
| 632 | |
| 633 | def test_exception_with_success(self): |
| 634 | def notification(): |
| 635 | # Wait until the main thread is waiting for the exception. |
| 636 | time.sleep(1) |
| 637 | with f1._condition: |
| 638 | f1._state = FINISHED |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 639 | f1._exception = OSError() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 640 | f1._condition.notify_all() |
| 641 | |
| 642 | f1 = create_future(state=PENDING) |
| 643 | t = threading.Thread(target=notification) |
| 644 | t.start() |
| 645 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 646 | self.assertTrue(isinstance(f1.exception(timeout=5), OSError)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 648 | @test.support.reap_threads |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 649 | def test_main(): |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 650 | try: |
| 651 | test.support.run_unittest(ProcessPoolExecutorTest, |
| 652 | ThreadPoolExecutorTest, |
| 653 | ProcessPoolWaitTests, |
| 654 | ThreadPoolWaitTests, |
| 655 | ProcessPoolAsCompletedTests, |
| 656 | ThreadPoolAsCompletedTests, |
| 657 | FutureTests, |
| 658 | ProcessPoolShutdownTest, |
Antoine Pitrou | d06a065 | 2011-07-16 01:13:34 +0200 | [diff] [blame] | 659 | ThreadPoolShutdownTest, |
| 660 | ) |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 661 | finally: |
| 662 | test.support.reap_children() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 663 | |
| 664 | if __name__ == "__main__": |
| 665 | test_main() |