diff --git a/README.md b/README.md index 250fb32e..b073a364 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,13 @@ if outcome.status != "COMPLETED": print(outcome.status, outcome.reason) ``` +Reads back off from 5 to 60 seconds apart. When the next read would fall in the +last 5 seconds of the wait or after its deadline, the wait reads 5 seconds before +the deadline instead, unless that is under 5 seconds after the previous read. A +review that finishes during the final interval is therefore not reported as +`TIMED_OUT`. A review that has still not finished after that read ends the wait at +its deadline, as before. + Set `timeout=0` for one read using the client's request timeout. A completed review that finishes decoding at the wait deadline still produces an outcome for the requested review. If a server retry delay would reach the wait deadline, diff --git a/proofline/client.py b/proofline/client.py index 49e1a9e1..22f8afaf 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -163,6 +163,20 @@ def _head_commit(value: str | None) -> None: raise ValueError("The head commit must be a full 40 or 64 character hexadecimal SHA.") +_FIRST_POLL_INTERVAL = 5.0 +_LAST_READ_LEAD = 5.0 + + +def _poll_delay(interval: float, budget: float) -> float: + # A read scheduled at the deadline never runs, so the wait would sleep out its + # last interval unobserved. Read once more shortly before the deadline instead, + # unless that read would come sooner than the first poll interval allows. + if interval + _LAST_READ_LEAD < budget: + return interval + last_read = budget - _LAST_READ_LEAD + return last_read if last_read >= _FIRST_POLL_INTERVAL else min(interval, budget) + + def _next_cursor(page: ReviewPage, seen: set[str]) -> str | None: if not isinstance(page.truncated, bool): raise _transport.invalid_response() @@ -550,7 +564,11 @@ def wait_for_review( """Wait for recorded review completion or return TIMED_OUT. A name lookup still running at the deadline returns TIMED_OUT; it never - reports completion. + reports completion. Reads back off from 5 to 60 seconds apart. When the + next read would fall in the last five seconds or after the deadline, it + moves to five seconds before the deadline, so the final interval is not + slept out unobserved. If that would put it under five seconds after the + previous read, the read keeps its place and may not run at all. head_commit_sha holds the outcome to that commit. A review pinned to another head ends the wait on that read and is never COMPLETED. @@ -561,7 +579,7 @@ def wait_for_review( _head_commit(head_commit_sha) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None - interval = 5.0 + interval = _FIRST_POLL_INTERVAL with _transport.review_wait_budget(timeout): while timeout == 0 or (_transport.remaining() or 0) > 0: try: @@ -578,7 +596,7 @@ def wait_for_review( budget = _transport.remaining() if timeout == 0 or not budget: break - sleep(min(interval, budget)) + sleep(_poll_delay(interval, budget)) interval = min(60, interval * 2) timed_out = outcome( last, url, "TIMED_OUT", review_id=review_id, head_commit_sha=head_commit_sha @@ -949,7 +967,7 @@ async def wait_for_review( _head_commit(head_commit_sha) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None - interval = 5.0 + interval = _FIRST_POLL_INTERVAL with _transport.review_wait_budget(timeout): while timeout == 0 or (_transport.remaining() or 0) > 0: try: @@ -966,7 +984,7 @@ async def wait_for_review( budget = _transport.remaining() if timeout == 0 or not budget: break - await async_sleep(min(interval, budget)) + await async_sleep(_poll_delay(interval, budget)) interval = min(60, interval * 2) timed_out = outcome( last, url, "TIMED_OUT", review_id=review_id, head_commit_sha=head_commit_sha diff --git a/tests/test_http.py b/tests/test_http.py index 68c6228e..c0eec7c8 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1441,6 +1441,61 @@ async def test_polling_backoff_and_timeout_keep_review_url_and_last_record( assert len(server.requests) == 8 +@MODES +async def test_wait_reads_again_before_the_deadline_instead_of_sleeping_it_out( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + started = clock.now + + def respond(request: Request, index: int) -> Reply: + # The review finishes 260 seconds in: after the read at 255, inside the budget. + return Reply(body=review("COMPLETED" if clock.now - started >= 260 else "RUNNING")) + + with Server(respond) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW) + assert result.status == "COMPLETED" + assert result.review.id == REVIEW + assert clock.delays == [5, 10, 20, 40, 60, 60, 60, 40] + assert clock.now - started == 295 + assert len(server.requests) == 9 + + +@pytest.mark.parametrize( + ("interval", "budget", "delay"), + [ + (5, 300, 5), # far from the deadline: the backoff interval + (60, 45, 40), # the next read would land past the deadline: read 5 s before it + (60, 60.5, 55.5), # the next read would land inside the last 5 s + (5, 10, 5), # the moved read coincides with the regular one + (5, 9, 5), # moving it would poll sooner than 5 s: keep the interval + (60, 5, 5), # within the last read's lead: sleep out the budget as before + ], +) +def test_poll_delay_moves_only_a_read_the_deadline_would_skip( + interval: float, budget: float, delay: float +) -> None: + assert client_module._poll_delay(interval, budget) == delay + + +@MODES +async def test_wait_still_lasts_its_timeout_after_the_last_read( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + started = clock.now + with Server(lambda request, index: Reply(body=review("RUNNING"))) as server: + async with session(mode, server.url) as client: + result = await call(client, "wait_for_review", ORG, REVIEW, timeout=120) + assert result.status == "TIMED_OUT" + assert result.review.id == REVIEW + # Reads at 0, 5, 15, 35, 75 and 115; the last five seconds stay a wait, not a poll. + assert clock.delays == [5, 10, 20, 40, 40, 5] + assert clock.now - started == 120 + assert len(server.requests) == 6 + + @MODES @pytest.mark.parametrize("status", ["COMPLETED", "RUNNING"]) async def test_zero_wait_reads_once(