Skip to content

Commit a226278

Browse files
authored
gh-100522 Add a test for 'futures.as_completed' timing out with a non-zero timeout value (#100523)
1 parent 73245d0 commit a226278

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Lib/test/test_concurrent_futures.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ def future_func():
711711

712712

713713
class AsCompletedTests:
714-
# TODO([email protected]): Should have a test with a non-zero timeout.
715714
def test_no_timeout(self):
716715
future1 = self.executor.submit(mul, 2, 21)
717716
future2 = self.executor.submit(mul, 7, 6)
@@ -728,24 +727,29 @@ def test_no_timeout(self):
728727
future1, future2]),
729728
completed)
730729

731-
def test_zero_timeout(self):
732-
future1 = self.executor.submit(time.sleep, 2)
733-
completed_futures = set()
734-
try:
735-
for future in futures.as_completed(
736-
[CANCELLED_AND_NOTIFIED_FUTURE,
737-
EXCEPTION_FUTURE,
738-
SUCCESSFUL_FUTURE,
739-
future1],
740-
timeout=0):
741-
completed_futures.add(future)
742-
except futures.TimeoutError:
743-
pass
730+
def test_future_times_out(self):
731+
"""Test ``futures.as_completed`` timing out before
732+
completing it's final future."""
733+
already_completed = {CANCELLED_AND_NOTIFIED_FUTURE,
734+
EXCEPTION_FUTURE,
735+
SUCCESSFUL_FUTURE}
744736

745-
self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE,
746-
EXCEPTION_FUTURE,
747-
SUCCESSFUL_FUTURE]),
748-
completed_futures)
737+
for timeout in (0, 0.01):
738+
with self.subTest(timeout):
739+
740+
future = self.executor.submit(time.sleep, 0.1)
741+
completed_futures = set()
742+
try:
743+
for f in futures.as_completed(
744+
already_completed | {future},
745+
timeout
746+
):
747+
completed_futures.add(f)
748+
except futures.TimeoutError:
749+
pass
750+
751+
# Check that ``future`` wasn't completed.
752+
self.assertEqual(completed_futures, already_completed)
749753

750754
def test_duplicate_futures(self):
751755
# Issue 20367. Duplicate futures should not raise exceptions or give

0 commit comments

Comments
 (0)