|
14 | 14 | @pytest.mark.anyio
|
15 | 15 | async def test_messages_are_executed_concurrently():
|
16 | 16 | server = FastMCP("test")
|
| 17 | + call_timestamps = [] |
17 | 18 |
|
18 | 19 | @server.tool("sleep")
|
19 | 20 | async def sleep_tool():
|
| 21 | + start_time = anyio.current_time() |
| 22 | + call_timestamps.append(("tool_start", start_time)) |
20 | 23 | await anyio.sleep(_sleep_time_seconds)
|
| 24 | + end_time = anyio.current_time() |
| 25 | + call_timestamps.append(("tool_end", end_time)) |
21 | 26 | return "done"
|
22 | 27 |
|
23 | 28 | @server.resource(_resource_name)
|
24 | 29 | async def slow_resource():
|
| 30 | + start_time = anyio.current_time() |
| 31 | + call_timestamps.append(("resource_start", start_time)) |
25 | 32 | await anyio.sleep(_sleep_time_seconds)
|
| 33 | + end_time = anyio.current_time() |
| 34 | + call_timestamps.append(("resource_end", end_time)) |
26 | 35 | return "slow"
|
27 | 36 |
|
28 | 37 | async with create_session(server._mcp_server) as client_session:
|
29 |
| - start_time = anyio.current_time() |
30 | 38 | async with anyio.create_task_group() as tg:
|
31 | 39 | for _ in range(10):
|
32 | 40 | tg.start_soon(client_session.call_tool, "sleep")
|
33 | 41 | tg.start_soon(client_session.read_resource, AnyUrl(_resource_name))
|
34 | 42 |
|
35 |
| - end_time = anyio.current_time() |
36 |
| - |
37 |
| - duration = end_time - start_time |
38 |
| - assert duration < 3 * _sleep_time_seconds |
39 |
| - print(duration) |
| 43 | + # Verify concurrent execution by checking for overlapping calls |
| 44 | + active_calls = 0 |
| 45 | + max_concurrent_calls = 0 |
| 46 | + for call_type, timestamp in sorted(call_timestamps, key=lambda x: x[1]): |
| 47 | + if "start" in call_type: |
| 48 | + active_calls += 1 |
| 49 | + max_concurrent_calls = max(max_concurrent_calls, active_calls) |
| 50 | + else: |
| 51 | + active_calls -= 1 |
| 52 | + print(f"\nMax concurrent calls: {max_concurrent_calls}") |
| 53 | + assert max_concurrent_calls > 1, "No concurrent calls detected" |
40 | 54 |
|
41 | 55 |
|
42 | 56 | def main():
|
|
0 commit comments