| 1 |
import sys |
| 2 |
import threading |
| 3 |
import unittest |
| 4 |
from concurrent import futures |
| 5 |
from test import support |
| 6 |
from test.support import threading_helper |
| 7 |
|
| 8 |
from .util import ( |
| 9 |
CANCELLED_FUTURE, CANCELLED_AND_NOTIFIED_FUTURE, EXCEPTION_FUTURE, |
| 10 |
SUCCESSFUL_FUTURE, |
| 11 |
create_executor_tests, setup_module, |
| 12 |
BaseTestCase, ThreadPoolMixin, |
| 13 |
ProcessPoolForkMixin, ProcessPoolForkserverMixin, ProcessPoolSpawnMixin) |
| 14 |
|
| 15 |
|
| 16 |
def mul(x, y): |
| 17 |
return x * y |
| 18 |
|
| 19 |
def wait_and_raise(e): |
| 20 |
e.wait() |
| 21 |
raise Exception('this is an exception') |
| 22 |
|
| 23 |
|
| 24 |
class WaitTests: |
| 25 |
def test_20369(self): |
| 26 |
# See https://bugs.python.org/issue20369 |
| 27 |
future = self.executor.submit(mul, 1, 2) |
| 28 |
done, not_done = futures.wait([future, future], |
| 29 |
return_when=futures.ALL_COMPLETED) |
| 30 |
self.assertEqual({future}, done) |
| 31 |
self.assertEqual(set(), not_done) |
| 32 |
|
| 33 |
|
| 34 |
def test_first_completed(self): |
| 35 |
event = self.create_event() |
| 36 |
future1 = self.executor.submit(mul, 21, 2) |
| 37 |
future2 = self.executor.submit(event.wait) |
| 38 |
|
| 39 |
try: |
| 40 |
done, not_done = futures.wait( |
| 41 |
[CANCELLED_FUTURE, future1, future2], |
| 42 |
return_when=futures.FIRST_COMPLETED) |
| 43 |
|
| 44 |
self.assertEqual(set([future1]), done) |
| 45 |
self.assertEqual(set([CANCELLED_FUTURE, future2]), not_done) |
| 46 |
finally: |
| 47 |
event.set() |
| 48 |
future2.result() # wait for job to finish |
| 49 |
|
| 50 |
def test_first_completed_some_already_completed(self): |
| 51 |
event = self.create_event() |
| 52 |
future1 = self.executor.submit(event.wait) |
| 53 |
|
| 54 |
try: |
| 55 |
finished, pending = futures.wait( |
| 56 |
[CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE, future1], |
| 57 |
return_when=futures.FIRST_COMPLETED) |
| 58 |
|
| 59 |
self.assertEqual( |
| 60 |
set([CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE]), |
| 61 |
finished) |
| 62 |
self.assertEqual(set([future1]), pending) |
| 63 |
finally: |
| 64 |
event.set() |
| 65 |
future1.result() # wait for job to finish |
| 66 |
|
| 67 |
def test_first_exception(self): |
| 68 |
event1 = self.create_event() |
| 69 |
event2 = self.create_event() |
| 70 |
try: |
| 71 |
future1 = self.executor.submit(mul, 2, 21) |
| 72 |
future2 = self.executor.submit(wait_and_raise, event1) |
| 73 |
future3 = self.executor.submit(event2.wait) |
| 74 |
|
| 75 |
# Ensure that future1 is completed before future2 finishes |
| 76 |
def wait_for_future1(): |
| 77 |
future1.result() |
| 78 |
event1.set() |
| 79 |
|
| 80 |
t = threading.Thread(target=wait_for_future1) |
| 81 |
t.start() |
| 82 |
|
| 83 |
finished, pending = futures.wait( |
| 84 |
[future1, future2, future3], |
| 85 |
return_when=futures.FIRST_EXCEPTION) |
| 86 |
|
| 87 |
self.assertEqual(set([future1, future2]), finished) |
| 88 |
self.assertEqual(set([future3]), pending) |
| 89 |
|
| 90 |
threading_helper.join_thread(t) |
| 91 |
finally: |
| 92 |
event1.set() |
| 93 |
event2.set() |
| 94 |
future3.result() # wait for job to finish |
| 95 |
|
| 96 |
def test_first_exception_some_already_complete(self): |
| 97 |
event = self.create_event() |
| 98 |
future1 = self.executor.submit(divmod, 21, 0) |
| 99 |
future2 = self.executor.submit(event.wait) |
| 100 |
|
| 101 |
try: |
| 102 |
finished, pending = futures.wait( |
| 103 |
[SUCCESSFUL_FUTURE, |
| 104 |
CANCELLED_FUTURE, |
| 105 |
CANCELLED_AND_NOTIFIED_FUTURE, |
| 106 |
future1, future2], |
| 107 |
return_when=futures.FIRST_EXCEPTION) |
| 108 |
|
| 109 |
self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 110 |
CANCELLED_AND_NOTIFIED_FUTURE, |
| 111 |
future1]), finished) |
| 112 |
self.assertEqual(set([CANCELLED_FUTURE, future2]), pending) |
| 113 |
finally: |
| 114 |
event.set() |
| 115 |
future2.result() # wait for job to finish |
| 116 |
|
| 117 |
def test_first_exception_one_already_failed(self): |
| 118 |
event = self.create_event() |
| 119 |
future1 = self.executor.submit(event.wait) |
| 120 |
|
| 121 |
try: |
| 122 |
finished, pending = futures.wait( |
| 123 |
[EXCEPTION_FUTURE, future1], |
| 124 |
return_when=futures.FIRST_EXCEPTION) |
| 125 |
|
| 126 |
self.assertEqual(set([EXCEPTION_FUTURE]), finished) |
| 127 |
self.assertEqual(set([future1]), pending) |
| 128 |
finally: |
| 129 |
event.set() |
| 130 |
future1.result() # wait for job to finish |
| 131 |
|
| 132 |
def test_all_completed(self): |
| 133 |
future1 = self.executor.submit(divmod, 2, 0) |
| 134 |
future2 = self.executor.submit(mul, 2, 21) |
| 135 |
|
| 136 |
finished, pending = futures.wait( |
| 137 |
[SUCCESSFUL_FUTURE, |
| 138 |
CANCELLED_AND_NOTIFIED_FUTURE, |
| 139 |
EXCEPTION_FUTURE, |
| 140 |
future1, |
| 141 |
future2], |
| 142 |
return_when=futures.ALL_COMPLETED) |
| 143 |
|
| 144 |
self.assertEqual(set([SUCCESSFUL_FUTURE, |
| 145 |
CANCELLED_AND_NOTIFIED_FUTURE, |
| 146 |
EXCEPTION_FUTURE, |
| 147 |
future1, |
| 148 |
future2]), finished) |
| 149 |
self.assertEqual(set(), pending) |
| 150 |
|
| 151 |
def test_timeout(self): |
| 152 |
short_timeout = 0.050 |
| 153 |
|
| 154 |
event = self.create_event() |
| 155 |
future = self.executor.submit(event.wait) |
| 156 |
|
| 157 |
try: |
| 158 |
finished, pending = futures.wait( |
| 159 |
[CANCELLED_AND_NOTIFIED_FUTURE, |
| 160 |
EXCEPTION_FUTURE, |
| 161 |
SUCCESSFUL_FUTURE, |
| 162 |
future], |
| 163 |
timeout=short_timeout, |
| 164 |
return_when=futures.ALL_COMPLETED) |
| 165 |
|
| 166 |
self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, |
| 167 |
EXCEPTION_FUTURE, |
| 168 |
SUCCESSFUL_FUTURE]), |
| 169 |
finished) |
| 170 |
self.assertEqual(set([future]), pending) |
| 171 |
finally: |
| 172 |
event.set() |
| 173 |
future.result() # wait for job to finish |
| 174 |
|
| 175 |
|
| 176 |
class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, BaseTestCase): |
| 177 |
|
| 178 |
def test_pending_calls_race(self): |
| 179 |
# Issue #14406: multi-threaded race condition when waiting on all |
| 180 |
# futures. |
| 181 |
event = threading.Event() |
| 182 |
def future_func(): |
| 183 |
event.wait() |
| 184 |
oldswitchinterval = sys.getswitchinterval() |
| 185 |
support.setswitchinterval(1e-6) |
| 186 |
try: |
| 187 |
fs = {self.executor.submit(future_func) for i in range(100)} |
| 188 |
event.set() |
| 189 |
futures.wait(fs, return_when=futures.ALL_COMPLETED) |
| 190 |
finally: |
| 191 |
sys.setswitchinterval(oldswitchinterval) |
| 192 |
|
| 193 |
|
| 194 |
create_executor_tests(globals(), WaitTests, |
| 195 |
executor_mixins=(ProcessPoolForkMixin, |
| 196 |
ProcessPoolForkserverMixin, |
| 197 |
ProcessPoolSpawnMixin)) |
| 198 |
|
| 199 |
|
| 200 |
def setUpModule(): |
| 201 |
setup_module() |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == "__main__": |
| 205 |
unittest.main() |