Skip to content

Commit 3add9e4

Browse files
add test
1 parent 51f092d commit 3add9e4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Lib/test/test_asyncio/test_free_threading.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import threading
23
import unittest
34
from threading import Thread
45
from unittest import TestCase
@@ -58,6 +59,38 @@ def runner():
5859
with threading_helper.start_threads(threads):
5960
pass
6061

62+
def test_all_tasks_different_thread(self) -> None:
63+
loop = None
64+
started = threading.Event()
65+
66+
async def coro():
67+
await asyncio.sleep(0.01)
68+
69+
lock = threading.Lock()
70+
count = 0
71+
72+
async def main():
73+
nonlocal count, loop
74+
loop = asyncio.get_running_loop()
75+
started.set()
76+
for i in range(1000):
77+
with lock:
78+
asyncio.create_task(coro())
79+
count = len(self.all_tasks(loop))
80+
81+
runner = threading.Thread(target=lambda: asyncio.run(main()))
82+
83+
def check():
84+
started.wait()
85+
with lock:
86+
self.assertEqual(count, len(self.all_tasks(loop)))
87+
88+
threads = [threading.Thread(target=check) for _ in range(10)]
89+
threads.append(runner)
90+
91+
with threading_helper.start_threads(threads):
92+
pass
93+
6194
def test_run_coroutine_threadsafe(self) -> None:
6295
results = []
6396

0 commit comments

Comments
 (0)