From ca7d403a7849278a3f5291097005dbaa02e4441e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 Nov 2023 17:23:17 -0600 Subject: [PATCH] Add coverage for trying to use the client while still handshaking (#749) --- tests/test_client.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_client.py b/tests/test_client.py index 7e20a4f..c6a41e3 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio import itertools import logging +from functools import partial from typing import Any from unittest.mock import AsyncMock, MagicMock, call, patch @@ -210,6 +211,29 @@ async def test_finish_connection_wraps_exceptions_as_unhandled_api_error() -> No await cli.finish_connection(False) +@pytest.mark.asyncio +async def test_request_while_handshaking(event_loop) -> None: + """Test trying a request while handshaking raises.""" + + class PatchableApiClient(APIClient): + pass + + cli = PatchableApiClient("host", 1234, None) + with patch.object( + event_loop, "sock_connect", side_effect=partial(asyncio.sleep, 1) + ), patch.object(cli, "finish_connection"): + connect_task = asyncio.create_task(cli.connect()) + + await asyncio.sleep(0) + with pytest.raises( + APIConnectionError, match="Authenticated connection not ready yet" + ): + await cli.device_info() + + connect_task.cancel() + await asyncio.sleep(0) + + @pytest.mark.asyncio async def test_connect_while_already_connected(auth_client: APIClient) -> None: """Test connecting while already connected raises."""