Skip to content

Commit

Permalink
feat: improve test coverage (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 9, 2023
1 parent 409a309 commit afcfe5a
Showing 1 changed file with 126 additions and 2 deletions.
128 changes: 126 additions & 2 deletions tests/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ def _socket(*args, **kw):
raise OSError(errors[idx])

m_socket.socket = _socket # type: ignore
addr_info = [(2, 1, 6, "", ("107.6.106.82", 80))]
addr_info = [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("107.6.106.82", 80),
)
]
with pytest.raises(OSError, match=errors[0]):
await create_connection(addr_info)

Expand All @@ -71,7 +79,123 @@ def _socket(*args, **kw):
return mock_socket

m_socket.socket = _socket # type: ignore
addr_info = [(2, 1, 6, "", ("107.6.106.82", 80))]
addr_info = [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("107.6.106.82", 80),
)
]
loop = asyncio.get_running_loop()
with mock.patch.object(loop, "sock_connect", return_value=None):
assert await create_connection(addr_info) == mock_socket


@pytest.mark.asyncio
@patch_socket
async def test_create_connection_multiple_addr_success_second_one(
m_socket: ModuleType,
) -> None:
mock_socket = mock.MagicMock(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
fileno=mock.MagicMock(return_value=1),
)
idx = -1
errors = ["err1", "err2"]

def _socket(*args, **kw):
nonlocal idx, errors
idx += 1
if idx == 1:
raise OSError(errors[idx])
return mock_socket

m_socket.socket = _socket # type: ignore
addr_info = [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("107.6.106.82", 80),
)
]
loop = asyncio.get_running_loop()
with mock.patch.object(loop, "sock_connect", return_value=None):
assert await create_connection(addr_info) == mock_socket


@pytest.mark.asyncio
@patch_socket
async def test_create_connection_multiple_addr_success_second_one_happy_eyeballs(
m_socket: ModuleType,
) -> None:
mock_socket = mock.MagicMock(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
fileno=mock.MagicMock(return_value=1),
)
idx = -1
errors = ["err1", "err2"]

def _socket(*args, **kw):
nonlocal idx, errors
idx += 1
if idx == 1:
raise OSError(errors[idx])
return mock_socket

m_socket.socket = _socket # type: ignore
addr_info = [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("107.6.106.82", 80),
)
]
loop = asyncio.get_running_loop()
with mock.patch.object(loop, "sock_connect", return_value=None):
assert (
await create_connection(addr_info, happy_eyeballs_delay=0.3) == mock_socket
)


@pytest.mark.asyncio
@patch_socket
async def test_create_connection_multiple_addr_all_fail_happy_eyeballs(
m_socket: ModuleType,
) -> None:
mock.MagicMock(
family=socket.AF_INET,
type=socket.SOCK_STREAM,
proto=socket.IPPROTO_TCP,
fileno=mock.MagicMock(return_value=1),
)
idx = -1
errors = ["err1", "err2"]

def _socket(*args, **kw):
nonlocal idx, errors
idx += 1
raise OSError(errors[idx])

m_socket.socket = _socket # type: ignore
addr_info = [
(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP,
"",
("107.6.106.82", 80),
)
]
asyncio.get_running_loop()
with pytest.raises(OSError, match=errors[0]):
await create_connection(addr_info, happy_eyeballs_delay=0.3)

0 comments on commit afcfe5a

Please sign in to comment.