mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 07:56:52 +08:00
refactor: own fanart cache cleanup
This commit is contained in:
+3
-2
@@ -13,8 +13,8 @@
|
||||
"runtime_to_db": [],
|
||||
"workflow_to_db": []
|
||||
},
|
||||
"edge_count": 6500,
|
||||
"edge_sha256": "6fa263f6f10aa51b4395aa41cf32a0c24254c2131d7882341936412400a10e4a",
|
||||
"edge_count": 6501,
|
||||
"edge_sha256": "3c01f9aeb7703065ff447672284ddcc88d6120ac3236b9659200df9b6f921bdd",
|
||||
"edges": [
|
||||
"app -> app.runtime",
|
||||
"app -> app.runtime.compat",
|
||||
@@ -4115,6 +4115,7 @@
|
||||
"app.modules.fanart -> app.runtime.cache",
|
||||
"app.modules.fanart -> app.runtime.log",
|
||||
"app.modules.fanart -> app.runtime.settings",
|
||||
"app.modules.fanart -> app.runtime.tasks",
|
||||
"app.modules.fanart -> app.schemas",
|
||||
"app.modules.fanart -> app.schemas.types",
|
||||
"app.modules.feishu -> app.application",
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""Fanart 模块缓存清理生命周期测试。"""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from app.modules.fanart import FanartModule
|
||||
from app.runtime.tasks import TaskRegistry
|
||||
|
||||
|
||||
def test_fanart_clear_cache_registers_async_cleanup_in_running_loop() -> None:
|
||||
"""同步清缓存入口应复用宿主 owner,并保持立即返回的兼容约定。"""
|
||||
|
||||
async def scenario() -> None:
|
||||
"""验证异步缓存清理完成前始终由宿主登记器持有。"""
|
||||
registry = TaskRegistry()
|
||||
release = asyncio.Event()
|
||||
|
||||
async def clear_async_cache() -> None:
|
||||
"""等待测试释放,以便观察登记中的清理任务。"""
|
||||
await release.wait()
|
||||
|
||||
sync_cache = SimpleNamespace(cache_clear=Mock())
|
||||
async_cache = SimpleNamespace(
|
||||
cache_clear=Mock(side_effect=lambda: clear_async_cache())
|
||||
)
|
||||
module = FanartModule()
|
||||
with (
|
||||
patch.object(
|
||||
FanartModule,
|
||||
"_FanartModule__request_fanart",
|
||||
sync_cache,
|
||||
),
|
||||
patch.object(
|
||||
FanartModule,
|
||||
"_FanartModule__async_request_fanart",
|
||||
async_cache,
|
||||
),
|
||||
patch("app.modules.fanart.get_task_registry", return_value=registry),
|
||||
):
|
||||
assert module.clear_cache() is None
|
||||
assert [record.owner for record in registry.records] == [
|
||||
"module.fanart.cache_clear"
|
||||
]
|
||||
|
||||
release.set()
|
||||
await registry.records[0].task
|
||||
await asyncio.sleep(0)
|
||||
|
||||
sync_cache.cache_clear.assert_called_once_with()
|
||||
async_cache.cache_clear.assert_called_once_with()
|
||||
assert registry.records == ()
|
||||
|
||||
asyncio.run(scenario())
|
||||
Reference in New Issue
Block a user