Skip to content

Commit

Permalink
Parametrize the tests to use uvloop
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Apr 29, 2024
1 parent 7577fbf commit 150213d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"pytest-asyncio",
"pytest-cov",
"pytest-repeat",
"uvloop",
],
license="GPLv3",
python_requires=">=3.8",
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pytest
pytest-asyncio
pytest-cov
pytest-repeat
uvloop
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
import asyncio
import uvloop


@pytest.fixture(
params=(
asyncio.DefaultEventLoopPolicy(),
uvloop.EventLoopPolicy(),
),
ids=("default", "uvloop"),
)
def event_loop_policy(request):
return request.param


@pytest.fixture
def is_uvloop(event_loop_policy):
return isinstance(event_loop_policy, uvloop.EventLoopPolicy)
15 changes: 10 additions & 5 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on windows")
@pytest.mark.asyncio
async def test_create_standard_stream_with_pipe():
async def test_create_standard_stream_with_pipe(is_uvloop):
r1, w1 = os.pipe()
r2, w2 = os.pipe()
stdin = open(r1)
Expand All @@ -34,8 +34,12 @@ async def test_create_standard_stream_with_pipe():
await writer2.drain()
assert os.read(r2, 2) == b"b\n"

# Uvloop works differently
if is_uvloop:
return

# Mock transport and stdout
stdout.close()
stdout_actual_close = stdout.close
stdout.close = Mock()
stdout_transport = writer1._transport
stdout_transport.close = Mock()
Expand All @@ -47,6 +51,7 @@ async def test_create_standard_stream_with_pipe():
# Check that the transport has been closed but not stdout
stdout_transport.close.assert_called_once_with()
assert not stdout.close.called
stdout_actual_close()


@pytest.mark.asyncio
Expand Down Expand Up @@ -168,21 +173,21 @@ async def test_read_from_closed_pipe():
result = await ainput(">>> ", streams=(reader, writer1))
assert result == "hello"

f_stdout.close()
writer1.close()
await writer1.wait_closed()
f_stdout.close()

f_stderr.close()
writer2.close()
await writer2.wait_closed()
f_stderr.close()

assert open(stdout_r).read() == ">>> "
assert open(stderr_r).read() == ""


@pytest.mark.skipif(sys.platform == "win32", reason="Not supported on windows")
@pytest.mark.asyncio
async def test_standard_stream_pipe_buffering():
async def test_standard_stream_pipe_buffering(is_uvloop):
r1, w1 = os.pipe()
r2, w2 = os.pipe()
stdin = open(r1)
Expand Down

0 comments on commit 150213d

Please sign in to comment.