Skip to content

Commit e96264a

Browse files
authored
fix: resolve warnings when running tests (#144)
- Instead of raising SystemExit which causes a RuntimeError, mock out SystemExit to a new exception - Make sure the event loop is closed in tests fixes #97
1 parent ece56a5 commit e96264a

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/aiohappyeyeballs/_staggered.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
_T = TypeVar("_T")
1818

19+
RE_RAISE_EXCEPTIONS = (SystemExit, KeyboardInterrupt)
20+
1921

2022
def _set_result(wait_next: "asyncio.Future[None]") -> None:
2123
"""Set the result of a future if it is not already done."""
@@ -125,7 +127,7 @@ async def run_one_coro(
125127
"""
126128
try:
127129
result = await coro_fn()
128-
except (SystemExit, KeyboardInterrupt):
130+
except RE_RAISE_EXCEPTIONS:
129131
raise
130132
except BaseException as e:
131133
exceptions[this_index] = e

tests/test_impl.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from aiohappyeyeballs import start_connection
9+
from aiohappyeyeballs import _staggered, start_connection
1010

1111

1212
def mock_socket_module():
@@ -1652,11 +1652,14 @@ async def _sock_connect(
16521652

16531653
@patch_socket
16541654
@pytest.mark.asyncio
1655-
@pytest.mark.xfail(reason="raises RuntimeError: coroutine ignored GeneratorExit")
16561655
async def test_handling_system_exit(
16571656
m_socket: ModuleType,
16581657
) -> None:
16591658
"""Test handling SystemExit."""
1659+
1660+
class MockSystemExit(BaseException):
1661+
"""Mock SystemExit."""
1662+
16601663
mock_socket = mock.MagicMock(
16611664
family=socket.AF_INET,
16621665
type=socket.SOCK_STREAM,
@@ -1674,7 +1677,7 @@ async def _sock_connect(
16741677
sock: socket.socket, address: Tuple[str, int, int, int]
16751678
) -> None:
16761679
create_calls.append(address)
1677-
raise SystemExit
1680+
raise MockSystemExit
16781681

16791682
m_socket.socket = _socket # type: ignore
16801683
ipv6_addr_info = (
@@ -1716,9 +1719,9 @@ async def _sock_connect(
17161719
),
17171720
]
17181721
loop = asyncio.get_running_loop()
1719-
with pytest.raises(SystemExit), mock.patch.object(
1722+
with pytest.raises(MockSystemExit), mock.patch.object(
17201723
loop, "sock_connect", _sock_connect
1721-
):
1724+
), mock.patch.object(_staggered, "RE_RAISE_EXCEPTIONS", (MockSystemExit,)):
17221725
await start_connection(
17231726
addr_info,
17241727
happy_eyeballs_delay=0.3,

tests/test_staggered.py

+1
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@ async def coro(idx):
8484
assert excs == [None, None, None, None]
8585

8686
loop.run_until_complete(run())
87+
loop.close()

0 commit comments

Comments
 (0)