| 1 |
import threading |
| 2 |
import time |
| 3 |
import unittest |
| 4 |
import weakref |
| 5 |
from concurrent import futures |
| 6 |
from test import support |
| 7 |
from test.support import Py_GIL_DISABLED |
| 8 |
|
| 9 |
|
| 10 |
def mul(x, y): |
| 11 |
return x * y |
| 12 |
|
| 13 |
def capture(*args, **kwargs): |
| 14 |
return args, kwargs |
| 15 |
|
| 16 |
|
| 17 |
class MyObject(object): |
| 18 |
def my_method(self): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
def make_dummy_object(_): |
| 23 |
return MyObject() |
| 24 |
|
| 25 |
|
| 26 |
# Used in test_swallows_falsey_exceptions |
| 27 |
def raiser(exception, msg='std'): |
| 28 |
raise exception(msg) |
| 29 |
|
| 30 |
|
| 31 |
class FalseyBoolException(Exception): |
| 32 |
def __bool__(self): |
| 33 |
return False |
| 34 |
|
| 35 |
|
| 36 |
class FalseyLenException(Exception): |
| 37 |
def __len__(self): |
| 38 |
return 0 |
| 39 |
|
| 40 |
|
| 41 |
class ExecutorTest: |
| 42 |
# Executor.shutdown() and context manager usage is tested by |
| 43 |
# ExecutorShutdownTest. |
| 44 |
def test_submit(self): |
| 45 |
future = self.executor.submit(pow, 2, 8) |
| 46 |
self.assertEqual(256, future.result()) |
| 47 |
|
| 48 |
def test_submit_keyword(self): |
| 49 |
future = self.executor.submit(mul, 2, y=8) |
| 50 |
self.assertEqual(16, future.result()) |
| 51 |
future = self.executor.submit(capture, 1, self=2, fn=3) |
| 52 |
self.assertEqual(future.result(), ((1,), {'self': 2, 'fn': 3})) |
| 53 |
with self.assertRaises(TypeError): |
| 54 |
self.executor.submit(fn=capture, arg=1) |
| 55 |
with self.assertRaises(TypeError): |
| 56 |
self.executor.submit(arg=1) |
| 57 |
|
| 58 |
def test_map(self): |
| 59 |
self.assertEqual( |
| 60 |
list(self.executor.map(pow, range(10), range(10))), |
| 61 |
list(map(pow, range(10), range(10)))) |
| 62 |
|
| 63 |
self.assertEqual( |
| 64 |
list(self.executor.map(pow, range(10), range(10), chunksize=3)), |
| 65 |
list(map(pow, range(10), range(10)))) |
| 66 |
|
| 67 |
def test_map_exception(self): |
| 68 |
i = self.executor.map(divmod, [1, 1, 1, 1], [2, 3, 0, 5]) |
| 69 |
self.assertEqual(i.__next__(), (0, 1)) |
| 70 |
self.assertEqual(i.__next__(), (0, 1)) |
| 71 |
self.assertRaises(ZeroDivisionError, i.__next__) |
| 72 |
|
| 73 |
@support.requires_resource('walltime') |
| 74 |
def test_map_timeout(self): |
| 75 |
results = [] |
| 76 |
try: |
| 77 |
for i in self.executor.map(time.sleep, |
| 78 |
[0, 0, 6], |
| 79 |
timeout=5): |
| 80 |
results.append(i) |
| 81 |
except futures.TimeoutError: |
| 82 |
pass |
| 83 |
else: |
| 84 |
self.fail('expected TimeoutError') |
| 85 |
|
| 86 |
self.assertEqual([None, None], results) |
| 87 |
|
| 88 |
def test_shutdown_race_issue12456(self): |
| 89 |
# Issue #12456: race condition at shutdown where trying to post a |
| 90 |
# sentinel in the call queue blocks (the queue is full while processes |
| 91 |
# have exited). |
| 92 |
self.executor.map(str, [2] * (self.worker_count + 1)) |
| 93 |
self.executor.shutdown() |
| 94 |
|
| 95 |
@support.cpython_only |
| 96 |
def test_no_stale_references(self): |
| 97 |
# Issue #16284: check that the executors don't unnecessarily hang onto |
| 98 |
# references. |
| 99 |
my_object = MyObject() |
| 100 |
my_object_collected = threading.Event() |
| 101 |
def set_event(): |
| 102 |
if Py_GIL_DISABLED: |
| 103 |
# gh-117688 Avoid deadlock by setting the event in a |
| 104 |
# background thread. The current thread may be in the middle |
| 105 |
# of the my_object_collected.wait() call, which holds locks |
| 106 |
# needed by my_object_collected.set(). |
| 107 |
threading.Thread(target=my_object_collected.set).start() |
| 108 |
else: |
| 109 |
my_object_collected.set() |
| 110 |
my_object_callback = weakref.ref(my_object, lambda obj: set_event()) |
| 111 |
# Deliberately discarding the future. |
| 112 |
self.executor.submit(my_object.my_method) |
| 113 |
del my_object |
| 114 |
|
| 115 |
if Py_GIL_DISABLED: |
| 116 |
# Due to biased reference counting, my_object might only be |
| 117 |
# deallocated while the thread that created it runs -- if the |
| 118 |
# thread is paused waiting on an event, it may not merge the |
| 119 |
# refcount of the queued object. For that reason, we alternate |
| 120 |
# between running the GC and waiting for the event. |
| 121 |
wait_time = 0 |
| 122 |
collected = False |
| 123 |
while not collected and wait_time <= support.SHORT_TIMEOUT: |
| 124 |
support.gc_collect() |
| 125 |
collected = my_object_collected.wait(timeout=1.0) |
| 126 |
wait_time += 1.0 |
| 127 |
else: |
| 128 |
collected = my_object_collected.wait(timeout=support.SHORT_TIMEOUT) |
| 129 |
self.assertTrue(collected, |
| 130 |
"Stale reference not collected within timeout.") |
| 131 |
|
| 132 |
def test_max_workers_negative(self): |
| 133 |
for number in (0, -1): |
| 134 |
with self.assertRaisesRegex(ValueError, |
| 135 |
"max_workers must be greater " |
| 136 |
"than 0"): |
| 137 |
self.executor_type(max_workers=number) |
| 138 |
|
| 139 |
def test_free_reference(self): |
| 140 |
# Issue #14406: Result iterator should not keep an internal |
| 141 |
# reference to result objects. |
| 142 |
for obj in self.executor.map(make_dummy_object, range(10)): |
| 143 |
wr = weakref.ref(obj) |
| 144 |
del obj |
| 145 |
support.gc_collect() # For PyPy or other GCs. |
| 146 |
|
| 147 |
for _ in support.sleeping_retry(support.SHORT_TIMEOUT): |
| 148 |
if wr() is None: |
| 149 |
break |
| 150 |
|
| 151 |
def test_swallows_falsey_exceptions(self): |
| 152 |
# see gh-132063: Prevent exceptions that evaluate as falsey |
| 153 |
# from being ignored. |
| 154 |
# Recall: `x` is falsey if `len(x)` returns 0 or `bool(x)` returns False. |
| 155 |
|
| 156 |
msg = 'boolbool' |
| 157 |
with self.assertRaisesRegex(FalseyBoolException, msg): |
| 158 |
self.executor.submit(raiser, FalseyBoolException, msg).result() |
| 159 |
|
| 160 |
msg = 'lenlen' |
| 161 |
with self.assertRaisesRegex(FalseyLenException, msg): |
| 162 |
self.executor.submit(raiser, FalseyLenException, msg).result() |