Skip to content

Commit

Permalink
Clean up event_loop fixture (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHjelmare authored Jan 3, 2024
1 parent 376773f commit d1a6eea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
14 changes: 9 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
logging.basicConfig(level=logging.DEBUG)


@pytest.fixture(name="loop")
async def loop_fixture():
"""Return the event loop."""
return asyncio.get_running_loop()


@pytest.fixture
async def aioclient_mock(event_loop):
async def aioclient_mock(loop):
"""Fixture to mock aioclient calls."""
loop = event_loop
with mock_aiohttp_client(loop) as mock_session:
yield mock_session


@pytest.fixture
async def cloud_mock(event_loop, aioclient_mock):
async def cloud_mock(loop, aioclient_mock):
"""Yield a simple cloud mock."""
loop = event_loop
cloud = MagicMock(name="Mock Cloud", is_logged_in=True)
cloud.run_task = loop.create_task
cloud.run_task = asyncio.create_task

def _executor(call, *args):
"""Run executor."""
Expand Down
14 changes: 6 additions & 8 deletions tests/test_google_report_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from .common import MockClient


async def create_grs(loop, ws_server, server_msg_handler) -> GoogleReportState:
async def create_grs(ws_server, server_msg_handler) -> GoogleReportState:
"""Create a grs instance."""
client = await ws_server(server_msg_handler)
mock_cloud = Mock(
run_task=loop.create_task,
run_task=asyncio.create_task,
subscription_expired=False,
remotestate_server="mock-report-state-url.com",
auth=Mock(async_check_token=AsyncMock()),
Expand All @@ -30,9 +30,8 @@ async def test_ws_server_url():
)


async def test_send_messages(event_loop, ws_server):
async def test_send_messages(ws_server):
"""Test that we connect if we are not connected."""
loop = event_loop
server_msgs = []

async def handle_server_msg(msg):
Expand All @@ -51,7 +50,7 @@ async def handle_server_msg(msg):
"message": "mock-message",
}

grs = await create_grs(loop, ws_server, handle_server_msg)
grs = await create_grs(ws_server, handle_server_msg)
assert grs.state == iot_base.STATE_DISCONNECTED

# Test we can handle two simultaneous messages while disconnected
Expand All @@ -76,9 +75,8 @@ async def handle_server_msg(msg):
assert grs._message_sender_task is None


async def test_max_queue_message(event_loop, ws_server):
async def test_max_queue_message(ws_server):
"""Test that we connect if we are not connected."""
loop = event_loop
server_msgs = []

async def handle_server_msg(msg):
Expand All @@ -87,7 +85,7 @@ async def handle_server_msg(msg):
server_msgs.append(incoming["payload"])
return {"msgid": incoming["msgid"], "payload": incoming["payload"]["hello"]}

grs = await create_grs(loop, ws_server, handle_server_msg)
grs = await create_grs(ws_server, handle_server_msg)

# Test we can handle sending more messages than queue fits
with patch.object(grs, "_async_message_sender"):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,16 @@ async def test_send_message_no_answer(cloud_mock_iot):
assert msg["payload"] == {"msg": "yo"}


async def test_send_message_answer(event_loop, cloud_mock_iot):
async def test_send_message_answer(cloud_mock_iot):
"""Test sending a message that expects an answer."""
loop = event_loop
cloud_iot = iot.CloudIoT(cloud_mock_iot)
cloud_iot.state = iot_base.STATE_CONNECTED
cloud_iot.client = MagicMock(send_json=AsyncMock())

uuid = 5

with patch("hass_nabucasa.iot.uuid.uuid4", return_value=MagicMock(hex=uuid)):
send_task = loop.create_task(
send_task = asyncio.create_task(
cloud_iot.async_send_message("webhook", {"msg": "yo"})
)
await asyncio.sleep(0)
Expand Down
14 changes: 8 additions & 6 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,9 @@ async def test_get_certificate_details(


async def test_certificate_task_no_backend(
event_loop, auth_cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock
auth_cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock
):
"""Initialize backend."""
loop = event_loop
valid = utcnow() + timedelta(days=1)
auth_cloud_mock.servicehandlers_server = "test.local"
remote = RemoteUI(auth_cloud_mock)
Expand Down Expand Up @@ -482,7 +481,9 @@ async def test_certificate_task_no_backend(
with patch("hass_nabucasa.utils.next_midnight", return_value=0), patch(
"random.randint", return_value=0
):
acme_task = remote._acme_task = loop.create_task(remote._certificate_handler())
acme_task = remote._acme_task = asyncio.create_task(
remote._certificate_handler()
)
await asyncio.sleep(0.1)
assert acme_mock.call_issue
assert snitun_mock.call_start
Expand All @@ -494,10 +495,9 @@ async def test_certificate_task_no_backend(


async def test_certificate_task_renew_cert(
event_loop, auth_cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock
auth_cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock
):
"""Initialize backend."""
loop = event_loop
valid = utcnow() + timedelta(days=1)
auth_cloud_mock.servicehandlers_server = "test.local"
remote = RemoteUI(auth_cloud_mock)
Expand Down Expand Up @@ -525,7 +525,9 @@ async def test_certificate_task_renew_cert(
with patch("hass_nabucasa.utils.next_midnight", return_value=0), patch(
"random.randint", return_value=0
):
acme_task = remote._acme_task = loop.create_task(remote._certificate_handler())
acme_task = remote._acme_task = asyncio.create_task(
remote._certificate_handler()
)

await remote.load_backend()
await asyncio.sleep(0.1)
Expand Down

0 comments on commit d1a6eea

Please sign in to comment.