Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,30 @@ 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():
handler = log_sink.install_log_sink(BackgroundTasksSettings(log_sink_enabled=True))
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:
Expand Down
21 changes: 19 additions & 2 deletions scripts/release_preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
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

from scripts.check_metadata import parse_publish_matrix

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:
Expand All @@ -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:
Expand Down
55 changes: 55 additions & 0 deletions scripts/tests/test_scripts_release_preflight_retry.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion scripts/tests/test_scripts_release_resumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading