diff --git a/gate/github.py b/gate/github.py index b27c6a60..f87befa1 100644 --- a/gate/github.py +++ b/gate/github.py @@ -95,7 +95,10 @@ def _wait_for_connectivity(max_wait: float = 30.0) -> bool: s.close() except Exception: pass - time.sleep(min(2.0, deadline - time.monotonic())) + remaining = deadline - time.monotonic() + if remaining <= 0: + break + time.sleep(min(2.0, remaining)) logger.warning(f"GitHub unreachable after {max_wait}s of probing") return False diff --git a/tests/test_github.py b/tests/test_github.py index 6111232e..af19dfe8 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -1,8 +1,10 @@ """Tests for gate.github module.""" +import socket import subprocess from unittest.mock import patch +from gate import github from gate.github import ( _build_comment, _format_build_section, @@ -16,6 +18,34 @@ ) +class TestWaitForConnectivity: + def test_timeout_does_not_sleep_negative(self, monkeypatch): + class FakeSocket: + def settimeout(self, _timeout): + pass + + def connect(self, _target): + raise TimeoutError("timed out") + + def close(self): + pass + + times = iter([0.0, 0.0, 6.0]) + sleeps: list[float] = [] + + monkeypatch.setattr( + socket, + "getaddrinfo", + lambda *_args, **_kwargs: [(None, None, None, None, ("127.0.0.1", 443))], + ) + monkeypatch.setattr(socket, "socket", lambda *_args, **_kwargs: FakeSocket()) + monkeypatch.setattr(github.time, "monotonic", lambda: next(times)) + monkeypatch.setattr(github.time, "sleep", sleeps.append) + + assert github._wait_for_connectivity(max_wait=5.0) is False + assert sleeps == [] + + class TestFormatFindings: def test_empty_findings(self): assert _format_findings([]) == ""