mirror of
https://github.com/FuQuan233/nonebot-plugin-llmchat.git
synced 2026-08-13 10:09:27 +00:00
♻️ 重构MCPClient会话管理,增加会话所有权控制的测试用例
This commit is contained in:
parent
f091497613
commit
f2014723c5
2 changed files with 104 additions and 21 deletions
44
tests/test_mcpclient.py
Normal file
44
tests/test_mcpclient.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from types import SimpleNamespace
|
||||
from unittest import IsolatedAsyncioTestCase
|
||||
|
||||
from anyio import CancelScope
|
||||
|
||||
from nonebot_plugin_llmchat.mcpclient import MCPClient
|
||||
|
||||
|
||||
class TestMCPClientSessionOwnership(IsolatedAsyncioTestCase):
|
||||
async def test_session_context_is_closed_by_its_owner_task(self):
|
||||
entered_by = None
|
||||
exited_by = None
|
||||
|
||||
@asynccontextmanager
|
||||
async def task_bound_context():
|
||||
nonlocal entered_by, exited_by
|
||||
entered_by = asyncio.current_task()
|
||||
with CancelScope():
|
||||
try:
|
||||
yield SimpleNamespace()
|
||||
finally:
|
||||
exited_by = asyncio.current_task()
|
||||
|
||||
client = object.__new__(MCPClient)
|
||||
client.operation_timeout = 1
|
||||
|
||||
async def initialize(server_name, session_stack):
|
||||
session = await session_stack.enter_async_context(task_bound_context())
|
||||
return session, session_stack
|
||||
|
||||
client._initialize_server_session = initialize
|
||||
|
||||
session, handle = await client._create_server_session("test")
|
||||
client.sessions = {"test": session}
|
||||
client._session_handles = {"test": handle}
|
||||
client._session_last_used = {"test": 0.0}
|
||||
|
||||
await client._close_server_session("test")
|
||||
|
||||
assert entered_by is handle.owner_task
|
||||
assert exited_by is handle.owner_task
|
||||
assert handle.owner_task.done()
|
||||
Loading…
Add table
Add a link
Reference in a new issue