Skip to content

Commit 0a49784

Browse files
committed
unit tests: merge into a single test method with test messages
1 parent cdf239c commit 0a49784

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

Lib/test/test_concurrent_futures/executor.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import weakref
55
from concurrent import futures
6+
from operator import add
67
from test import support
78
from test.support import Py_GIL_DISABLED
89

@@ -72,52 +73,52 @@ def test_map_timeout(self):
7273

7374
self.assertEqual([None, None], results)
7475

75-
def test_map_with_buffersize(self):
76-
iterable = range(4)
77-
with self.assertRaisesRegex(ValueError, "buffersize must be None or > 0"):
78-
self.executor.map(bool, iterable, buffersize=0)
76+
def test_map_buffersize(self):
77+
integers = range(8)
78+
79+
with self.assertRaisesRegex(
80+
ValueError,
81+
"buffersize must be None or > 0",
82+
msg="`Executor.map` should raise if `buffersize` is not positive.",
83+
):
84+
self.executor.map(str, integers, buffersize=0)
85+
86+
for buffersize in (1, 2, len(integers), len(integers) * 2):
87+
self.assertEqual(
88+
list(self.executor.map(str, integers, buffersize=buffersize)),
89+
list(map(str, integers)),
90+
msg="`Executor.map` with `buffersize` should behave the same as `map`.",
91+
)
92+
93+
self.assertEqual(
94+
list(self.executor.map(add, integers, integers, buffersize=buffersize)),
95+
list(map(sum, zip(integers, integers))),
96+
msg="`Executor.map` with `buffersize` should work correctly on multiple input iterables",
97+
)
98+
99+
self.assertEqual(
100+
next(self.executor.map(str, itertools.count(), buffersize=buffersize)),
101+
next(map(str, integers)),
102+
msg="`Executor.map` with `buffersize` should work correctly on an infinite input iterator.",
103+
)
104+
105+
self.assertFalse(
106+
list(self.executor.map(str, [], buffersize=buffersize)),
107+
msg="`Executor.map` with `buffersize` should return an empty iterator if the input iterable is empty.",
108+
)
109+
110+
self.assertFalse(
111+
list(self.executor.map(str, buffersize=buffersize)),
112+
msg="`Executor.map` with `buffersize` should return an empty iterator if no input iterable is provided.",
113+
)
114+
115+
integers_iter = iter(integers)
116+
self.executor.map(str, integers_iter, buffersize=buffersize)
117+
self.executor.shutdown(wait=True) # wait for pending tasks to complete
79118
self.assertEqual(
80-
list(self.executor.map(str, iterable, buffersize=1)),
81-
["0", "1", "2", "3"],
82-
)
83-
self.assertEqual(
84-
list(self.executor.map(str, iterable, buffersize=2)),
85-
["0", "1", "2", "3"],
86-
)
87-
88-
# test with multiple input iterables
89-
self.assertEqual(
90-
list(self.executor.map(int.__add__, iterable, iterable, buffersize=2)),
91-
[0, 2, 4, 6],
92-
)
93-
94-
# test without input iterable
95-
no_result = self.executor.map(bool, buffersize=2)
96-
with self.assertRaises(StopIteration):
97-
next(no_result)
98-
99-
def test_map_with_buffersize_on_infinite_iterable(self):
100-
results = self.executor.map(str, itertools.count(1), buffersize=1)
101-
self.assertEqual(next(iter(results)), "1")
102-
103-
def test_map_with_buffersize_on_iterable_smaller_than_buffer(self):
104-
iterable = range(2)
105-
results = self.executor.map(str, iterable, buffersize=8)
106-
self.assertListEqual(list(results), ["0", "1"])
107-
108-
def test_map_with_buffersize_on_empty_iterable(self):
109-
results = self.executor.map(str, [], buffersize=8)
110-
self.assertListEqual(list(results), [])
111-
112-
def test_map_with_buffersize_when_buffer_becomes_full(self):
113-
iterator = iter(range(8))
114-
buffersize = 4
115-
self.executor.map(str, iterator, buffersize=buffersize)
116-
self.executor.shutdown(wait=True)
117-
self.assertEqual(
118-
next(iterator),
119+
next(integers_iter),
119120
buffersize,
120-
msg="only the first `buffersize` elements should be processed",
121+
msg="`Executor.map` should pull only the first `buffersize` elements from the input iterable.",
121122
)
122123

123124
def test_shutdown_race_issue12456(self):

0 commit comments

Comments
 (0)