Skip to content

Commit

Permalink
[FIX] Fixed flaky tests on uvloop
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Dec 7, 2023
1 parent 4202dc7 commit d1c64a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import pytest
import pytest_asyncio

from .....tools import is_uvloop_event_loop


async def readline(loop: asyncio.AbstractEventLoop, sock: Socket) -> bytes:
buf: list[bytes] = []
Expand Down Expand Up @@ -72,42 +74,50 @@ async def test____send_packet____default(

async def test____send_packet____connection_error____fresh_connection_closed_by_server(
self,
event_loop: asyncio.AbstractEventLoop,
client: AsyncTCPNetworkClient[str, str],
server: Socket,
) -> None:
if is_uvloop_event_loop(event_loop):
pytest.skip("It is not mandadtory for uvloop to raise ConnectionAbortedError")
server.close()
with pytest.raises(ConnectionAbortedError):
for _ in range(3): # Windows and macOS catch the issue after several send()
await client.send_packet("ABCDEF")
await asyncio.sleep(0)
await asyncio.sleep(0.01)

async def test____send_packet____connection_error____after_previous_successful_try(
self,
event_loop: asyncio.AbstractEventLoop,
client: AsyncTCPNetworkClient[str, str],
server: Socket,
) -> None:
if is_uvloop_event_loop(event_loop):
pytest.skip("It is not mandadtory for uvloop to raise ConnectionAbortedError")

await client.send_packet("ABCDEF")
assert await readline(event_loop, server) == b"ABCDEF\n"
server.close()
with pytest.raises(ConnectionAbortedError):
for _ in range(3): # Windows and macOS catch the issue after several send()
await client.send_packet("ABCDEF")
await asyncio.sleep(0)
await asyncio.sleep(0.01)

async def test____send_packet____connection_error____partial_read_then_close(
self,
event_loop: asyncio.AbstractEventLoop,
client: AsyncTCPNetworkClient[str, str],
server: Socket,
) -> None:
if is_uvloop_event_loop(event_loop):
pytest.skip("It is not mandadtory for uvloop to raise ConnectionAbortedError")
await client.send_packet("ABC")
assert await event_loop.sock_recv(server, 1) == b"A"
server.close()
with pytest.raises(ConnectionAbortedError):
for _ in range(3): # Windows and macOS catch the issue after several send()
await client.send_packet("DEF")
await asyncio.sleep(0)
await asyncio.sleep(0.01)

async def test____send_packet____closed_client(self, client: AsyncTCPNetworkClient[str, str]) -> None:
await client.aclose()
Expand Down
9 changes: 9 additions & 0 deletions tests/tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import importlib
import sys
import time
from collections.abc import Generator
Expand Down Expand Up @@ -70,6 +71,14 @@ def is_proactor_event_loop(event_loop: asyncio.AbstractEventLoop) -> bool:
return isinstance(event_loop, ProactorEventLoop)


def is_uvloop_event_loop(event_loop: asyncio.AbstractEventLoop) -> bool:
try:
uvloop = importlib.import_module("uvloop")
except ModuleNotFoundError:
return False
return isinstance(event_loop, uvloop.Loop)


_TooShortBufferBehavior: TypeAlias = Literal["error", "fill_at_most", "xfail"]


Expand Down

0 comments on commit d1c64a5

Please sign in to comment.