Skip to content

Commit a69677c

Browse files
committed
Add fast tests for custom request timeouts
1 parent bb0bf44 commit a69677c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

tests/test_query.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,61 @@ def test_default_timeout(
9292
assert not matches
9393

9494

95+
class TestCustomRequestTimeout:
96+
"""Tests for custom request timeout values."""
97+
98+
@staticmethod
99+
@pytest.mark.parametrize(
100+
argnames=(
101+
"custom_timeout",
102+
"response_delay_seconds",
103+
"expect_timeout",
104+
),
105+
argvalues=[
106+
(0.1, 0.09, False),
107+
(0.1, 0.11, True),
108+
((5.0, 0.1), 0.09, False),
109+
((5.0, 0.1), 0.11, True),
110+
],
111+
)
112+
def test_custom_timeout(
113+
image: io.BytesIO | BinaryIO,
114+
*,
115+
custom_timeout: float | tuple[float, float],
116+
response_delay_seconds: float,
117+
expect_timeout: bool,
118+
) -> None:
119+
"""Custom timeouts are honored for both float and tuple forms."""
120+
with (
121+
freeze_time() as frozen_datetime,
122+
MockVWS(
123+
response_delay_seconds=response_delay_seconds,
124+
sleep_fn=lambda seconds: (
125+
frozen_datetime.tick(
126+
delta=datetime.timedelta(seconds=seconds),
127+
),
128+
None,
129+
)[1],
130+
) as mock,
131+
):
132+
database = VuforiaDatabase()
133+
mock.add_database(database=database)
134+
cloud_reco_client = CloudRecoService(
135+
client_access_key=database.client_access_key,
136+
client_secret_key=database.client_secret_key,
137+
request_timeout_seconds=custom_timeout,
138+
)
139+
140+
if expect_timeout:
141+
with pytest.raises(
142+
expected_exception=requests.exceptions.Timeout,
143+
):
144+
cloud_reco_client.query(image=image)
145+
else:
146+
matches = cloud_reco_client.query(image=image)
147+
assert not matches
148+
149+
95150
class TestCustomBaseVWQURL:
96151
"""Tests for using a custom base VWQ URL."""
97152

tests/test_vws.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,72 @@ def test_default_timeout(
150150
)
151151

152152

153+
class TestCustomRequestTimeout:
154+
"""Tests for custom request timeout values."""
155+
156+
@staticmethod
157+
@pytest.mark.parametrize(
158+
argnames=(
159+
"custom_timeout",
160+
"response_delay_seconds",
161+
"expect_timeout",
162+
),
163+
argvalues=[
164+
(0.1, 0.09, False),
165+
(0.1, 0.11, True),
166+
((5.0, 0.1), 0.09, False),
167+
((5.0, 0.1), 0.11, True),
168+
],
169+
)
170+
def test_custom_timeout(
171+
image: io.BytesIO | BinaryIO,
172+
*,
173+
custom_timeout: float | tuple[float, float],
174+
response_delay_seconds: float,
175+
expect_timeout: bool,
176+
) -> None:
177+
"""Custom timeouts are honored for both float and tuple forms."""
178+
with (
179+
freeze_time() as frozen_datetime,
180+
MockVWS(
181+
response_delay_seconds=response_delay_seconds,
182+
sleep_fn=lambda seconds: (
183+
frozen_datetime.tick(
184+
delta=datetime.timedelta(seconds=seconds),
185+
),
186+
None,
187+
)[1],
188+
) as mock,
189+
):
190+
database = VuforiaDatabase()
191+
mock.add_database(database=database)
192+
vws_client = VWS(
193+
server_access_key=database.server_access_key,
194+
server_secret_key=database.server_secret_key,
195+
request_timeout_seconds=custom_timeout,
196+
)
197+
198+
if expect_timeout:
199+
with pytest.raises(
200+
expected_exception=requests.exceptions.Timeout,
201+
):
202+
vws_client.add_target(
203+
name="x",
204+
width=1,
205+
image=image,
206+
active_flag=True,
207+
application_metadata=None,
208+
)
209+
else:
210+
vws_client.add_target(
211+
name="x",
212+
width=1,
213+
image=image,
214+
active_flag=True,
215+
application_metadata=None,
216+
)
217+
218+
153219
class TestCustomBaseVWSURL:
154220
"""Tests for using a custom base VWS URL."""
155221

0 commit comments

Comments
 (0)