]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129874: improve tests to use correct implementations in asyncio (#130516)
authorKumar Aditya <kumaraditya@python.org>
Mon, 24 Feb 2025 16:03:52 +0000 (21:33 +0530)
committerGitHub <noreply@github.com>
Mon, 24 Feb 2025 16:03:52 +0000 (21:33 +0530)
Lib/test/test_asyncio/test_eager_task_factory.py
Lib/test/test_asyncio/test_free_threading.py

index bb0760a6967dbacc82b0d9d0f85ed51d44abcea0..a2fb1022ae4d3fb2b7ff971d9fe941c26623d219 100644 (file)
@@ -268,12 +268,15 @@ class PyEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase
     Task = tasks._PyTask
 
     def setUp(self):
+        self._all_tasks = asyncio.all_tasks
         self._current_task = asyncio.current_task
         asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks
         return super().setUp()
 
     def tearDown(self):
         asyncio.current_task = asyncio.tasks.current_task = self._current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks
         return super().tearDown()
 
 
@@ -285,11 +288,14 @@ class CEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase)
 
     def setUp(self):
         self._current_task = asyncio.current_task
+        self._all_tasks = asyncio.all_tasks
         asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks
         return super().setUp()
 
     def tearDown(self):
         asyncio.current_task = asyncio.tasks.current_task = self._current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks
         return super().tearDown()
 
 
index 199dbbdda5e8a6f14b6297c35b1690ccc6794250..110996c348554cef719a46a58c648c45367312c2 100644 (file)
@@ -33,7 +33,7 @@ class TestFreeThreading:
                 for _ in range(100):
                     tasks.add(tg.create_task(coro()))
 
-                all_tasks = self.all_tasks(loop)
+                all_tasks = asyncio.all_tasks(loop)
                 self.assertEqual(len(all_tasks), 101)
 
                 for task in all_tasks:
@@ -77,7 +77,7 @@ class TestFreeThreading:
             for i in range(1000):
                 with lock:
                     asyncio.create_task(coro())
-                    tasks = self.all_tasks(loop)
+                    tasks = asyncio.all_tasks(loop)
             done.wait()
 
         runner = threading.Thread(target=lambda: asyncio.run(main()))
@@ -85,7 +85,7 @@ class TestFreeThreading:
         def check():
             started.wait()
             with lock:
-                self.assertSetEqual(tasks & self.all_tasks(loop), tasks)
+                self.assertSetEqual(tasks & asyncio.all_tasks(loop), tasks)
 
         threads = [threading.Thread(target=check) for _ in range(10)]
         runner.start()
@@ -167,15 +167,23 @@ class TestFreeThreading:
 
 
 class TestPyFreeThreading(TestFreeThreading, TestCase):
-    all_tasks = staticmethod(asyncio.tasks._py_all_tasks)
 
     def setUp(self):
         self._old_current_task = asyncio.current_task
         asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task
+        self._old_all_tasks = asyncio.all_tasks
+        asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks
+        self._old_Task = asyncio.Task
+        asyncio.Task = asyncio.tasks.Task = asyncio.tasks._PyTask
+        self._old_Future = asyncio.Future
+        asyncio.Future = asyncio.futures.Future = asyncio.futures._PyFuture
         return super().setUp()
 
     def tearDown(self):
         asyncio.current_task = asyncio.tasks.current_task = self._old_current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks
+        asyncio.Task = asyncio.tasks.Task = self._old_Task
+        asyncio.Future = asyncio.tasks.Future = self._old_Future
         return super().tearDown()
 
     def factory(self, loop, coro, **kwargs):
@@ -184,15 +192,23 @@ class TestPyFreeThreading(TestFreeThreading, TestCase):
 
 @unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio")
 class TestCFreeThreading(TestFreeThreading, TestCase):
-    all_tasks = staticmethod(getattr(asyncio.tasks, "_c_all_tasks", None))
 
     def setUp(self):
         self._old_current_task = asyncio.current_task
         asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task
+        self._old_all_tasks = asyncio.all_tasks
+        asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks
+        self._old_Task = asyncio.Task
+        asyncio.Task = asyncio.tasks.Task = asyncio.tasks._CTask
+        self._old_Future = asyncio.Future
+        asyncio.Future = asyncio.futures.Future = asyncio.futures._CFuture
         return super().setUp()
 
     def tearDown(self):
         asyncio.current_task = asyncio.tasks.current_task = self._old_current_task
+        asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks
+        asyncio.Task = asyncio.tasks.Task = self._old_Task
+        asyncio.Future = asyncio.futures.Future = self._old_Future
         return super().tearDown()