Skip to content

Commit 150e913

Browse files
Tin TvrtkovićTin Tvrtković
Tin Tvrtković
authored and
Tin Tvrtković
committed
Fix subprocess handling when forbid_global_loop.
1 parent eef8e0b commit 150e913

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ Changelog
169169
0.6.0 (UNRELEASED)
170170
~~~~~~~~~~~~~~~~~~
171171
- ``pytestmark`` now works on both module and class level.
172+
- Using ``forbid_global_loop`` now allows tests to use ``asyncio``
173+
subprocesses.
174+
`#36 <https://github.com/pytest-dev/pytest-asyncio/issues/36>`_
172175

173176
0.5.0 (2016-09-07)
174177
~~~~~~~~~~~~~~~~~~

pytest_asyncio/plugin.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""pytest-asyncio implementation."""
12
import asyncio
23
import inspect
34
import socket
@@ -10,9 +11,18 @@
1011
from _pytest.python import transfer_markers
1112

1213

13-
class ForbiddenEventLoopPolicy(asyncio.AbstractEventLoopPolicy):
14-
"""An event loop policy that raises errors on any operation."""
15-
pass
14+
class ForbiddenEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
15+
"""An event loop policy that raises errors on most operations.
16+
17+
Operations involving child watchers are permitted."""
18+
19+
def get_event_loop(self):
20+
"""Not allowed."""
21+
raise NotImplementedError
22+
23+
def set_event_loop(self, _):
24+
"""Not allowed."""
25+
raise NotImplementedError
1626

1727

1828
def _is_coroutine(obj):
@@ -21,6 +31,7 @@ def _is_coroutine(obj):
2131

2232

2333
def pytest_configure(config):
34+
"""Inject documentation."""
2435
config.addinivalue_line("markers",
2536
"asyncio: "
2637
"mark the test as a coroutine, it will be "
@@ -65,6 +76,7 @@ def pytest_fixture_setup(fixturedef, request):
6576
policy = asyncio.get_event_loop_policy()
6677
if forbid_global_loop:
6778
asyncio.set_event_loop_policy(ForbiddenEventLoopPolicy())
79+
asyncio.get_child_watcher().attach_loop(loop)
6880
fixturedef.addfinalizer(lambda: asyncio.set_event_loop_policy(policy))
6981
else:
7082
policy.set_event_loop(loop)

test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
coverage==4.1
2+
tox==2.5.0

tests/test_subprocess.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Tests for using subprocesses in tests."""
2+
import sys
3+
import asyncio
4+
import asyncio.subprocess
5+
6+
import pytest
7+
8+
9+
@pytest.mark.asyncio(forbid_global_loop=False)
10+
@asyncio.coroutine
11+
def test_subprocess(event_loop):
12+
"""Starting a subprocess should be possible."""
13+
proc = yield from asyncio.subprocess.create_subprocess_exec(
14+
sys.executable, '--version', stdout=asyncio.subprocess.PIPE,
15+
loop=event_loop)
16+
yield from proc.communicate()
17+
18+
19+
@pytest.mark.asyncio(forbid_global_loop=True)
20+
@asyncio.coroutine
21+
def test_subprocess_forbid(event_loop):
22+
"""Starting a subprocess should be possible."""
23+
proc = yield from asyncio.subprocess.create_subprocess_exec(
24+
sys.executable, '--version', stdout=asyncio.subprocess.PIPE,
25+
loop=event_loop)
26+
yield from proc.communicate()

0 commit comments

Comments
 (0)