File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 11import asyncio
2+ import unittest .mock
23from unittest .mock import MagicMock
34
45import psutil
@@ -94,3 +95,18 @@ async def async_sleep():
9495 just_run (async_sleep ())
9596 diff .append (proc .num_fds () - fds_count )
9697 assert diff == [0 ] * 10
98+
99+
100+ def test_just_run_clears_new_loop ():
101+ async def async_sleep ():
102+ await asyncio .sleep (0.1 )
103+
104+ loop = asyncio .new_event_loop ()
105+ loop .stop = MagicMock (wraps = loop .stop )
106+ loop .close = MagicMock (wraps = loop .close )
107+
108+ with unittest .mock .patch .object (asyncio , "new_event_loop" , return_value = loop ):
109+ just_run (async_sleep ())
110+
111+ loop .stop .assert_called_once
112+ loop .close .assert_called_once
Original file line number Diff line number Diff line change @@ -57,7 +57,11 @@ def just_run(coro: Awaitable) -> Any:
5757
5858 nest_asyncio .apply ()
5959 check_patch_tornado ()
60- return loop .run_until_complete (coro )
60+ res = loop .run_until_complete (coro )
61+ if not had_running_loop :
62+ loop .stop ()
63+ loop .close ()
64+ return res
6165
6266
6367T = TypeVar ("T" )
You can’t perform that action at this time.
0 commit comments