File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change
1
+ Uses :py:class:`~asyncio.ThreadedChildWatcher` under POSIX to allow setting up test loop in non-main thread.
Original file line number Diff line number Diff line change @@ -497,7 +497,7 @@ def setup_test_loop(
497
497
# * https://stackoverflow.com/a/58614689/595220
498
498
# * https://bugs.python.org/issue35621
499
499
# * https://github.com/python/cpython/pull/14344
500
- watcher = asyncio .MultiLoopChildWatcher ()
500
+ watcher = asyncio .ThreadedChildWatcher ()
501
501
except AttributeError : # Python < 3.8
502
502
watcher = asyncio .SafeChildWatcher ()
503
503
watcher .attach_loop (loop )
Original file line number Diff line number Diff line change 6
6
import pytest
7
7
8
8
from aiohttp import web
9
- from aiohttp .test_utils import AioHTTPTestCase
9
+ from aiohttp .helpers import PY_38
10
+ from aiohttp .test_utils import AioHTTPTestCase , loop_context
10
11
11
12
12
13
@pytest .mark .skipif (
13
14
platform .system () == "Windows" , reason = "the test is not valid for Windows"
14
15
)
15
16
async def test_subprocess_co (loop : Any ) -> None :
16
- assert threading .current_thread () is threading .main_thread ()
17
+ assert PY_38 or threading .current_thread () is threading .main_thread ()
17
18
proc = await asyncio .create_subprocess_shell (
18
19
"exit 0" ,
19
20
stdin = asyncio .subprocess .DEVNULL ,
@@ -43,3 +44,24 @@ def test_default_loop(self) -> None:
43
44
44
45
def test_default_loop (loop : Any ) -> None :
45
46
assert asyncio .get_event_loop () is loop
47
+
48
+
49
+ @pytest .mark .xfail (not PY_38 , reason = "ThreadedChildWatcher is only available in 3.8+" )
50
+ def test_setup_loop_non_main_thread () -> None :
51
+ child_exc = None
52
+
53
+ def target () -> None :
54
+ try :
55
+ with loop_context () as loop :
56
+ assert asyncio .get_event_loop () is loop
57
+ loop .run_until_complete (test_subprocess_co (loop ))
58
+ except Exception as exc :
59
+ nonlocal child_exc
60
+ child_exc = exc
61
+
62
+ # Ensures setup_test_loop can be called by pytest-xdist in non-main thread.
63
+ t = threading .Thread (target = target )
64
+ t .start ()
65
+ t .join ()
66
+
67
+ assert child_exc is None
You can’t perform that action at this time.
0 commit comments