fix: Replace arbitrary sleeps with active server readiness checks in tests (#1527)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
f97f7c4a7d
commit
3e86edfb2f
2 changed files with 45 additions and 98 deletions
|
|
@ -3,7 +3,6 @@
|
|||
import logging
|
||||
import multiprocessing
|
||||
import socket
|
||||
import time
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
|
@ -17,6 +16,7 @@ from mcp.server import Server
|
|||
from mcp.server.sse import SseServerTransport
|
||||
from mcp.server.transport_security import TransportSecuritySettings
|
||||
from mcp.types import Tool
|
||||
from tests.test_helpers import wait_for_server
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
SERVER_NAME = "test_sse_security_server"
|
||||
|
|
@ -66,26 +66,6 @@ def run_server_with_settings(port: int, security_settings: TransportSecuritySett
|
|||
uvicorn.run(starlette_app, host="127.0.0.1", port=port, log_level="error")
|
||||
|
||||
|
||||
def wait_for_server(port: int, timeout: float = 5.0) -> None:
|
||||
"""Wait for server to be ready to accept connections.
|
||||
|
||||
Polls the server port until it accepts connections or timeout is reached.
|
||||
This eliminates race conditions without arbitrary sleeps.
|
||||
"""
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
s.settimeout(0.1)
|
||||
s.connect(("127.0.0.1", port))
|
||||
# Server is ready
|
||||
return
|
||||
except (ConnectionRefusedError, OSError):
|
||||
# Server not ready yet, retry quickly
|
||||
time.sleep(0.01)
|
||||
raise TimeoutError(f"Server on port {port} did not start within {timeout} seconds")
|
||||
|
||||
|
||||
def start_server_process(port: int, security_settings: TransportSecuritySettings | None = None):
|
||||
"""Start server in a separate process."""
|
||||
process = multiprocessing.Process(target=run_server_with_settings, args=(port, security_settings))
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
import logging
|
||||
import multiprocessing
|
||||
import socket
|
||||
import time
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
|
|
@ -18,6 +17,7 @@ from mcp.server import Server
|
|||
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
|
||||
from mcp.server.transport_security import TransportSecuritySettings
|
||||
from mcp.types import Tool
|
||||
from tests.test_helpers import wait_for_server
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
SERVER_NAME = "test_streamable_http_security_server"
|
||||
|
|
@ -77,8 +77,8 @@ def start_server_process(port: int, security_settings: TransportSecuritySettings
|
|||
"""Start server in a separate process."""
|
||||
process = multiprocessing.Process(target=run_server_with_settings, args=(port, security_settings))
|
||||
process.start()
|
||||
# Give server time to start
|
||||
time.sleep(1)
|
||||
# Wait for server to be ready to accept connections
|
||||
wait_for_server(port)
|
||||
return process
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue