Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions tests/client/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,16 @@ async def nested_sse_server():
ws="websockets-sansio",
)

server_task = asyncio.create_task(uvicorn.Server(config).serve())
uvicorn_server = uvicorn.Server(config)
server_task = asyncio.create_task(uvicorn_server.serve())
await asyncio.sleep(0.1)

try:
yield f"http://127.0.0.1:{port}/nest-outer/nest-inner/mcp/sse/"
finally:
server_task.cancel()
try:
await server_task
except asyncio.CancelledError:
pass
# Graceful shutdown - required for uvicorn 0.39+ due to context isolation
uvicorn_server.should_exit = True
await server_task


async def test_run_server_on_path(sse_server_custom_path: str):
Expand Down
12 changes: 5 additions & 7 deletions tests/client/test_streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,15 @@ async def nested_server():
)

# Use the simple asyncio pattern
server_task = asyncio.create_task(uvicorn.Server(config).serve())
uvicorn_server = uvicorn.Server(config)
server_task = asyncio.create_task(uvicorn_server.serve())
await asyncio.sleep(0.1)

yield f"http://127.0.0.1:{port}/nest-outer/nest-inner/final/mcp"

# Cleanup
server_task.cancel()
try:
await server_task
except asyncio.CancelledError:
pass
# Graceful shutdown - required for uvicorn 0.39+ due to context isolation
uvicorn_server.should_exit = True
await server_task


async def test_ping(streamable_http_server: str):
Expand Down
27 changes: 21 additions & 6 deletions tests/server/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ async def test_uvicorn_logging_default_level(
)
mock_server_instance.serve.assert_awaited_once()

# Signal the mock to finish and cancel with timeout
# Required for uvicorn 0.39+ due to context isolation
serve_finished_event.set()
server_task.cancel()
with pytest.raises(asyncio.CancelledError):
await server_task
try:
await asyncio.wait_for(server_task, timeout=2.0)
except (asyncio.CancelledError, asyncio.TimeoutError):
pass


@patch("fastmcp.server.server.uvicorn.Server")
Expand Down Expand Up @@ -109,9 +114,14 @@ async def test_uvicorn_logging_with_custom_log_config(
)
mock_server_instance.serve.assert_awaited_once()

# Signal the mock to finish and cancel with timeout
# Required for uvicorn 0.39+ due to context isolation
serve_finished_event.set()
server_task.cancel()
with pytest.raises(asyncio.CancelledError):
await server_task
try:
await asyncio.wait_for(server_task, timeout=2.0)
except (asyncio.CancelledError, asyncio.TimeoutError):
pass


@patch("fastmcp.server.server.uvicorn.Server")
Expand Down Expand Up @@ -172,6 +182,11 @@ async def test_uvicorn_logging_custom_log_config_overrides_log_level_param(
)
mock_server_instance.serve.assert_awaited_once()

# Signal the mock to finish and cancel with timeout
# Required for uvicorn 0.39+ due to context isolation
serve_finished_event.set()
server_task.cancel()
with pytest.raises(asyncio.CancelledError):
await server_task
try:
await asyncio.wait_for(server_task, timeout=2.0)
except (asyncio.CancelledError, asyncio.TimeoutError):
pass
Loading