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 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 12 | from test.support.script_helper import assert_python_ok |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 13 | |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 14 | import os |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 15 | import sys |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 16 | import threading |
| 17 | import time |
| 18 | import unittest |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 19 | import weakref |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 20 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 21 | from concurrent import futures |
| 22 | from concurrent.futures._base import ( |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 23 | PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future) |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 24 | from concurrent.futures.process import BrokenProcessPool |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 25 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 26 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 27 | def create_future(state=PENDING, exception=None, result=None): |
| 28 | f = Future() |
| 29 | f._state = state |
| 30 | f._exception = exception |
| 31 | f._result = result |
| 32 | return f |
| 33 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 34 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 35 | PENDING_FUTURE = create_future(state=PENDING) |
| 36 | RUNNING_FUTURE = create_future(state=RUNNING) |
| 37 | CANCELLED_FUTURE = create_future(state=CANCELLED) |
| 38 | CANCELLED_AND_NOTIFIED_FUTURE = create_future(state=CANCELLED_AND_NOTIFIED) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 39 | EXCEPTION_FUTURE = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 40 | SUCCESSFUL_FUTURE = create_future(state=FINISHED, result=42) |
| 41 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 42 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 43 | def mul(x, y): |
| 44 | return x * y |
| 45 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 46 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 47 | def sleep_and_raise(t): |
| 48 | time.sleep(t) |
| 49 | raise Exception('this is an exception') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 50 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 51 | def sleep_and_print(t, msg): |
| 52 | time.sleep(t) |
| 53 | print(msg) |
| 54 | sys.stdout.flush() |
| 55 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 56 | |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 57 | class MyObject(object): |
| 58 | def my_method(self): |
| 59 | pass |
| 60 | |
| 61 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 62 | class ExecutorMixin: |
| 63 | worker_count = 5 |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 64 | |
| 65 | def setUp(self): |
| 66 | self.t1 = time.time() |
| 67 | try: |
| 68 | self.executor = self.executor_type(max_workers=self.worker_count) |
| 69 | except NotImplementedError as e: |
| 70 | self.skipTest(str(e)) |
| 71 | self._prime_executor() |
| 72 | |
| 73 | def tearDown(self): |
| 74 | self.executor.shutdown(wait=True) |
| 75 | dt = time.time() - self.t1 |
| 76 | if test.support.verbose: |
| 77 | print("%.2fs" % dt, end=' ') |
| 78 | self.assertLess(dt, 60, "synchronization issue: test lasted too long") |
| 79 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 80 | def _prime_executor(self): |
| 81 | # Make sure that the executor is ready to do work before running the |
| 82 | # tests. This should reduce the probability of timeouts in the tests. |
| 83 | futures = [self.executor.submit(time.sleep, 0.1) |
| 84 | for _ in range(self.worker_count)] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 85 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 86 | for f in futures: |
| 87 | f.result() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 88 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 89 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 90 | class ThreadPoolMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 91 | executor_type = futures.ThreadPoolExecutor |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 92 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 93 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 94 | class ProcessPoolMixin(ExecutorMixin): |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 95 | executor_type = futures.ProcessPoolExecutor |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 96 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 98 | class ExecutorShutdownTest: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 99 | def test_run_after_shutdown(self): |
| 100 | self.executor.shutdown() |
| 101 | self.assertRaises(RuntimeError, |
| 102 | self.executor.submit, |
| 103 | pow, 2, 5) |
| 104 | |
Antoine Pitrou | aebac0b | 2011-03-24 15:47:39 +0100 | [diff] [blame] | 105 | def test_interpreter_shutdown(self): |
| 106 | # Test the atexit hook for shutdown of worker threads and processes |
| 107 | rc, out, err = assert_python_ok('-c', """if 1: |
| 108 | from concurrent.futures import {executor_type} |
| 109 | from time import sleep |
| 110 | from test.test_concurrent_futures import sleep_and_print |
| 111 | t = {executor_type}(5) |
| 112 | t.submit(sleep_and_print, 1.0, "apple") |
| 113 | """.format(executor_type=self.executor_type.__name__)) |
| 114 | # Errors in atexit hooks don't change the process exit code, check |
| 115 | # stderr manually. |
| 116 | self.assertFalse(err) |
| 117 | self.assertEqual(out.strip(), b"apple") |
| 118 | |
Ross Lagerwall | 66e2fb6 | 2012-01-08 08:29:40 +0200 | [diff] [blame] | 119 | def test_hang_issue12364(self): |
| 120 | fs = [self.executor.submit(time.sleep, 0.1) for _ in range(50)] |
| 121 | self.executor.shutdown() |
| 122 | for f in fs: |
| 123 | f.result() |
| 124 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 126 | class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, unittest.TestCase): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 127 | def _prime_executor(self): |
| 128 | pass |
| 129 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 130 | def test_threads_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 131 | self.executor.submit(mul, 21, 2) |
| 132 | self.executor.submit(mul, 6, 7) |
| 133 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 134 | self.assertEqual(len(self.executor._threads), 3) |
| 135 | self.executor.shutdown() |
| 136 | for t in self.executor._threads: |
| 137 | t.join() |
| 138 | |
| 139 | def test_context_manager_shutdown(self): |
| 140 | with futures.ThreadPoolExecutor(max_workers=5) as e: |
| 141 | executor = e |
| 142 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 143 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 144 | |
| 145 | for t in executor._threads: |
| 146 | t.join() |
| 147 | |
| 148 | def test_del_shutdown(self): |
| 149 | executor = futures.ThreadPoolExecutor(max_workers=5) |
| 150 | executor.map(abs, range(-5, 5)) |
| 151 | threads = executor._threads |
| 152 | del executor |
| 153 | |
| 154 | for t in threads: |
| 155 | t.join() |
| 156 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 158 | class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest, unittest.TestCase): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 159 | def _prime_executor(self): |
| 160 | pass |
| 161 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 162 | def test_processes_terminate(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 163 | self.executor.submit(mul, 21, 2) |
| 164 | self.executor.submit(mul, 6, 7) |
| 165 | self.executor.submit(mul, 3, 14) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 166 | self.assertEqual(len(self.executor._processes), 5) |
| 167 | processes = self.executor._processes |
| 168 | self.executor.shutdown() |
| 169 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 170 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 171 | p.join() |
| 172 | |
| 173 | def test_context_manager_shutdown(self): |
| 174 | with futures.ProcessPoolExecutor(max_workers=5) as e: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 175 | processes = e._processes |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 176 | self.assertEqual(list(e.map(abs, range(-5, 5))), |
| 177 | [5, 4, 3, 2, 1, 0, 1, 2, 3, 4]) |
| 178 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 179 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 180 | p.join() |
| 181 | |
| 182 | def test_del_shutdown(self): |
| 183 | executor = futures.ProcessPoolExecutor(max_workers=5) |
| 184 | list(executor.map(abs, range(-5, 5))) |
| 185 | queue_management_thread = executor._queue_management_thread |
| 186 | processes = executor._processes |
| 187 | del executor |
| 188 | |
| 189 | queue_management_thread.join() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 190 | for p in processes.values(): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 191 | p.join() |
| 192 | |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 193 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 194 | class WaitTests: |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 195 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 196 | def test_first_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 197 | future1 = self.executor.submit(mul, 21, 2) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 198 | future2 = self.executor.submit(time.sleep, 1.5) |
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 | done, not_done = futures.wait( |
| 201 | [CANCELLED_FUTURE, future1, future2], |
| 202 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 203 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 204 | self.assertEqual(set([future1]), done) |
| 205 | self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) |
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 | def test_first_completed_some_already_completed(self): |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 208 | future1 = self.executor.submit(time.sleep, 1.5) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 209 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 210 | finished, pending = futures.wait( |
| 211 | [CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], |
| 212 | return_when=futures.FIRST_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 213 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 214 | self.assertEqual( |
| 215 | set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), |
| 216 | finished) |
| 217 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 218 | |
| 219 | def test_first_exception(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 220 | future1 = self.executor.submit(mul, 2, 21) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 221 | future2 = self.executor.submit(sleep_and_raise, 1.5) |
| 222 | future3 = self.executor.submit(time.sleep, 3) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 223 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 224 | finished, pending = futures.wait( |
| 225 | [future1, future2, future3], |
| 226 | return_when=futures.FIRST_EXCEPTION) |
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 | self.assertEqual(set([future1, future2]), finished) |
| 229 | self.assertEqual(set([future3]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 230 | |
| 231 | def test_first_exception_some_already_complete(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 232 | future1 = self.executor.submit(divmod, 21, 0) |
Antoine Pitrou | 8e5e942 | 2011-03-22 18:30:30 +0100 | [diff] [blame] | 233 | future2 = self.executor.submit(time.sleep, 1.5) |
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 | [SUCCESSFUL_FUTURE, |
| 237 | CANCELLED_FUTURE, |
| 238 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 239 | future1, future2], |
| 240 | return_when=futures.FIRST_EXCEPTION) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 241 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 242 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 243 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 244 | future1]), finished) |
| 245 | self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 246 | |
| 247 | def test_first_exception_one_already_failed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 248 | future1 = self.executor.submit(time.sleep, 2) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 249 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 250 | finished, pending = futures.wait( |
| 251 | [EXCEPTION_FUTURE, future1], |
| 252 | return_when=futures.FIRST_EXCEPTION) |
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([EXCEPTION_FUTURE]), finished) |
| 255 | self.assertEqual(set([future1]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 256 | |
| 257 | def test_all_completed(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 258 | future1 = self.executor.submit(divmod, 2, 0) |
| 259 | future2 = self.executor.submit(mul, 2, 21) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 260 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 261 | finished, pending = futures.wait( |
| 262 | [SUCCESSFUL_FUTURE, |
| 263 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 264 | EXCEPTION_FUTURE, |
| 265 | future1, |
| 266 | future2], |
| 267 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 268 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 269 | self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 270 | CANCELLED_AND_NOTIFIED_FUTURE, |
| 271 | EXCEPTION_FUTURE, |
| 272 | future1, |
| 273 | future2]), finished) |
| 274 | self.assertEqual(set(), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 275 | |
| 276 | def test_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 277 | future1 = self.executor.submit(mul, 6, 7) |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 278 | future2 = self.executor.submit(time.sleep, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 279 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 280 | finished, pending = futures.wait( |
| 281 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 282 | EXCEPTION_FUTURE, |
| 283 | SUCCESSFUL_FUTURE, |
| 284 | future1, future2], |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 285 | timeout=5, |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 286 | return_when=futures.ALL_COMPLETED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 287 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 288 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 289 | EXCEPTION_FUTURE, |
| 290 | SUCCESSFUL_FUTURE, |
| 291 | future1]), finished) |
| 292 | self.assertEqual(set([future2]), pending) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 293 | |
| 294 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 295 | class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, unittest.TestCase): |
Antoine Pitrou | f70401e | 2012-03-31 20:23:30 +0200 | [diff] [blame] | 296 | |
| 297 | def test_pending_calls_race(self): |
| 298 | # Issue #14406: multi-threaded race condition when waiting on all |
| 299 | # futures. |
| 300 | event = threading.Event() |
| 301 | def future_func(): |
| 302 | event.wait() |
| 303 | oldswitchinterval = sys.getswitchinterval() |
| 304 | sys.setswitchinterval(1e-6) |
| 305 | try: |
| 306 | fs = {self.executor.submit(future_func) for i in range(100)} |
| 307 | event.set() |
| 308 | futures.wait(fs, return_when=futures.ALL_COMPLETED) |
| 309 | finally: |
| 310 | sys.setswitchinterval(oldswitchinterval) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 311 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 313 | class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests, unittest.TestCase): |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 314 | pass |
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 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 317 | class AsCompletedTests: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 318 | # TODO(brian@sweetapp.com): Should have a test with a non-zero timeout. |
| 319 | def test_no_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 320 | future1 = self.executor.submit(mul, 2, 21) |
| 321 | future2 = self.executor.submit(mul, 7, 6) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 322 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 323 | completed = set(futures.as_completed( |
| 324 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 325 | EXCEPTION_FUTURE, |
| 326 | SUCCESSFUL_FUTURE, |
| 327 | future1, future2])) |
| 328 | self.assertEqual(set( |
| 329 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 330 | EXCEPTION_FUTURE, |
| 331 | SUCCESSFUL_FUTURE, |
| 332 | future1, future2]), |
| 333 | completed) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 334 | |
| 335 | def test_zero_timeout(self): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 336 | future1 = self.executor.submit(time.sleep, 2) |
| 337 | completed_futures = set() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 338 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 339 | for future in futures.as_completed( |
| 340 | [CANCELLED_AND_NOTIFIED_FUTURE, |
| 341 | EXCEPTION_FUTURE, |
| 342 | SUCCESSFUL_FUTURE, |
| 343 | future1], |
| 344 | timeout=0): |
| 345 | completed_futures.add(future) |
| 346 | except futures.TimeoutError: |
| 347 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 348 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 349 | self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 350 | EXCEPTION_FUTURE, |
| 351 | SUCCESSFUL_FUTURE]), |
| 352 | completed_futures) |
| 353 | |
Guido van Rossum | e6994ff | 2014-01-26 09:57:51 -0800 | [diff] [blame] | 354 | def test_duplicate_futures(self): |
| 355 | # Issue 20367. Duplicate futures should not raise exceptions or give |
| 356 | # duplicate responses. |
| 357 | future1 = self.executor.submit(time.sleep, 2) |
| 358 | completed = [f for f in futures.as_completed([future1,future1])] |
| 359 | self.assertEqual(len(completed), 1) |
| 360 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 362 | class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests, unittest.TestCase): |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 363 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 364 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 366 | class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests, unittest.TestCase): |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 367 | pass |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 368 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 370 | class ExecutorTest: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 371 | # Executor.shutdown() and context manager usage is tested by |
| 372 | # ExecutorShutdownTest. |
| 373 | def test_submit(self): |
| 374 | future = self.executor.submit(pow, 2, 8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 375 | self.assertEqual(256, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 376 | |
| 377 | def test_submit_keyword(self): |
| 378 | future = self.executor.submit(mul, 2, y=8) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 379 | self.assertEqual(16, future.result()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 380 | |
| 381 | def test_map(self): |
| 382 | self.assertEqual( |
| 383 | list(self.executor.map(pow, range(10), range(10))), |
| 384 | list(map(pow, range(10), range(10)))) |
| 385 | |
| 386 | def test_map_exception(self): |
| 387 | i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) |
| 388 | self.assertEqual(i.__next__(), (0, 1)) |
| 389 | self.assertEqual(i.__next__(), (0, 1)) |
| 390 | self.assertRaises(ZeroDivisionError, i.__next__) |
| 391 | |
| 392 | def test_map_timeout(self): |
| 393 | results = [] |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 394 | try: |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 395 | for i in self.executor.map(time.sleep, |
Brian Quinlan | 1ae2998 | 2011-05-30 21:52:24 +1000 | [diff] [blame] | 396 | [0, 0, 6], |
| 397 | timeout=5): |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 398 | results.append(i) |
| 399 | except futures.TimeoutError: |
| 400 | pass |
| 401 | else: |
| 402 | self.fail('expected TimeoutError') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 403 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 404 | self.assertEqual([None, None], results) |
| 405 | |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 406 | def test_shutdown_race_issue12456(self): |
| 407 | # Issue #12456: race condition at shutdown where trying to post a |
| 408 | # sentinel in the call queue blocks (the queue is full while processes |
| 409 | # have exited). |
| 410 | self.executor.map(str, [2] * (self.worker_count + 1)) |
| 411 | self.executor.shutdown() |
| 412 | |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 413 | @test.support.cpython_only |
| 414 | def test_no_stale_references(self): |
| 415 | # Issue #16284: check that the executors don't unnecessarily hang onto |
| 416 | # references. |
| 417 | my_object = MyObject() |
| 418 | my_object_collected = threading.Event() |
| 419 | my_object_callback = weakref.ref( |
| 420 | my_object, lambda obj: my_object_collected.set()) |
| 421 | # Deliberately discarding the future. |
| 422 | self.executor.submit(my_object.my_method) |
| 423 | del my_object |
| 424 | |
| 425 | collected = my_object_collected.wait(timeout=5.0) |
| 426 | self.assertTrue(collected, |
| 427 | "Stale reference not collected within timeout.") |
| 428 | |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 429 | def test_max_workers_negative(self): |
| 430 | for number in (0, -1): |
R David Murray | 475a476 | 2014-06-11 16:25:05 -0400 | [diff] [blame] | 431 | with self.assertRaisesRegex(ValueError, |
| 432 | "max_workers must be greater " |
| 433 | "than 0"): |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 434 | self.executor_type(max_workers=number) |
| 435 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 437 | class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase): |
Brian Quinlan | f007876 | 2011-04-08 08:19:33 +1000 | [diff] [blame] | 438 | def test_map_submits_without_iteration(self): |
| 439 | """Tests verifying issue 11777.""" |
| 440 | finished = [] |
| 441 | def record_finished(n): |
| 442 | finished.append(n) |
| 443 | |
| 444 | self.executor.map(record_finished, range(10)) |
| 445 | self.executor.shutdown(wait=True) |
| 446 | self.assertCountEqual(finished, range(10)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 448 | def test_default_workers(self): |
| 449 | executor = self.executor_type() |
| 450 | self.assertEqual(executor._max_workers, |
| 451 | (os.cpu_count() or 1) * 5) |
| 452 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 454 | class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, unittest.TestCase): |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 455 | def test_killed_child(self): |
| 456 | # When a child process is abruptly terminated, the whole pool gets |
| 457 | # "broken". |
| 458 | futures = [self.executor.submit(time.sleep, 3)] |
| 459 | # Get one of the processes, and terminate (kill) it |
| 460 | p = next(iter(self.executor._processes.values())) |
| 461 | p.terminate() |
| 462 | for fut in futures: |
| 463 | self.assertRaises(BrokenProcessPool, fut.result) |
| 464 | # Submitting other jobs fails as well. |
| 465 | self.assertRaises(BrokenProcessPool, self.executor.submit, pow, 2, 8) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 466 | |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 467 | def test_map_chunksize(self): |
| 468 | def bad_map(): |
| 469 | list(self.executor.map(pow, range(40), range(40), chunksize=-1)) |
| 470 | |
| 471 | ref = list(map(pow, range(40), range(40))) |
| 472 | self.assertEqual( |
| 473 | list(self.executor.map(pow, range(40), range(40), chunksize=6)), |
| 474 | ref) |
| 475 | self.assertEqual( |
| 476 | list(self.executor.map(pow, range(40), range(40), chunksize=50)), |
| 477 | ref) |
| 478 | self.assertEqual( |
| 479 | list(self.executor.map(pow, range(40), range(40), chunksize=40)), |
| 480 | ref) |
| 481 | self.assertRaises(ValueError, bad_map) |
| 482 | |
Antoine Pitrou | 1285c9b | 2015-01-17 20:02:14 +0100 | [diff] [blame] | 483 | @classmethod |
| 484 | def _test_traceback(cls): |
| 485 | raise RuntimeError(123) # some comment |
| 486 | |
| 487 | def test_traceback(self): |
| 488 | # We want ensure that the traceback from the child process is |
| 489 | # contained in the traceback raised in the main process. |
| 490 | future = self.executor.submit(self._test_traceback) |
| 491 | with self.assertRaises(Exception) as cm: |
| 492 | future.result() |
| 493 | |
| 494 | exc = cm.exception |
| 495 | self.assertIs(type(exc), RuntimeError) |
| 496 | self.assertEqual(exc.args, (123,)) |
| 497 | cause = exc.__cause__ |
| 498 | self.assertIs(type(cause), futures.process._RemoteTraceback) |
| 499 | self.assertIn('raise RuntimeError(123) # some comment', cause.tb) |
| 500 | |
| 501 | with test.support.captured_stderr() as f1: |
| 502 | try: |
| 503 | raise exc |
| 504 | except RuntimeError: |
| 505 | sys.excepthook(*sys.exc_info()) |
| 506 | self.assertIn('raise RuntimeError(123) # some comment', |
| 507 | f1.getvalue()) |
| 508 | |
Brian Quinlan | 1d1df82 | 2011-01-03 02:56:39 +0000 | [diff] [blame] | 509 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 510 | class FutureTests(unittest.TestCase): |
| 511 | def test_done_callback_with_result(self): |
| 512 | callback_result = None |
| 513 | def fn(callback_future): |
| 514 | nonlocal callback_result |
| 515 | callback_result = callback_future.result() |
| 516 | |
| 517 | f = Future() |
| 518 | f.add_done_callback(fn) |
| 519 | f.set_result(5) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 520 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 521 | |
| 522 | def test_done_callback_with_exception(self): |
| 523 | callback_exception = None |
| 524 | def fn(callback_future): |
| 525 | nonlocal callback_exception |
| 526 | callback_exception = callback_future.exception() |
| 527 | |
| 528 | f = Future() |
| 529 | f.add_done_callback(fn) |
| 530 | f.set_exception(Exception('test')) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 531 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 532 | |
| 533 | def test_done_callback_with_cancel(self): |
| 534 | was_cancelled = None |
| 535 | def fn(callback_future): |
| 536 | nonlocal was_cancelled |
| 537 | was_cancelled = callback_future.cancelled() |
| 538 | |
| 539 | f = Future() |
| 540 | f.add_done_callback(fn) |
| 541 | self.assertTrue(f.cancel()) |
| 542 | self.assertTrue(was_cancelled) |
| 543 | |
| 544 | def test_done_callback_raises(self): |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 545 | with test.support.captured_stderr() as stderr: |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 546 | raising_was_called = False |
| 547 | fn_was_called = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 548 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 549 | def raising_fn(callback_future): |
| 550 | nonlocal raising_was_called |
| 551 | raising_was_called = True |
| 552 | raise Exception('doh!') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 553 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 554 | def fn(callback_future): |
| 555 | nonlocal fn_was_called |
| 556 | fn_was_called = True |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 557 | |
Brian Quinlan | 1e2ae4f | 2010-10-06 13:05:45 +0000 | [diff] [blame] | 558 | f = Future() |
| 559 | f.add_done_callback(raising_fn) |
| 560 | f.add_done_callback(fn) |
| 561 | f.set_result(5) |
| 562 | self.assertTrue(raising_was_called) |
| 563 | self.assertTrue(fn_was_called) |
Brian Quinlan | 251cc84 | 2010-12-28 21:14:34 +0000 | [diff] [blame] | 564 | self.assertIn('Exception: doh!', stderr.getvalue()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 565 | |
| 566 | def test_done_callback_already_successful(self): |
| 567 | callback_result = None |
| 568 | def fn(callback_future): |
| 569 | nonlocal callback_result |
| 570 | callback_result = callback_future.result() |
| 571 | |
| 572 | f = Future() |
| 573 | f.set_result(5) |
| 574 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 575 | self.assertEqual(5, callback_result) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 576 | |
| 577 | def test_done_callback_already_failed(self): |
| 578 | callback_exception = None |
| 579 | def fn(callback_future): |
| 580 | nonlocal callback_exception |
| 581 | callback_exception = callback_future.exception() |
| 582 | |
| 583 | f = Future() |
| 584 | f.set_exception(Exception('test')) |
| 585 | f.add_done_callback(fn) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 586 | self.assertEqual(('test',), callback_exception.args) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 587 | |
| 588 | def test_done_callback_already_cancelled(self): |
| 589 | was_cancelled = None |
| 590 | def fn(callback_future): |
| 591 | nonlocal was_cancelled |
| 592 | was_cancelled = callback_future.cancelled() |
| 593 | |
| 594 | f = Future() |
| 595 | self.assertTrue(f.cancel()) |
| 596 | f.add_done_callback(fn) |
| 597 | self.assertTrue(was_cancelled) |
| 598 | |
| 599 | def test_repr(self): |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 600 | self.assertRegex(repr(PENDING_FUTURE), |
| 601 | '<Future at 0x[0-9a-f]+ state=pending>') |
| 602 | self.assertRegex(repr(RUNNING_FUTURE), |
| 603 | '<Future at 0x[0-9a-f]+ state=running>') |
| 604 | self.assertRegex(repr(CANCELLED_FUTURE), |
| 605 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 606 | self.assertRegex(repr(CANCELLED_AND_NOTIFIED_FUTURE), |
| 607 | '<Future at 0x[0-9a-f]+ state=cancelled>') |
| 608 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 609 | repr(EXCEPTION_FUTURE), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 610 | '<Future at 0x[0-9a-f]+ state=finished raised OSError>') |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 611 | self.assertRegex( |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 612 | repr(SUCCESSFUL_FUTURE), |
| 613 | '<Future at 0x[0-9a-f]+ state=finished returned int>') |
| 614 | |
| 615 | |
| 616 | def test_cancel(self): |
| 617 | f1 = create_future(state=PENDING) |
| 618 | f2 = create_future(state=RUNNING) |
| 619 | f3 = create_future(state=CANCELLED) |
| 620 | f4 = create_future(state=CANCELLED_AND_NOTIFIED) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 621 | f5 = create_future(state=FINISHED, exception=OSError()) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 622 | f6 = create_future(state=FINISHED, result=5) |
| 623 | |
| 624 | self.assertTrue(f1.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 625 | self.assertEqual(f1._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 626 | |
| 627 | self.assertFalse(f2.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 628 | self.assertEqual(f2._state, RUNNING) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 629 | |
| 630 | self.assertTrue(f3.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 631 | self.assertEqual(f3._state, CANCELLED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 632 | |
| 633 | self.assertTrue(f4.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 634 | self.assertEqual(f4._state, CANCELLED_AND_NOTIFIED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 635 | |
| 636 | self.assertFalse(f5.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 637 | self.assertEqual(f5._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 638 | |
| 639 | self.assertFalse(f6.cancel()) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 640 | self.assertEqual(f6._state, FINISHED) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 641 | |
| 642 | def test_cancelled(self): |
| 643 | self.assertFalse(PENDING_FUTURE.cancelled()) |
| 644 | self.assertFalse(RUNNING_FUTURE.cancelled()) |
| 645 | self.assertTrue(CANCELLED_FUTURE.cancelled()) |
| 646 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.cancelled()) |
| 647 | self.assertFalse(EXCEPTION_FUTURE.cancelled()) |
| 648 | self.assertFalse(SUCCESSFUL_FUTURE.cancelled()) |
| 649 | |
| 650 | def test_done(self): |
| 651 | self.assertFalse(PENDING_FUTURE.done()) |
| 652 | self.assertFalse(RUNNING_FUTURE.done()) |
| 653 | self.assertTrue(CANCELLED_FUTURE.done()) |
| 654 | self.assertTrue(CANCELLED_AND_NOTIFIED_FUTURE.done()) |
| 655 | self.assertTrue(EXCEPTION_FUTURE.done()) |
| 656 | self.assertTrue(SUCCESSFUL_FUTURE.done()) |
| 657 | |
| 658 | def test_running(self): |
| 659 | self.assertFalse(PENDING_FUTURE.running()) |
| 660 | self.assertTrue(RUNNING_FUTURE.running()) |
| 661 | self.assertFalse(CANCELLED_FUTURE.running()) |
| 662 | self.assertFalse(CANCELLED_AND_NOTIFIED_FUTURE.running()) |
| 663 | self.assertFalse(EXCEPTION_FUTURE.running()) |
| 664 | self.assertFalse(SUCCESSFUL_FUTURE.running()) |
| 665 | |
| 666 | def test_result_with_timeout(self): |
| 667 | self.assertRaises(futures.TimeoutError, |
| 668 | PENDING_FUTURE.result, timeout=0) |
| 669 | self.assertRaises(futures.TimeoutError, |
| 670 | RUNNING_FUTURE.result, timeout=0) |
| 671 | self.assertRaises(futures.CancelledError, |
| 672 | CANCELLED_FUTURE.result, timeout=0) |
| 673 | self.assertRaises(futures.CancelledError, |
| 674 | CANCELLED_AND_NOTIFIED_FUTURE.result, timeout=0) |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 675 | self.assertRaises(OSError, EXCEPTION_FUTURE.result, timeout=0) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 676 | self.assertEqual(SUCCESSFUL_FUTURE.result(timeout=0), 42) |
| 677 | |
| 678 | def test_result_with_success(self): |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 679 | # TODO(brian@sweetapp.com): This test is timing dependent. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 680 | def notification(): |
| 681 | # Wait until the main thread is waiting for the result. |
| 682 | time.sleep(1) |
| 683 | f1.set_result(42) |
| 684 | |
| 685 | f1 = create_future(state=PENDING) |
| 686 | t = threading.Thread(target=notification) |
| 687 | t.start() |
| 688 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 689 | self.assertEqual(f1.result(timeout=5), 42) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 690 | |
| 691 | def test_result_with_cancel(self): |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 692 | # TODO(brian@sweetapp.com): This test is timing dependent. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 693 | def notification(): |
| 694 | # Wait until the main thread is waiting for the result. |
| 695 | time.sleep(1) |
| 696 | f1.cancel() |
| 697 | |
| 698 | f1 = create_future(state=PENDING) |
| 699 | t = threading.Thread(target=notification) |
| 700 | t.start() |
| 701 | |
| 702 | self.assertRaises(futures.CancelledError, f1.result, timeout=5) |
| 703 | |
| 704 | def test_exception_with_timeout(self): |
| 705 | self.assertRaises(futures.TimeoutError, |
| 706 | PENDING_FUTURE.exception, timeout=0) |
| 707 | self.assertRaises(futures.TimeoutError, |
| 708 | RUNNING_FUTURE.exception, timeout=0) |
| 709 | self.assertRaises(futures.CancelledError, |
| 710 | CANCELLED_FUTURE.exception, timeout=0) |
| 711 | self.assertRaises(futures.CancelledError, |
| 712 | CANCELLED_AND_NOTIFIED_FUTURE.exception, timeout=0) |
| 713 | self.assertTrue(isinstance(EXCEPTION_FUTURE.exception(timeout=0), |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 714 | OSError)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 715 | self.assertEqual(SUCCESSFUL_FUTURE.exception(timeout=0), None) |
| 716 | |
| 717 | def test_exception_with_success(self): |
| 718 | def notification(): |
| 719 | # Wait until the main thread is waiting for the exception. |
| 720 | time.sleep(1) |
| 721 | with f1._condition: |
| 722 | f1._state = FINISHED |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 723 | f1._exception = OSError() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 724 | f1._condition.notify_all() |
| 725 | |
| 726 | f1 = create_future(state=PENDING) |
| 727 | t = threading.Thread(target=notification) |
| 728 | t.start() |
| 729 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 730 | self.assertTrue(isinstance(f1.exception(timeout=5), OSError)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 732 | @test.support.reap_threads |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 733 | def test_main(): |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 734 | try: |
Antoine Pitrou | 9816a1e | 2013-10-15 23:23:32 +0200 | [diff] [blame] | 735 | test.support.run_unittest(__name__) |
Antoine Pitrou | 9470ab4 | 2011-07-15 20:25:20 +0200 | [diff] [blame] | 736 | finally: |
| 737 | test.support.reap_children() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 738 | |
| 739 | if __name__ == "__main__": |
| 740 | test_main() |