Skip to content

Commit 10e7f21

Browse files
committed
Wait for real reco reports to become ready
1 parent 30622af commit 10e7f21

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

‎tests/mock_vws/test_reco_counts_report.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,36 @@ def _request_reco_counts_report(
121121
)
122122

123123

124+
@retry(
125+
wait=wait_fixed(wait=0.5),
126+
stop=stop_after_delay(max_delay=60),
127+
retry=retry_if_exception_type(exception_types=(_ReportNotReadyError,)),
128+
reraise=True,
129+
)
130+
@beartype
131+
def _request_ready_reco_counts_report(
132+
*,
133+
vuforia_database: CloudDatabase,
134+
month: str,
135+
) -> requests.Response:
136+
"""Request a report until the generated report is ready."""
137+
response = _request_reco_counts_report(
138+
vuforia_database=vuforia_database,
139+
database_id=vuforia_database.database_id,
140+
month=month,
141+
)
142+
if response.status_code == HTTPStatus.NOT_FOUND:
143+
raise _ReportNotReadyError
144+
return response
145+
146+
124147
@beartype
125148
def _presigned_url(*, vuforia_database: CloudDatabase, month: str) -> str:
126149
"""Request a report for the given month and return its download
127150
URL.
128151
"""
129-
response = _request_reco_counts_report(
152+
response = _request_ready_reco_counts_report(
130153
vuforia_database=vuforia_database,
131-
database_id=vuforia_database.database_id,
132154
month=month,
133155
)
134156
assert response.status_code == HTTPStatus.OK
@@ -224,9 +246,8 @@ def test_reco_counts_report(
224246
"""A report can be requested for the current and previous
225247
month.
226248
"""
227-
response = _request_reco_counts_report(
249+
response = _request_ready_reco_counts_report(
228250
vuforia_database=vuforia_database,
229-
database_id=vuforia_database.database_id,
230251
month=_month_offset_from_now(months=-months_ago),
231252
)
232253

‎tests/mock_vws/utils/__init__.py‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import secrets
55
from collections.abc import Mapping
66
from dataclasses import dataclass
7+
from http import HTTPStatus
78
from typing import Literal
89
from urllib.parse import urljoin
910

@@ -13,6 +14,12 @@
1314
from PIL import Image
1415
from requests.exceptions import Timeout as RequestsTimeout
1516
from requests.structures import CaseInsensitiveDict
17+
from tenacity import (
18+
retry,
19+
retry_if_exception_type,
20+
stop_after_delay,
21+
wait_fixed,
22+
)
1623
from vws.response import Response
1724

1825
from mock_vws._constants import ResultCodes
@@ -23,6 +30,11 @@
2330
_REQUEST_TIMEOUT_SECONDS = 5
2431

2532

33+
@beartype
34+
class _TransientRecoCountsNotFoundError(Exception):
35+
"""The real reco counts endpoint temporarily returned an HTML 404."""
36+
37+
2638
@beartype
2739
def _send_request(
2840
*,
@@ -63,6 +75,39 @@ def _send_request(
6375
)
6476

6577

78+
@retry(
79+
wait=wait_fixed(wait=0.5),
80+
stop=stop_after_delay(max_delay=60),
81+
retry=retry_if_exception_type(
82+
exception_types=(_TransientRecoCountsNotFoundError,),
83+
),
84+
reraise=True,
85+
)
86+
@beartype
87+
def _send_endpoint_request(
88+
*,
89+
method: str,
90+
url: str,
91+
headers: Mapping[str, str],
92+
data: bytes | str,
93+
) -> Response:
94+
"""Send an endpoint request, retrying a transient real-service 404."""
95+
response = _send_request(
96+
method=method,
97+
url=url,
98+
headers=headers,
99+
data=data,
100+
)
101+
if (
102+
url.startswith("https://vws.vuforia.com/imagetargets/databases/")
103+
and url.endswith("/reports/recoCounts")
104+
and response.status_code == HTTPStatus.NOT_FOUND
105+
and response.headers.get("Content-Type", "").startswith("text/html")
106+
):
107+
raise _TransientRecoCountsNotFoundError
108+
return response
109+
110+
66111
@dataclass(frozen=True, kw_only=True)
67112
class Endpoint:
68113
"""Details of endpoints to be called in tests.
@@ -103,7 +148,7 @@ class Endpoint:
103148
@beartype
104149
def send(self) -> Response:
105150
"""Send the request."""
106-
return _send_request(
151+
return _send_endpoint_request(
107152
method=self.method,
108153
url=urljoin(base=self.base_url, url=self.path_url),
109154
headers=self.headers,

0 commit comments

Comments
 (0)