From 2796f3d3af81080ded6f51f8602881f3530af1b0 Mon Sep 17 00:00:00 2001 From: Ghraven Date: Sat, 6 Jun 2026 00:12:07 +0800 Subject: [PATCH] fix(cli): bound server readiness polling --- burr/cli/__main__.py | 4 +++- tests/cli/test_main.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/cli/test_main.py diff --git a/burr/cli/__main__.py b/burr/cli/__main__.py index 5bbc99dd5..33d1b95da 100644 --- a/burr/cli/__main__.py +++ b/burr/cli/__main__.py @@ -49,6 +49,8 @@ # Clear default handlers setup_logging(logging.INFO) +SERVER_READY_TIMEOUT_SECONDS = 1 + def _command(command: str, capture_output: bool, addl_env: dict | None = None) -> str: """Runs a simple command""" @@ -100,7 +102,7 @@ def _locate_package_root() -> Optional[str]: def open_when_ready(check_url: str, open_url: str): while True: try: - response = requests.get(check_url) + response = requests.get(check_url, timeout=SERVER_READY_TIMEOUT_SECONDS) if response.status_code == 200: webbrowser.open(open_url) return diff --git a/tests/cli/test_main.py b/tests/cli/test_main.py new file mode 100644 index 000000000..e31f0f711 --- /dev/null +++ b/tests/cli/test_main.py @@ -0,0 +1,25 @@ +from unittest.mock import Mock + +import requests + +from burr.cli import __main__ as cli_main + + +def test_open_when_ready_uses_timeout_and_retries(monkeypatch): + ready_response = Mock(status_code=200) + get = Mock(side_effect=[requests.exceptions.Timeout, ready_response]) + sleep = Mock() + open_browser = Mock() + + monkeypatch.setattr(cli_main.requests, "get", get) + monkeypatch.setattr(cli_main.time, "sleep", sleep) + monkeypatch.setattr(cli_main.webbrowser, "open", open_browser) + + cli_main.open_when_ready("http://localhost:7241", "http://localhost:7241/app") + + assert get.call_count == 2 + get.assert_called_with( + "http://localhost:7241", timeout=cli_main.SERVER_READY_TIMEOUT_SECONDS + ) + sleep.assert_called_once_with(1) + open_browser.assert_called_once_with("http://localhost:7241/app")