|
@@ -1,8 +1,97 @@
|
|
|
"""Tests for helper functions."""
|
|
"""Tests for helper functions."""
|
|
|
|
|
|
|
|
|
|
+import asyncio
|
|
|
|
|
+import gc
|
|
|
|
|
+import logging
|
|
|
|
|
+
|
|
|
import pytest
|
|
import pytest
|
|
|
|
|
|
|
|
-from switchbot.helpers import parse_power_data
|
|
|
|
|
|
|
+from switchbot.helpers import (
|
|
|
|
|
+ _BACKGROUND_TASKS,
|
|
|
|
|
+ create_background_task,
|
|
|
|
|
+ parse_power_data,
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+async def _drain_background_task(task: asyncio.Task) -> list[dict]:
|
|
|
|
|
+ """Let a background task finish and collect unhandled-exception reports."""
|
|
|
|
|
+ loop = asyncio.get_running_loop()
|
|
|
|
|
+ reports: list[dict] = []
|
|
|
|
|
+ previous_handler = loop.get_exception_handler()
|
|
|
|
|
+ loop.set_exception_handler(lambda _loop, context: reports.append(context))
|
|
|
|
|
+ try:
|
|
|
|
|
+ await asyncio.wait([task])
|
|
|
|
|
+ await asyncio.sleep(0)
|
|
|
|
|
+ del task
|
|
|
|
|
+ gc.collect()
|
|
|
|
|
+ await asyncio.sleep(0)
|
|
|
|
|
+ finally:
|
|
|
|
|
+ loop.set_exception_handler(previous_handler)
|
|
|
|
|
+ return reports
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+async def _update() -> None:
|
|
|
|
|
+ raise TimeoutError
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+async def _parse() -> None:
|
|
|
|
|
+ raise IndexError("index out of range")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+@pytest.mark.parametrize(
|
|
|
|
|
+ ("coro_func", "level", "exc_name"),
|
|
|
|
|
+ [
|
|
|
|
|
+ pytest.param(_update, logging.DEBUG, "TimeoutError", id="transient"),
|
|
|
|
|
+ pytest.param(_parse, logging.ERROR, "IndexError", id="unexpected"),
|
|
|
|
|
+ ],
|
|
|
|
|
+)
|
|
|
|
|
+async def test_background_task_failure_is_logged(
|
|
|
|
|
+ caplog: pytest.LogCaptureFixture,
|
|
|
|
|
+ coro_func,
|
|
|
|
|
+ level: int,
|
|
|
|
|
+ exc_name: str,
|
|
|
|
|
+) -> None:
|
|
|
|
|
+ """Test a failed background task is logged instead of left unretrieved."""
|
|
|
|
|
+ caplog.set_level(logging.DEBUG, logger="switchbot.helpers")
|
|
|
|
|
+ pending = set(_BACKGROUND_TASKS)
|
|
|
|
|
+
|
|
|
|
|
+ reports = await _drain_background_task(create_background_task(coro_func()))
|
|
|
|
|
+
|
|
|
|
|
+ assert reports == []
|
|
|
|
|
+ assert pending == _BACKGROUND_TASKS
|
|
|
|
|
+ [record] = caplog.records
|
|
|
|
|
+ assert record.levelno == level
|
|
|
|
|
+ assert record.getMessage() == f"Background task {coro_func.__name__} failed"
|
|
|
|
|
+ assert record.exc_info[0].__name__ == exc_name
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_background_task_cancelled() -> None:
|
|
|
|
|
+ """Test a cancelled background task is dropped without errors."""
|
|
|
|
|
+ task = create_background_task(asyncio.sleep(10))
|
|
|
|
|
+ await asyncio.sleep(0)
|
|
|
|
|
+ task.cancel()
|
|
|
|
|
+
|
|
|
|
|
+ reports = await _drain_background_task(task)
|
|
|
|
|
+
|
|
|
|
|
+ assert reports == []
|
|
|
|
|
+ assert task not in _BACKGROUND_TASKS
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_background_task_success() -> None:
|
|
|
|
|
+ """Test a successful background task is dropped from the set."""
|
|
|
|
|
+
|
|
|
|
|
+ async def _ok() -> int:
|
|
|
|
|
+ return 1
|
|
|
|
|
+
|
|
|
|
|
+ pending = set(_BACKGROUND_TASKS)
|
|
|
|
|
+
|
|
|
|
|
+ reports = await _drain_background_task(create_background_task(_ok()))
|
|
|
|
|
+
|
|
|
|
|
+ assert reports == []
|
|
|
|
|
+ assert pending == _BACKGROUND_TASKS
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_power_data_basic():
|
|
def test_parse_power_data_basic():
|