|  | 
| 1 | 1 | import asyncio | 
| 2 | 2 | import unittest | 
|  | 3 | +import threading | 
|  | 4 | +import weakref | 
|  | 5 | +from test import support | 
| 3 | 6 | from threading import Thread | 
| 4 | 7 | from unittest import TestCase | 
| 5 | 8 | 
 | 
| @@ -58,6 +61,43 @@ def runner(): | 
| 58 | 61 |         with threading_helper.start_threads(threads): | 
| 59 | 62 |             pass | 
| 60 | 63 | 
 | 
|  | 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 | + | 
| 61 | 101 |     def test_run_coroutine_threadsafe(self) -> None: | 
| 62 | 102 |         results = [] | 
| 63 | 103 | 
 | 
|  | 
0 commit comments