Add test for attempting to reuse an APIConnection object raises (#703)

This commit is contained in:
J. Nick Koston 2023-11-25 08:18:08 -06:00 committed by GitHub
parent 554190228e
commit b67e19bd94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -512,7 +512,7 @@ class APIConnection:
does not initialize the frame helper or send the hello message.
"""
if self.connection_state is not ConnectionState.INITIALIZED:
raise ValueError(
raise RuntimeError(
"Connection can only be used once, connection is not in init state"
)

View File

@ -769,3 +769,18 @@ async def test_bad_protobuf_message_drops_connection(
mock_data_received(protocol, message_with_bad_protobuf_data)
assert "Invalid protobuf message: type=TextSensorStateResponse" in caplog.text
assert connection.is_connected is False
@pytest.mark.asyncio
async def test_connection_cannot_be_reused(
plaintext_connect_task_with_login: tuple[
APIConnection, asyncio.Transport, APIPlaintextFrameHelper, asyncio.Task
],
) -> None:
"""Test that we raise when trying to connect when already connected."""
conn, transport, protocol, connect_task = plaintext_connect_task_with_login
send_plaintext_hello(protocol)
send_plaintext_connect_response(protocol, False)
await connect_task
with pytest.raises(RuntimeError):
await conn.start_connection()