diff --git a/modules/background_tasks/tests/logs/sink/test_background_tasks_log_sink_loop.py b/modules/background_tasks/tests/logs/sink/test_background_tasks_log_sink_loop.py index e0244ce2..483fb458 100644 --- a/modules/background_tasks/tests/logs/sink/test_background_tasks_log_sink_loop.py +++ b/modules/background_tasks/tests/logs/sink/test_background_tasks_log_sink_loop.py @@ -33,9 +33,19 @@ def _root_at_debug() -> Iterator[None]: db_logger.setLevel(db_before) -def _count() -> int: +def _count(logger: str | None = None) -> int: + stmt = select(func.count()).select_from(RetainedLog) + if logger is not None: + stmt = stmt.where(RetainedLog.logger == logger) with sync_db.get_sync_session_factory()() as session: - return session.execute(select(func.count()).select_from(RetainedLog)).scalar_one() + return session.execute(stmt).scalar_one() + + +def _wait_for_app_lines(expected: int, timeout: float = 15.0) -> None: + deadline = time.monotonic() + timeout + while _count(APP_LOGGER.name) < expected: + assert time.monotonic() < deadline, "the sink never stored the app lines" + time.sleep(0.05) # intentional-sleep: polls the writer thread's flush def test_sink_writes_are_not_retained_again(): @@ -43,9 +53,10 @@ def test_sink_writes_are_not_retained_again(): assert handler is not None for i in range(5): APP_LOGGER.info("line %d", i) - time.sleep(1.5) # intentional-sleep: absence of feedback growth can only be observed over time + # The writer flushes on its own clock; a slow runner needs more than one interval. + _wait_for_app_lines(5) first = _count() - time.sleep(1.5) # intentional-sleep: second observation window + time.sleep(1.5) # intentional-sleep: absence of feedback growth can only be observed over time assert _count() == first log_sink.uninstall_log_sink() with sync_db.get_sync_session_factory()() as session: diff --git a/scripts/release_preflight.py b/scripts/release_preflight.py index 9d7f7d9f..43ee3063 100644 --- a/scripts/release_preflight.py +++ b/scripts/release_preflight.py @@ -3,6 +3,7 @@ import argparse from collections.abc import Sequence from pathlib import Path +from time import sleep from urllib.error import HTTPError, URLError from urllib.request import urlopen @@ -10,6 +11,23 @@ WORKFLOW = Path(__file__).resolve().parents[1] / ".github/workflows/release.yml" PYPI_PROJECT_URL = "https://pypi.org/pypi/{name}/json" +ATTEMPTS = 3 +RETRY_DELAY_SECONDS = 2.0 + + +def _probe(name: str) -> None: + """Open the project's JSON page; retry network failures and 5xx, raise the last one.""" + for attempt in range(1, ATTEMPTS + 1): + try: + with urlopen(PYPI_PROJECT_URL.format(name=name), timeout=30): + return + except HTTPError as exc: + if exc.code < 500 or attempt == ATTEMPTS: + raise + except (URLError, TimeoutError, OSError): + if attempt == ATTEMPTS: + raise + sleep(RETRY_DELAY_SECONDS * attempt) def main(argv: Sequence[str] | None = None) -> int: @@ -25,8 +43,7 @@ def main(argv: Sequence[str] | None = None) -> int: errors: list[str] = [] for name in parse_publish_matrix(WORKFLOW): try: - with urlopen(PYPI_PROJECT_URL.format(name=name), timeout=30): - pass + _probe(name) except HTTPError as exc: if exc.code == 404: if name in confirmed: diff --git a/scripts/tests/test_scripts_release_preflight_retry.py b/scripts/tests/test_scripts_release_preflight_retry.py new file mode 100644 index 00000000..43a171c5 --- /dev/null +++ b/scripts/tests/test_scripts_release_preflight_retry.py @@ -0,0 +1,55 @@ +"""The release preflight retries a flaky PyPI before failing the release.""" + +import ssl +from io import BytesIO +from urllib.error import HTTPError, URLError + +import pytest + +from scripts import release_preflight + + +def _http_error(code: int) -> HTTPError: + return HTTPError("https://pypi.org", code, "x", {}, None) + + +@pytest.fixture +def pypi(monkeypatch): + """Answer each probe from a list of outcomes; record how many were made.""" + outcomes: list[object] = [] + calls: list[str] = [] + + def fake_urlopen(url, timeout): + calls.append(url) + outcome = outcomes.pop(0) if outcomes else BytesIO(b"{}") + if isinstance(outcome, BaseException): + raise outcome + return outcome + + monkeypatch.setattr(release_preflight, "urlopen", fake_urlopen) + monkeypatch.setattr(release_preflight, "sleep", lambda _: None) + monkeypatch.setattr(release_preflight, "parse_publish_matrix", lambda _: ["simple_module_a"]) + return outcomes, calls + + +def test_a_transient_handshake_timeout_is_retried(pypi) -> None: + outcomes, calls = pypi + outcomes += [URLError(ssl.SSLError("The handshake operation timed out")), _http_error(503)] + assert release_preflight.main([]) == 0 + assert len(calls) == 3 + + +def test_a_persistent_network_failure_still_fails(pypi, capsys) -> None: + outcomes, calls = pypi + outcomes += [URLError("down")] * release_preflight.ATTEMPTS + assert release_preflight.main([]) == 1 + assert len(calls) == release_preflight.ATTEMPTS + assert "cannot check PyPI" in capsys.readouterr().out + + +def test_a_404_is_not_retried(pypi, capsys) -> None: + outcomes, calls = pypi + outcomes.append(_http_error(404)) + assert release_preflight.main([]) == 1 + assert len(calls) == 1 + assert "pending publisher" in capsys.readouterr().out diff --git a/scripts/tests/test_scripts_release_resumption.py b/scripts/tests/test_scripts_release_resumption.py index 47739c3b..b3882462 100644 --- a/scripts/tests/test_scripts_release_resumption.py +++ b/scripts/tests/test_scripts_release_resumption.py @@ -136,7 +136,10 @@ def test_preflight_accepts_existing_projects() -> None: def test_preflight_fails_closed_on_registry_errors(error: URLError, confirmed: str) -> None: from scripts.release_preflight import main - with patch("scripts.release_preflight.urlopen", side_effect=error): + with ( + patch("scripts.release_preflight.urlopen", side_effect=error), + patch("scripts.release_preflight.sleep"), + ): assert main(["--pending-publishers", confirmed]) == 1