Skip to content
Open
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
12 changes: 10 additions & 2 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
ReviewRoutingViewType1,
ReviewRoutingViewType2,
)
# Socket timeouts on Windows can complete a fraction of a second early due to timer resolution.
_DEADLINE_TOLERANCE_SECONDS = 0.025


class ReviewFilters(TypedDict, total=False):
Expand Down Expand Up @@ -568,7 +570,10 @@ def wait_for_review(
try:
last = self.get_review(organization_id, review_id)
except ProoflineError as error:
if error.status == 0 and (_transport.remaining() or 0) <= 0:
if (
error.status == 0
and (_transport.remaining() or 0) <= _DEADLINE_TOLERANCE_SECONDS
):
break
raise
if (_transport.remaining() or 0) <= 0:
Expand Down Expand Up @@ -954,7 +959,10 @@ async def wait_for_review(
try:
last = await self.get_review(organization_id, review_id)
except ProoflineError as error:
if error.status == 0 and (_transport.remaining() or 0) <= 0:
if (
error.status == 0
and (_transport.remaining() or 0) <= _DEADLINE_TOLERANCE_SECONDS
):
break
raise
if (_transport.remaining() or 0) <= 0:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,3 +2586,32 @@ def respond(request: Request, index: int) -> Reply:
assert len(exported.rows) == 1
assert exported.rows[0]["claim"] == row["claim"]
assert exported.next_cursor == "next-page"


@MODES
async def test_wait_for_review_tolerates_early_socket_timeout_jitter(
mode: str, monkeypatch: pytest.MonkeyPatch
) -> None:
def raise_timeout(*args: Any, **kwargs: Any) -> Any:
raise _transport.timeout_error()

async def raise_timeout_async(*args: Any, **kwargs: Any) -> Any:
raise _transport.timeout_error()

target = raise_timeout_async if mode == "async" else raise_timeout

with Server(lambda request, index: Reply(status=200, body=review("PENDING"))) as server:
async with session(mode, server.url) as client:
with monkeypatch.context() as patch:
patch.setattr(client, "get_review", target)
patch.setattr(_transport, "remaining", lambda: 0.0005)
result = await call(client, "wait_for_review", ORG, REVIEW, timeout=0.1)
assert result.status == "TIMED_OUT"

with monkeypatch.context() as patch:
patch.setattr(client, "get_review", target)
patch.setattr(_transport, "remaining", lambda: 0.05)
with pytest.raises(ProoflineError) as raised:
await call(client, "wait_for_review", ORG, REVIEW, timeout=0.1)
assert raised.value.status == 0
assert raised.value.type == "urn:proofline:sdk:timeout"