File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 1
1
import asyncio
2
+ import unittest .mock
2
3
from unittest .mock import MagicMock
3
4
4
5
import psutil
@@ -94,3 +95,18 @@ async def async_sleep():
94
95
just_run (async_sleep ())
95
96
diff .append (proc .num_fds () - fds_count )
96
97
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:
57
57
58
58
nest_asyncio .apply ()
59
59
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
61
65
62
66
63
67
T = TypeVar ("T" )
You can’t perform that action at this time.
0 commit comments