|
26 | 26 | from mcp.types import JSONRPCMessage |
27 | 27 |
|
28 | 28 |
|
| 29 | +async def mock_app_run(*args: Any, **kwargs: Any) -> None: |
| 30 | + """Mock app.run that blocks until cancelled instead of completing immediately.""" |
| 31 | + try: |
| 32 | + await anyio.sleep_forever() |
| 33 | + except anyio.get_cancelled_exc_class(): |
| 34 | + # Task was cancelled, which is expected when test exits |
| 35 | + pass |
| 36 | + |
| 37 | + |
29 | 38 | class SimpleEventStore(EventStore): |
30 | 39 | """Simple in-memory event store for testing session roaming.""" |
31 | 40 |
|
@@ -76,8 +85,8 @@ async def test_session_roaming_with_eventstore(): |
76 | 85 | # Create first manager instance (simulating pod 1) |
77 | 86 | manager1 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
78 | 87 |
|
79 | | - # Mock app.run to complete immediately |
80 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 88 | + # Mock app.run to block until cancelled |
| 89 | + app.run = mock_app_run # type: ignore[method-assign] |
81 | 90 |
|
82 | 91 | sent_messages: list[Message] = [] |
83 | 92 |
|
@@ -122,7 +131,7 @@ async def mock_receive() -> dict[str, Any]: |
122 | 131 | manager2 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
123 | 132 |
|
124 | 133 | # Mock app.run for manager2 |
125 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 134 | + app.run = mock_app_run # type: ignore[method-assign] |
126 | 135 |
|
127 | 136 | # Start manager2 and use the session from manager1 |
128 | 137 | async with manager2.run(): |
@@ -199,7 +208,7 @@ async def test_session_roaming_concurrent_requests(): |
199 | 208 |
|
200 | 209 | # Create first manager and a session |
201 | 210 | manager1 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
202 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 211 | + app.run = mock_app_run # type: ignore[method-assign] |
203 | 212 |
|
204 | 213 | sent_messages: list[Message] = [] |
205 | 214 |
|
@@ -235,7 +244,7 @@ async def mock_receive() -> dict[str, Any]: |
235 | 244 |
|
236 | 245 | # Create second manager |
237 | 246 | manager2 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
238 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 247 | + app.run = mock_app_run # type: ignore[method-assign] |
239 | 248 |
|
240 | 249 | async with manager2.run(): |
241 | 250 | # Make two concurrent requests with the same roaming session ID |
@@ -362,7 +371,7 @@ async def test_session_roaming_fast_path_unchanged(): |
362 | 371 | event_store = SimpleEventStore() |
363 | 372 | manager = StreamableHTTPSessionManager(app=app, event_store=event_store) |
364 | 373 |
|
365 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 374 | + app.run = mock_app_run # type: ignore[method-assign] |
366 | 375 |
|
367 | 376 | sent_messages: list[Message] = [] |
368 | 377 |
|
@@ -433,7 +442,7 @@ async def test_session_roaming_logs_correctly(caplog: Any): # type: ignore[misc |
433 | 442 |
|
434 | 443 | # Create first manager and session |
435 | 444 | manager1 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
436 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 445 | + app.run = mock_app_run # type: ignore[method-assign] |
437 | 446 |
|
438 | 447 | sent_messages: list[Message] = [] |
439 | 448 |
|
@@ -471,7 +480,7 @@ async def mock_receive() -> dict[str, Any]: |
471 | 480 |
|
472 | 481 | # Create second manager |
473 | 482 | manager2 = StreamableHTTPSessionManager(app=app, event_store=event_store) |
474 | | - app.run = AsyncMock(return_value=None) # type: ignore[method-assign] |
| 483 | + app.run = mock_app_run # type: ignore[method-assign] |
475 | 484 |
|
476 | 485 | async with manager2.run(): |
477 | 486 | scope_with_session = { |
|
0 commit comments