Skip to content

Commit 1ec6824

Browse files
colesburymiss-islington
authored andcommitted
pythongh-128364: Fix flaky test_timeout test (pythongh-130724)
(cherry picked from commit cfa0b1d) Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent 137595f commit 1ec6824

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Lib/test/test_concurrent_futures/test_wait.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ def test_all_completed(self):
114114

115115
def test_timeout(self):
116116
short_timeout = 0.050
117-
long_timeout = short_timeout * 10
118117

119-
future = self.executor.submit(time.sleep, long_timeout)
118+
future = self.executor.submit(self.event.wait)
120119

121120
finished, pending = futures.wait(
122121
[CANCELLED_AND_NOTIFIED_FUTURE,
@@ -132,6 +131,9 @@ def test_timeout(self):
132131
finished)
133132
self.assertEqual(set([future]), pending)
134133

134+
# Set the event to allow the future to complete
135+
self.event.set()
136+
135137

136138
class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, BaseTestCase):
137139

Lib/test/test_concurrent_futures/util.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import multiprocessing
22
import sys
3+
import threading
34
import time
45
import unittest
56
from concurrent import futures
@@ -46,18 +47,24 @@ def setUp(self):
4647

4748
self.t1 = time.monotonic()
4849
if hasattr(self, "ctx"):
50+
self.manager = multiprocessing.Manager()
51+
self.event = self.manager.Event()
4952
self.executor = self.executor_type(
5053
max_workers=self.worker_count,
5154
mp_context=self.get_context(),
5255
**self.executor_kwargs)
5356
else:
57+
self.event = threading.Event()
5458
self.executor = self.executor_type(
5559
max_workers=self.worker_count,
5660
**self.executor_kwargs)
5761

5862
def tearDown(self):
5963
self.executor.shutdown(wait=True)
6064
self.executor = None
65+
if hasattr(self, "ctx"):
66+
self.manager.shutdown()
67+
self.manager = None
6168

6269
dt = time.monotonic() - self.t1
6370
if support.verbose:

0 commit comments

Comments
 (0)