Coverage for tests/brokers/supervisor/test_supervisor.py: 97%
67 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import asyncio
2import logging
3from contextlib import suppress
4from typing import Any
5from unittest.mock import patch
7import pytest
9from faststream._internal.endpoint.subscriber.supervisor import (
10 TaskCallbackSupervisor,
11 _SupervisorCache,
12)
15@pytest.mark.asyncio()
16async def test_task_failing(subscriber_with_task_mixin: Any) -> None:
17 async def failing_task() -> Any:
18 raise ValueError
20 logging.disable(logging.CRITICAL + 1)
22 subscriber_with_task_mixin.add_task(failing_task)
23 task = subscriber_with_task_mixin.tasks[-1]
24 with suppress(ValueError):
25 await task
27 assert len(subscriber_with_task_mixin.tasks) > 1
28 # mypy does not resolve name-mangled attributes
29 supervisor: Any = TaskCallbackSupervisor
30 assert len(supervisor._TaskCallbackSupervisor__cache) == 1
33@pytest.mark.asyncio()
34async def test_task_failed_after_stop_is_not_restarted(
35 subscriber_with_task_mixin: Any,
36) -> None:
37 async def failing_task() -> Any:
38 # `stop()` forgets the tasks while this one is on its way down
39 subscriber_with_task_mixin.tasks.clear()
40 raise ValueError
42 subscriber_with_task_mixin.add_task(failing_task)
43 task = subscriber_with_task_mixin.tasks[-1]
44 with suppress(ValueError):
45 await task
47 assert subscriber_with_task_mixin.tasks == []
50@pytest.mark.asyncio()
51async def test_task_failing_without_restart(subscriber_with_task_mixin: Any) -> None:
52 async def failing_task() -> Any:
53 raise ValueError
55 subscriber_with_task_mixin.add_task(
56 failing_task,
57 restart_on_failure=False,
58 )
59 task = subscriber_with_task_mixin.tasks[-1]
60 with suppress(ValueError):
61 await task
62 await asyncio.sleep(0)
64 assert len(subscriber_with_task_mixin.tasks) == 1
67@pytest.mark.asyncio()
68async def test_task_successful(subscriber_with_task_mixin: Any) -> Any:
69 async def successful_task() -> Any:
70 return True
72 subscriber_with_task_mixin.add_task(successful_task)
73 task = subscriber_with_task_mixin.tasks[-1]
74 await task
75 assert len(subscriber_with_task_mixin.tasks) == 1
76 assert task.result()
79@pytest.mark.asyncio()
80@pytest.mark.slow()
81async def test_ignore_cancellation_error(subscriber_with_task_mixin: Any) -> Any:
82 async def cancelled_task() -> Any:
83 await asyncio.sleep(10)
84 return True
86 subscriber_with_task_mixin.add_task(cancelled_task)
87 task = subscriber_with_task_mixin.tasks[-1]
88 task.cancel()
89 with pytest.raises(asyncio.CancelledError):
90 await task
92 await asyncio.sleep(3)
93 assert len(subscriber_with_task_mixin.tasks) == 1
96def test_supervisor_cache() -> None:
97 with patch("time.time") as mocked_time:
98 mocked_time.return_value = 0
99 cache = _SupervisorCache()
100 cache.add(1)
101 cache.add(2)
102 mocked_time.return_value = cache.ttl - 1
103 assert 1 in cache
104 assert 2 in cache
105 assert 1 in cache
106 mocked_time.return_value = cache.ttl + 1
107 assert 1 not in cache
108 assert 2 not in cache