gh-100522 Add a test for 'futures.as_completed' timing out with a non-zero timeout value (#100523)

This commit is contained in:
JosephSBoyle 2023-01-26 07:01:11 +00:00 committed by GitHub
parent 73245d084e
commit a2262789ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 18 deletions

View File

@ -711,7 +711,6 @@ create_executor_tests(WaitTests,
class AsCompletedTests: class AsCompletedTests:
# TODO(brian@sweetapp.com): Should have a test with a non-zero timeout.
def test_no_timeout(self): def test_no_timeout(self):
future1 = self.executor.submit(mul, 2, 21) future1 = self.executor.submit(mul, 2, 21)
future2 = self.executor.submit(mul, 7, 6) future2 = self.executor.submit(mul, 7, 6)
@ -728,24 +727,29 @@ class AsCompletedTests:
future1, future2]), future1, future2]),
completed) completed)
def test_zero_timeout(self): def test_future_times_out(self):
future1 = self.executor.submit(time.sleep, 2) """Test ``futures.as_completed`` timing out before
completed_futures = set() completing it's final future."""
try: already_completed = {CANCELLED_AND_NOTIFIED_FUTURE,
for future in futures.as_completed( EXCEPTION_FUTURE,
[CANCELLED_AND_NOTIFIED_FUTURE, SUCCESSFUL_FUTURE}
EXCEPTION_FUTURE,
SUCCESSFUL_FUTURE,
future1],
timeout=0):
completed_futures.add(future)
except futures.TimeoutError:
pass
self.assertEqual(set([CANCELLED_AND_NOTIFIED_FUTURE, for timeout in (0, 0.01):
EXCEPTION_FUTURE, with self.subTest(timeout):
SUCCESSFUL_FUTURE]),
completed_futures) future = self.executor.submit(time.sleep, 0.1)
completed_futures = set()
try:
for f in futures.as_completed(
already_completed | {future},
timeout
):
completed_futures.add(f)
except futures.TimeoutError:
pass
# Check that ``future`` wasn't completed.
self.assertEqual(completed_futures, already_completed)
def test_duplicate_futures(self): def test_duplicate_futures(self):
# Issue 20367. Duplicate futures should not raise exceptions or give # Issue 20367. Duplicate futures should not raise exceptions or give