Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Apr 10, 2024
1 parent 068d0c9 commit 10f74d2
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion tests/utils/test_queue_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Queue tests."""
import asyncio
from unittest.mock import AsyncMock

import pytest
Expand All @@ -7,11 +8,11 @@
from custom_components.hacs.exceptions import HacsExecutionStillInProgress
from custom_components.hacs.utils.queue_manager import QueueManager

dummy_task = AsyncMock()


async def test_queue_manager(hacs: HacsBase, caplog: pytest.LogCaptureFixture) -> None:
"""Test the queue manager."""
dummy_task = AsyncMock()

queue_manager = QueueManager(hass=hacs.hass)
assert not queue_manager.running
Expand Down Expand Up @@ -46,3 +47,41 @@ async def test_queue_manager(hacs: HacsBase, caplog: pytest.LogCaptureFixture) -
assert queue_manager.pending_tasks == 0
assert queue_manager.queue == []
assert "The queue is empty" in caplog.text


async def test_queue_manager_grouping(
event_loop: asyncio.AbstractEventLoop, hacs: HacsBase, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the queue manager excutes queue items in order."""

dummy_task = AsyncMock()
slow_task_event_1 = asyncio.Event()
slow_task_event_2 = asyncio.Event()

async def fast_task() -> None:
"""A fast task."""

async def slow_task() -> None:
"""A slow task."""
await asyncio.sleep(0.1)
slow_task_event_1.set()
await slow_task_event_2.wait()

coro_group_1 = (fast_task(), fast_task(), slow_task())
coro_group_2 = (dummy_task(), dummy_task())

queue_manager = QueueManager(hass=hacs.hass)
queue_manager.add(coro_group_1)
queue_manager.add(coro_group_2)
assert queue_manager.pending_tasks == 5

execute_task = event_loop.create_task(queue_manager.execute())
await slow_task_event_1.wait()

dummy_task.assert_not_awaited()

slow_task_event_2.set()

await execute_task

assert dummy_task.await_count == 2

0 comments on commit 10f74d2

Please sign in to comment.