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
1 changed files with 45 additions and 98 deletions
31
tests/test_helpers.py
Normal file
31
tests/test_helpers.py
Normal 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")
|
||||
Loading…
Reference in a new issue