diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index d396193f..9e8c1472 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -1403,13 +1403,18 @@ def _merge_check_hint(config: Config, pr: github.PR, readiness: github.Readiness return "" -def _poll_until(started: float, timeout: float, *, bounded: bool = False) -> Iterator[None]: +def _poll_until( + started: float, timeout: float, *, bounded: bool = False, admitted: Callable[[], bool] | None = None, +) -> Iterator[None]: delay = WAIT_INTERVAL while True: + was_admitted = admitted is not None and admitted() yield # Observe immediately, including --wait=0. remaining = timeout - (time.monotonic() - started) if remaining <= 0: return + if admitted is not None and not was_admitted and admitted(): + delay = WAIT_INTERVAL pause = delay * random.uniform(0.9, 1.0) if bounded and remaining <= pause: # Leave part of the final interval for the last observation to complete. @@ -1664,7 +1669,8 @@ def _wait_for_mergify( state = None deferred_landing_read = False print(f"PR #{pr.number} following Mergify queue {queue}; head {pr.head_sha[:12]}; waiting up to {timeout:g}s") - for _ in _poll_until(started, timeout): + polls = _poll_until(started, timeout, admitted=lambda: confirmed) + for _ in polls: sha = _landed(config, pr, closed) if sha: return sha diff --git a/tests/test_cli.py b/tests/test_cli.py index 8c5a895b..2c88f694 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4338,7 +4338,8 @@ def test_ready_pins_review_publishers_to_base(wt, repo, gh, capsys, monkeypatch, assert json.loads(gh.calls[-1]["--input"])["state"] == ("success" if publisher == LOGIN else "pending") -def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatch): +@pytest.mark.parametrize("confirmed", [True, False]) +def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatch, confirmed): head = commit_mergify_configuration(wt) config = cli.config_mod.load_trusted(wt) pr = github.PR(7, URL, head, "main", False, "OPEN", "MERGEABLE") @@ -4350,22 +4351,21 @@ def test_mergify_confirmed_wait_reads_pr_once_per_steady_poll(wt, gh, monkeypatc def membership(*_): reads_at_membership.append(len(gh.calls)) - if clock.now == 60: + if confirmed and clock.now == 60: gh.respond(" ".join(detail), merge_json(state="MERGED", head=head, sha="e" * 40)) - return mergify.Membership() - - def configured(*args): - configurations.append(args) - return True + return mergify.Membership() if confirmed or clock.now >= 210 else None - monkeypatch.setattr(mergify, "configured", configured) + monkeypatch.setattr(mergify, "configured", lambda *args: configurations.append(args) or True) + monkeypatch.setattr(mergify, "mark_confirmed", lambda *_: gh.respond( + " ".join(detail), merge_json(state="MERGED", head=head, sha="e" * 40))) monkeypatch.setattr(mergify, "membership", membership) - assert cli._wait_for_mergify(config, pr, 60, confirmed=True) == "e" * 40 - assert clock.sleeps == [30, 30] - assert configurations == [(REPO, "main", "main")] * 3 - assert reads_at_membership == [1, 2, 3] - assert [c["argv"][:2] for c in gh.calls] == [detail] * 4 + assert cli._wait_for_mergify(config, pr, 60 if confirmed else 360, confirmed=confirmed) == "e" * 40 + assert clock.sleeps == ([30, 30] if confirmed else [30, 60, 120, 30]) + assert configurations == [(REPO, "main", "main")] * (3 if confirmed else 4) + assert reads_at_membership == ([1, 2, 3] if confirmed else [1, 4, 7, 10]) + assert sum(c["argv"][:2] == detail for c in gh.calls) == (4 if confirmed else 9) + assert len(gh.calls) == (4 if confirmed else 12) @pytest.mark.parametrize("outcome,jitter", [ @@ -4438,9 +4438,9 @@ def membership(*_): if outcome == "worktree_removed": poll_until = cli._poll_until - def remove_worktree_then_poll(*args): + def remove_worktree_then_poll(*args, **kwargs): git(repo, "worktree", "remove", "--force", str(wt)) - return poll_until(*args) + return poll_until(*args, **kwargs) monkeypatch.setattr(cli, "_poll_until", remove_worktree_then_poll) try: