From 9bee1d97fe4c0f8b626679bf98ce8be343404511 Mon Sep 17 00:00:00 2001 From: AishwaryaKalloli <30429206+AishwaryaKalloli@users.noreply.github.com> Date: Mon, 12 May 2025 23:21:43 +0530 Subject: [PATCH] Test fixes for v1.7.x (#684) --- tests/issues/test_188_concurrency.py | 26 ++++++++++++++++++++------ tests/test_examples.py | 10 ++++++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/tests/issues/test_188_concurrency.py b/tests/issues/test_188_concurrency.py index 2aa6c49cb..10c462aaa 100644 --- a/tests/issues/test_188_concurrency.py +++ b/tests/issues/test_188_concurrency.py @@ -14,29 +14,43 @@ @pytest.mark.anyio async def test_messages_are_executed_concurrently(): server = FastMCP("test") + call_timestamps = [] @server.tool("sleep") async def sleep_tool(): + start_time = anyio.current_time() + call_timestamps.append(("tool_start", start_time)) await anyio.sleep(_sleep_time_seconds) + end_time = anyio.current_time() + call_timestamps.append(("tool_end", end_time)) return "done" @server.resource(_resource_name) async def slow_resource(): + start_time = anyio.current_time() + call_timestamps.append(("resource_start", start_time)) await anyio.sleep(_sleep_time_seconds) + end_time = anyio.current_time() + call_timestamps.append(("resource_end", end_time)) return "slow" async with create_session(server._mcp_server) as client_session: - start_time = anyio.current_time() async with anyio.create_task_group() as tg: for _ in range(10): tg.start_soon(client_session.call_tool, "sleep") tg.start_soon(client_session.read_resource, AnyUrl(_resource_name)) - end_time = anyio.current_time() - - duration = end_time - start_time - assert duration < 3 * _sleep_time_seconds - print(duration) + # Verify concurrent execution by checking for overlapping calls + active_calls = 0 + max_concurrent_calls = 0 + for call_type, timestamp in sorted(call_timestamps, key=lambda x: x[1]): + if "start" in call_type: + active_calls += 1 + max_concurrent_calls = max(max_concurrent_calls, active_calls) + else: + active_calls -= 1 + print(f"\nMax concurrent calls: {max_concurrent_calls}") + assert max_concurrent_calls > 1, "No concurrent calls detected" def main(): diff --git a/tests/test_examples.py b/tests/test_examples.py index c5e8ec9d7..d145b6bae 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -69,8 +69,14 @@ async def test_desktop(monkeypatch): content = result.contents[0] assert isinstance(content, TextResourceContents) assert isinstance(content.text, str) - assert "/fake/path/file1.txt" in content.text - assert "/fake/path/file2.txt" in content.text + assert any( + path in content.text + for path in ["/fake/path/file1.txt", "\\\\fake\\\\path\\\\file1.txt"] + ) + assert any( + path in content.text + for path in ["/fake/path/file2.txt", "\\\\fake\\\\path\\\\file2.txt"] + ) @pytest.mark.parametrize("example", find_examples("README.md"), ids=str)