fix: Replace arbitrary sleeps with active server readiness checks in tests (#1527)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Max Isbey 2025-10-28 21:42:33 +00:00 • committed by GitHub
commit 3e86edfb2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

31
tests/test_helpers.py Normal file
View file

@ -0,0 +1,31 @@
"""Common test utilities for MCP server tests."""
import socket
import time
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.
Args:
port: The port number to check
timeout: Maximum time to wait in seconds (default 5.0)
Raises:
TimeoutError: If server doesn't start within the timeout period
"""
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")