|
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 | + call_timestamps.append(("tool_start_time", anyio.current_time())) |
20 | 22 | await anyio.sleep(_sleep_time_seconds)
|
| 23 | + call_timestamps.append(("tool_end_time", anyio.current_time())) |
21 | 24 | return "done"
|
22 | 25 |
|
23 | 26 | @server.resource(_resource_name)
|
24 | 27 | async def slow_resource():
|
| 28 | + call_timestamps.append(("resource_start_time", anyio.current_time())) |
25 | 29 | await anyio.sleep(_sleep_time_seconds)
|
| 30 | + call_timestamps.append(("resource_end_time", anyio.current_time())) |
26 | 31 | return "slow"
|
27 | 32 |
|
28 | 33 | async with create_session(server._mcp_server) as client_session:
|
29 |
| - start_time = anyio.current_time() |
30 | 34 | async with anyio.create_task_group() as tg:
|
31 | 35 | for _ in range(10):
|
32 | 36 | tg.start_soon(client_session.call_tool, "sleep")
|
33 | 37 | tg.start_soon(client_session.read_resource, AnyUrl(_resource_name))
|
34 | 38 |
|
35 |
| - end_time = anyio.current_time() |
36 |
| - |
37 |
| - duration = end_time - start_time |
38 |
| - assert duration < 10 * _sleep_time_seconds |
39 |
| - print(duration) |
| 39 | + active_calls = 0 |
| 40 | + max_concurrent_calls = 0 |
| 41 | + for call_type, _ in sorted(call_timestamps, key=lambda x: x[1]): |
| 42 | + if "start" in call_type: |
| 43 | + active_calls += 1 |
| 44 | + max_concurrent_calls = max(max_concurrent_calls, active_calls) |
| 45 | + else: |
| 46 | + active_calls -= 1 |
| 47 | + print(f"Max concurrent calls: {max_concurrent_calls}") |
| 48 | + assert max_concurrent_calls > 1, "No concurrent calls were executed" |
40 | 49 |
|
41 | 50 |
|
42 | 51 | def main():
|
|
0 commit comments