Skip to content

Commit 5f5b95e

Browse files
add more tests
1 parent d0fbbc2 commit 5f5b95e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Lib/test/test_asyncio/test_free_threading.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import asyncio
22
import unittest
3+
import threading
4+
import weakref
5+
from test import support
36
from threading import Thread
47
from unittest import TestCase
58

@@ -58,6 +61,43 @@ def runner():
5861
with threading_helper.start_threads(threads):
5962
pass
6063

64+
def test_all_tasks_different_thread(self) -> None:
65+
task = None
66+
loop = None
67+
started = threading.Event()
68+
stop = threading.Event()
69+
done = False
70+
async def func():
71+
nonlocal task, loop, done
72+
loop = asyncio.get_running_loop()
73+
task = asyncio.current_task()
74+
started.set()
75+
while not stop.is_set():
76+
await asyncio.sleep(0)
77+
78+
thread = Thread(target=lambda: asyncio.run(func()))
79+
with threading_helper.start_threads([thread]):
80+
started.wait()
81+
self.assertSetEqual(asyncio.all_tasks(loop), {task})
82+
self.assertIs(task.get_loop(), loop)
83+
stop.set()
84+
85+
def test_all_tasks_different_thread_finalized(self) -> None:
86+
task = None
87+
loop = asyncio.EventLoop()
88+
async def func():
89+
nonlocal task
90+
task = asyncio.current_task()
91+
92+
loop.run_until_complete(func())
93+
94+
self.assertEqual(self.all_tasks(loop), set())
95+
wr = weakref.ref(task)
96+
del task
97+
# task finalization in different thread shoudn't crash
98+
support.gc_collect()
99+
self.assertIsNone(wr())
100+
61101
def test_run_coroutine_threadsafe(self) -> None:
62102
results = []
63103

0 commit comments

Comments
 (0)