Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ classifiers = [

requires-python = '>=3.12,<4.0'
dependencies = [
"saic-ismart-client-ng (>=0.9.3,<0.10.0)",
"saic-ismart-client-ng (>=0.10.0,<0.11.0)",
'httpx (>=0.28.1,<0.29.0)',
'inflection (>=0.5.1,<0.6.0)',
'apscheduler (>=3.11.0,<4.0.0)',
Expand Down
11 changes: 10 additions & 1 deletion src/mqtt_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,16 @@ async def run(self) -> None:
self.__scheduler.start()

LOG.info("Entering main loop")
await self.__run_until_all_tasks_done()
try:
await self.__run_until_all_tasks_done()
finally:
await self.__close_saic_api()

async def __close_saic_api(self) -> None:
try:
await self.saic_api.close()
except Exception:
LOG.warning("Failed to close the SAIC API client", exc_info=True)

async def __fetch_user_timezone(self) -> ZoneInfo | None:
try:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_gateway_shutdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from __future__ import annotations

import unittest
from unittest.mock import AsyncMock, patch

import pytest

from configuration import Configuration
from mqtt_gateway import MqttGateway

from .mocks import MessageCapturingConsolePublisher


def _make_gateway() -> MqttGateway:
config = Configuration()
config.saic_user = "user@example.com"
config.saic_password = "secret" # noqa: S105
with patch(
"mqtt_gateway.MqttGateway._MqttGateway__select_publisher",
return_value=MessageCapturingConsolePublisher(config),
):
return MqttGateway(config)


class TestGatewayShutdown(unittest.IsolatedAsyncioTestCase):
async def _run_with_main_loop(
self, gateway: MqttGateway, main_loop: AsyncMock
) -> None:
with (
patch.object(gateway, "_MqttGateway__do_initial_login", AsyncMock()),
patch.object(gateway, "_MqttGateway__run_until_all_tasks_done", main_loop),
patch.object(gateway, "_MqttGateway__scheduler", create=True),
):
await gateway.run()

async def test_api_client_closed_when_main_loop_exits(self) -> None:
gateway = _make_gateway()
gateway.saic_api.close = AsyncMock() # type: ignore[method-assign]

with pytest.raises(SystemExit):
await self._run_with_main_loop(
gateway, AsyncMock(side_effect=SystemExit(-1))
)

gateway.saic_api.close.assert_awaited_once()

async def test_close_failure_is_logged_not_raised(self) -> None:
gateway = _make_gateway()
gateway.saic_api.close = AsyncMock(side_effect=RuntimeError("boom")) # type: ignore[method-assign]

with self.assertLogs("mqtt_gateway", level="WARNING"):
await self._run_with_main_loop(gateway, AsyncMock())

gateway.saic_api.close.assert_awaited_once()
Loading