Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Sep 26, 2024
1 parent 47d9de3 commit b0ddf93
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/easynetwork/lowlevel/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def check_socket_family(family: int) -> None:
raise ValueError("Only these families are supported: AF_INET, AF_INET6")


def check_real_socket_state(socket: ISocket, error_msg: str = "{strerror}") -> None:
def check_real_socket_state(socket: ISocket, error_msg: str | None = None) -> None:
"""Verify socket saved error and raise OSError if there is one
There are some functions such as socket.send() which do not immediately fail and save the errno
Expand All @@ -180,7 +180,10 @@ def check_real_socket_state(socket: ISocket, error_msg: str = "{strerror}") -> N
errno = socket.getsockopt(_socket.SOL_SOCKET, _socket.SO_ERROR)
if errno != 0:
# The SO_ERROR is automatically reset to zero after getting the value
raise error_from_errno(errno, error_msg)
if error_msg:
raise error_from_errno(errno, error_msg)
else:
raise error_from_errno(errno)


class _SupportsSocketSendMSG(Protocol):
Expand Down
20 changes: 20 additions & 0 deletions tests/unit_test/test_tools/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@ def test____check_real_socket_state____socket_with_error(mock_tcp_socket: MagicM
mock_error_from_errno.assert_called_once_with(errno)


def test____check_real_socket_state____socket_with_error____custom_message(
mock_tcp_socket: MagicMock,
mocker: MockerFixture,
) -> None:
# Arrange
errno = 123456
exception = OSError(errno, "errno message")
mock_tcp_socket.getsockopt.return_value = errno
mock_error_from_errno = mocker.patch(f"{error_from_errno.__module__}.{error_from_errno.__qualname__}", return_value=exception)

# Act
with pytest.raises(OSError) as exc_info:
check_real_socket_state(mock_tcp_socket, error_msg="unrelated error: {strerror}")

# Assert
assert exc_info.value is exception
mock_tcp_socket.getsockopt.assert_called_once_with(SOL_SOCKET, SO_ERROR)
mock_error_from_errno.assert_called_once_with(errno, "unrelated error: {strerror}")


def test____check_real_socket_state____closed_socket(mock_tcp_socket: MagicMock, mocker: MockerFixture) -> None:
# Arrange
mock_tcp_socket.fileno.return_value = -1
Expand Down

0 comments on commit b0ddf93

Please sign in to comment.