gh-128364: Fix flaky test_concurrent_futures.test_wait tests (gh-130742)

Use events instead of relying on `time.sleep()`. The tests are also now about
four times faster.
This commit is contained in:
Sam Gross 2025-03-06 12:30:58 -05:00 committed by GitHub
parent 052cb717f5
commit c4d37eefb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 119 additions and 58 deletions

View file

@ -1,5 +1,6 @@
import multiprocessing
import sys
import threading
import time
import unittest
from concurrent import futures
@ -50,14 +51,19 @@ class ExecutorMixin:
max_workers=self.worker_count,
mp_context=self.get_context(),
**self.executor_kwargs)
self.manager = self.get_context().Manager()
else:
self.executor = self.executor_type(
max_workers=self.worker_count,
**self.executor_kwargs)
self.manager = None
def tearDown(self):
self.executor.shutdown(wait=True)
self.executor = None
if self.manager is not None:
self.manager.shutdown()
self.manager = None
dt = time.monotonic() - self.t1
if support.verbose:
@ -73,11 +79,17 @@ class ExecutorMixin:
class ThreadPoolMixin(ExecutorMixin):
executor_type = futures.ThreadPoolExecutor
def create_event(self):
return threading.Event()
@support.skip_if_sanitizer("gh-129824: data races in InterpreterPool tests", thread=True)
class InterpreterPoolMixin(ExecutorMixin):
executor_type = futures.InterpreterPoolExecutor
def create_event(self):
self.skipTest("InterpreterPoolExecutor doesn't support events")
class ProcessPoolForkMixin(ExecutorMixin):
executor_type = futures.ProcessPoolExecutor
@ -94,6 +106,9 @@ class ProcessPoolForkMixin(ExecutorMixin):
self.skipTest("TSAN doesn't support threads after fork")
return super().get_context()
def create_event(self):
return self.manager.Event()
class ProcessPoolSpawnMixin(ExecutorMixin):
executor_type = futures.ProcessPoolExecutor
@ -106,6 +121,9 @@ class ProcessPoolSpawnMixin(ExecutorMixin):
self.skipTest("ProcessPoolExecutor unavailable on this system")
return super().get_context()
def create_event(self):
return self.manager.Event()
class ProcessPoolForkserverMixin(ExecutorMixin):
executor_type = futures.ProcessPoolExecutor
@ -122,6 +140,9 @@ class ProcessPoolForkserverMixin(ExecutorMixin):
self.skipTest("TSAN doesn't support threads after fork")
return super().get_context()
def create_event(self):
return self.manager.Event()
def create_executor_tests(remote_globals, mixin, bases=(BaseTestCase,),
executor_mixins=(ThreadPoolMixin,