-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: Improve code using suggestions by @coderabbitai
- Loading branch information
Showing
4 changed files
with
67 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,53 @@ | ||
import socket | ||
import time | ||
|
||
import requests | ||
|
||
|
||
def random_port(): | ||
def random_port() -> int: | ||
""" | ||
Return a random available port. | ||
""" | ||
import socket | ||
Return a random available port by binding to port 0. | ||
Returns: | ||
int: An available port number that can be used for testing. | ||
""" | ||
sock = socket.socket() | ||
sock.bind(("", 0)) | ||
port = sock.getsockname()[1] | ||
sock.close() | ||
return port | ||
try: | ||
sock.bind(("", 0)) | ||
return sock.getsockname()[1] | ||
finally: | ||
sock.close() | ||
|
||
|
||
def wait_server(port: int): | ||
def wait_server( | ||
port: int, | ||
host: str = "127.0.0.1", | ||
protocol: str = "http", | ||
attempts: int = 20, | ||
timeout: float = 0.1, | ||
): | ||
""" | ||
Wait for server to be ready. | ||
Wait for server to be ready by attempting to connect to it. | ||
Args: | ||
port: The port number to connect to | ||
host: The host to connect to (default: "127.0.0.1") | ||
protocol: The protocol to use (default: "http") | ||
attempts: Number of connection attempts (default: 20) | ||
timeout: Timeout per attempt in seconds (default: 0.1) | ||
Raises: | ||
RuntimeError: If server is not ready after all attempts | ||
""" | ||
for _ in range(20): # 2 second timeout | ||
url = f"{protocol}://{host}:{port}/" | ||
for _ in range(1, attempts + 1): | ||
try: | ||
requests.get(f"http://127.0.0.1:{port}/", timeout=0.1) | ||
requests.get(url, timeout=timeout) | ||
break | ||
except requests.exceptions.RequestException: | ||
time.sleep(0.1) | ||
time.sleep(timeout) | ||
else: | ||
raise RuntimeError("Server failed to start") | ||
raise RuntimeError( | ||
f"Server at {url} failed to respond after {attempts} attempts " | ||
f"(total wait time: {attempts * timeout * 2:.1f}s)" | ||
) |