Skip to content

Commit 818fed4

Browse files
kumaraditya303seehwan80
authored andcommitted
pythongh-129874: improve tests to use correct implementations in asyncio (python#130516)
1 parent 474b495 commit 818fed4

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

Lib/test/test_asyncio/test_eager_task_factory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,15 @@ class PyEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase
268268
Task = tasks._PyTask
269269

270270
def setUp(self):
271+
self._all_tasks = asyncio.all_tasks
271272
self._current_task = asyncio.current_task
272273
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task
274+
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks
273275
return super().setUp()
274276

275277
def tearDown(self):
276278
asyncio.current_task = asyncio.tasks.current_task = self._current_task
279+
asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks
277280
return super().tearDown()
278281

279282

@@ -285,11 +288,14 @@ class CEagerTaskFactoryLoopTests(EagerTaskFactoryLoopTests, test_utils.TestCase)
285288

286289
def setUp(self):
287290
self._current_task = asyncio.current_task
291+
self._all_tasks = asyncio.all_tasks
288292
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task
293+
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks
289294
return super().setUp()
290295

291296
def tearDown(self):
292297
asyncio.current_task = asyncio.tasks.current_task = self._current_task
298+
asyncio.all_tasks = asyncio.tasks.all_tasks = self._all_tasks
293299
return super().tearDown()
294300

295301

Lib/test/test_asyncio/test_free_threading.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def coro():
3333
for _ in range(100):
3434
tasks.add(tg.create_task(coro()))
3535

36-
all_tasks = self.all_tasks(loop)
36+
all_tasks = asyncio.all_tasks(loop)
3737
self.assertEqual(len(all_tasks), 101)
3838

3939
for task in all_tasks:
@@ -77,15 +77,15 @@ async def main():
7777
for i in range(1000):
7878
with lock:
7979
asyncio.create_task(coro())
80-
tasks = self.all_tasks(loop)
80+
tasks = asyncio.all_tasks(loop)
8181
done.wait()
8282

8383
runner = threading.Thread(target=lambda: asyncio.run(main()))
8484

8585
def check():
8686
started.wait()
8787
with lock:
88-
self.assertSetEqual(tasks & self.all_tasks(loop), tasks)
88+
self.assertSetEqual(tasks & asyncio.all_tasks(loop), tasks)
8989

9090
threads = [threading.Thread(target=check) for _ in range(10)]
9191
runner.start()
@@ -167,15 +167,23 @@ async def main():
167167

168168

169169
class TestPyFreeThreading(TestFreeThreading, TestCase):
170-
all_tasks = staticmethod(asyncio.tasks._py_all_tasks)
171170

172171
def setUp(self):
173172
self._old_current_task = asyncio.current_task
174173
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task
174+
self._old_all_tasks = asyncio.all_tasks
175+
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks
176+
self._old_Task = asyncio.Task
177+
asyncio.Task = asyncio.tasks.Task = asyncio.tasks._PyTask
178+
self._old_Future = asyncio.Future
179+
asyncio.Future = asyncio.futures.Future = asyncio.futures._PyFuture
175180
return super().setUp()
176181

177182
def tearDown(self):
178183
asyncio.current_task = asyncio.tasks.current_task = self._old_current_task
184+
asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks
185+
asyncio.Task = asyncio.tasks.Task = self._old_Task
186+
asyncio.Future = asyncio.tasks.Future = self._old_Future
179187
return super().tearDown()
180188

181189
def factory(self, loop, coro, **kwargs):
@@ -184,15 +192,23 @@ def factory(self, loop, coro, **kwargs):
184192

185193
@unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio")
186194
class TestCFreeThreading(TestFreeThreading, TestCase):
187-
all_tasks = staticmethod(getattr(asyncio.tasks, "_c_all_tasks", None))
188195

189196
def setUp(self):
190197
self._old_current_task = asyncio.current_task
191198
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task
199+
self._old_all_tasks = asyncio.all_tasks
200+
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks
201+
self._old_Task = asyncio.Task
202+
asyncio.Task = asyncio.tasks.Task = asyncio.tasks._CTask
203+
self._old_Future = asyncio.Future
204+
asyncio.Future = asyncio.futures.Future = asyncio.futures._CFuture
192205
return super().setUp()
193206

194207
def tearDown(self):
195208
asyncio.current_task = asyncio.tasks.current_task = self._old_current_task
209+
asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks
210+
asyncio.Task = asyncio.tasks.Task = self._old_Task
211+
asyncio.Future = asyncio.futures.Future = self._old_Future
196212
return super().tearDown()
197213

198214

0 commit comments

Comments
 (0)