cleanup more tests

This commit is contained in:
J. Nick Koston 2023-12-09 18:00:35 -10:00
parent 8743b7e49c
commit f0df2fff21
No known key found for this signature in database
2 changed files with 23 additions and 33 deletions

View File

@ -30,7 +30,11 @@ from .common import (
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_log_runner(event_loop: asyncio.AbstractEventLoop, conn: APIConnection): async def test_log_runner(
event_loop: asyncio.AbstractEventLoop,
conn: APIConnection,
aiohappyeyeballs_start_connection,
):
"""Test the log runner logic.""" """Test the log runner logic."""
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
protocol: APIPlaintextFrameHelper | None = None protocol: APIPlaintextFrameHelper | None = None
@ -69,13 +73,9 @@ async def test_log_runner(event_loop: asyncio.AbstractEventLoop, conn: APIConnec
await original_subscribe_logs(*args, **kwargs) await original_subscribe_logs(*args, **kwargs)
subscribed.set() subscribed.set()
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, "create_connection", side_effect=_create_mock_transport_protocol loop, "create_connection", side_effect=_create_mock_transport_protocol
), patch.object( ), patch.object(cli, "subscribe_logs", _wait_subscribe_cli):
cli, "subscribe_logs", _wait_subscribe_cli
):
stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf)
await connected.wait() await connected.wait()
protocol = cli._connection._frame_helper protocol = cli._connection._frame_helper
@ -100,6 +100,7 @@ async def test_log_runner_reconnects_on_disconnect(
event_loop: asyncio.AbstractEventLoop, event_loop: asyncio.AbstractEventLoop,
conn: APIConnection, conn: APIConnection,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
aiohappyeyeballs_start_connection,
) -> None: ) -> None:
"""Test the log runner reconnects on disconnect.""" """Test the log runner reconnects on disconnect."""
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -139,13 +140,9 @@ async def test_log_runner_reconnects_on_disconnect(
await original_subscribe_logs(*args, **kwargs) await original_subscribe_logs(*args, **kwargs)
subscribed.set() subscribed.set()
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, "create_connection", side_effect=_create_mock_transport_protocol loop, "create_connection", side_effect=_create_mock_transport_protocol
), patch.object( ), patch.object(cli, "subscribe_logs", _wait_subscribe_cli):
cli, "subscribe_logs", _wait_subscribe_cli
):
stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf)
await connected.wait() await connected.wait()
protocol = cli._connection._frame_helper protocol = cli._connection._frame_helper
@ -181,6 +178,7 @@ async def test_log_runner_reconnects_on_subscribe_failure(
event_loop: asyncio.AbstractEventLoop, event_loop: asyncio.AbstractEventLoop,
conn: APIConnection, conn: APIConnection,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
aiohappyeyeballs_start_connection,
) -> None: ) -> None:
"""Test the log runner reconnects on subscribe failure.""" """Test the log runner reconnects on subscribe failure."""
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -222,9 +220,7 @@ async def test_log_runner_reconnects_on_subscribe_failure(
with patch.object( with patch.object(
cli, "disconnect", partial(cli.disconnect, force=True) cli, "disconnect", partial(cli.disconnect, force=True)
), patch.object(cli, "subscribe_logs", _wait_and_fail_subscribe_cli): ), patch.object(cli, "subscribe_logs", _wait_and_fail_subscribe_cli):
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, "create_connection", side_effect=_create_mock_transport_protocol loop, "create_connection", side_effect=_create_mock_transport_protocol
): ):
stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf) stop = await async_run(cli, on_log, aio_zeroconf_instance=async_zeroconf)
@ -237,13 +233,9 @@ async def test_log_runner_reconnects_on_subscribe_failure(
assert cli._connection is None assert cli._connection is None
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, "create_connection", side_effect=_create_mock_transport_protocol loop, "create_connection", side_effect=_create_mock_transport_protocol
), patch.object( ), patch.object(cli, "subscribe_logs"):
cli, "subscribe_logs"
):
connected.clear() connected.clear()
await asyncio.sleep(0) await asyncio.sleep(0)
async_fire_time_changed( async_fire_time_changed(

View File

@ -672,7 +672,9 @@ async def test_reconnect_logic_stop_callback_waits_for_handshake(
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventLoop): async def test_handling_unexpected_disconnect(
event_loop: asyncio.AbstractEventLoop, aiohappyeyeballs_start_connection
):
"""Test the disconnect callback fires with expected_disconnect=False.""" """Test the disconnect callback fires with expected_disconnect=False."""
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
protocol: APIPlaintextFrameHelper | None = None protocol: APIPlaintextFrameHelper | None = None
@ -710,9 +712,7 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL
name="fake", name="fake",
) )
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, loop,
"create_connection", "create_connection",
side_effect=partial(_create_mock_transport_protocol, transport, connected), side_effect=partial(_create_mock_transport_protocol, transport, connected),
@ -728,9 +728,7 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL
assert cli._connection.is_connected is True assert cli._connection.is_connected is True
await asyncio.sleep(0) await asyncio.sleep(0)
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, loop,
"create_connection", "create_connection",
side_effect=partial(_create_mock_transport_protocol, transport, connected), side_effect=partial(_create_mock_transport_protocol, transport, connected),
@ -750,7 +748,9 @@ async def test_handling_unexpected_disconnect(event_loop: asyncio.AbstractEventL
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_backoff_on_encryption_error( async def test_backoff_on_encryption_error(
event_loop: asyncio.AbstractEventLoop, caplog: pytest.LogCaptureFixture event_loop: asyncio.AbstractEventLoop,
caplog: pytest.LogCaptureFixture,
aiohappyeyeballs_start_connection,
) -> None: ) -> None:
"""Test we backoff on encryption error.""" """Test we backoff on encryption error."""
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -789,9 +789,7 @@ async def test_backoff_on_encryption_error(
name="fake", name="fake",
) )
with patch( with patch.object(
"aioesphomeapi.connection.aiohappyeyeballs.start_connection"
), patch.object(
loop, loop,
"create_connection", "create_connection",
side_effect=partial(_create_mock_transport_protocol, transport, connected), side_effect=partial(_create_mock_transport_protocol, transport, connected),