Skip to content

Commit 7123758

Browse files
adamtheturtleclaude
andcommitted
Fix CI failures in timeout tests
- Remove branching and time.sleep from slow_request mocks, fixing coverage gaps (partial branches) and Windows CI timeouts - Remove test_longer_timeout_succeeds (redundant with test_custom_timeout) - Assert the timeout value is passed correctly instead of branching - Remove unused time import and unused *args parameter Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1b2001e commit 7123758

2 files changed

Lines changed: 10 additions & 115 deletions

File tree

tests/test_query.py

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Tests for the ``CloudRecoService`` querying functionality."""
22

33
import io
4-
import time
54
import uuid
65
from typing import BinaryIO
76
from unittest.mock import patch
@@ -156,6 +155,7 @@ def test_timeout_raises_on_slow_response(
156155
image: io.BytesIO | BinaryIO,
157156
) -> None:
158157
"""A short timeout raises an error when the server is slow."""
158+
custom_timeout = 0.1
159159
with MockVWS() as mock:
160160
database = VuforiaDatabase()
161161
mock.add_database(database=database)
@@ -166,7 +166,7 @@ def test_timeout_raises_on_slow_response(
166166
cloud_reco_client = CloudRecoService(
167167
client_access_key=database.client_access_key,
168168
client_secret_key=database.client_secret_key,
169-
request_timeout_seconds=0.1,
169+
request_timeout_seconds=custom_timeout,
170170
)
171171

172172
target_id = vws_client.add_target(
@@ -178,19 +178,12 @@ def test_timeout_raises_on_slow_response(
178178
)
179179
vws_client.wait_for_target_processed(target_id=target_id)
180180

181-
simulated_slow_threshold = 0.5
182-
original_request = requests.request
183-
184181
def slow_request(
185-
*args: object,
186182
**kwargs: float | None,
187183
) -> requests.Response:
188-
"""Simulate a slow server response."""
189-
timeout = kwargs.get("timeout")
190-
if timeout is not None and timeout < simulated_slow_threshold:
191-
time.sleep(0.2)
192-
raise requests.exceptions.Timeout
193-
return original_request(*args, **kwargs) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
184+
"""Simulate a server that is too slow to respond."""
185+
assert kwargs["timeout"] == custom_timeout
186+
raise requests.exceptions.Timeout
194187

195188
with (
196189
patch.object(
@@ -202,55 +195,6 @@ def slow_request(
202195
):
203196
cloud_reco_client.query(image=image)
204197

205-
@staticmethod
206-
def test_longer_timeout_succeeds(image: io.BytesIO | BinaryIO) -> None:
207-
"""A longer timeout allows slow responses to complete."""
208-
simulated_slow_threshold = 0.5
209-
210-
with MockVWS() as mock:
211-
database = VuforiaDatabase()
212-
mock.add_database(database=database)
213-
vws_client = VWS(
214-
server_access_key=database.server_access_key,
215-
server_secret_key=database.server_secret_key,
216-
)
217-
cloud_reco_client = CloudRecoService(
218-
client_access_key=database.client_access_key,
219-
client_secret_key=database.client_secret_key,
220-
request_timeout_seconds=1.0,
221-
)
222-
223-
target_id = vws_client.add_target(
224-
name="x",
225-
width=1,
226-
image=image,
227-
active_flag=True,
228-
application_metadata=None,
229-
)
230-
vws_client.wait_for_target_processed(target_id=target_id)
231-
232-
original_request = requests.request
233-
234-
def slow_request(
235-
*args: object,
236-
**kwargs: float | None,
237-
) -> requests.Response:
238-
"""Simulate a slow server response."""
239-
timeout = kwargs.get("timeout")
240-
if timeout is not None and timeout < simulated_slow_threshold:
241-
time.sleep(0.2)
242-
raise requests.exceptions.Timeout
243-
return original_request(*args, **kwargs) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
244-
245-
with patch.object(
246-
target=requests,
247-
attribute="request",
248-
side_effect=slow_request,
249-
):
250-
# This should succeed because timeout is 1.0 > 0.5
251-
matches = cloud_reco_client.query(image=image)
252-
assert len(matches) == 1
253-
254198

255199
class TestCustomBaseVWQURL:
256200
"""Tests for using a custom base VWQ URL."""

tests/test_vws.py

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import datetime
55
import io
66
import secrets
7-
import time
87
import uuid
98
from typing import BinaryIO
109
from unittest.mock import patch
@@ -184,29 +183,22 @@ def test_timeout_raises_on_slow_response(
184183
image: io.BytesIO | BinaryIO,
185184
) -> None:
186185
"""A short timeout raises an error when the server is slow."""
187-
simulated_slow_threshold = 0.5
188-
186+
custom_timeout = 0.1
189187
with MockVWS() as mock:
190188
database = VuforiaDatabase()
191189
mock.add_database(database=database)
192190
vws_client = VWS(
193191
server_access_key=database.server_access_key,
194192
server_secret_key=database.server_secret_key,
195-
request_timeout_seconds=0.1,
193+
request_timeout_seconds=custom_timeout,
196194
)
197195

198-
original_request = requests.request
199-
200196
def slow_request(
201-
*args: object,
202197
**kwargs: float | None,
203198
) -> requests.Response:
204-
"""Simulate a slow server response."""
205-
timeout = kwargs.get("timeout")
206-
if timeout is not None and timeout < simulated_slow_threshold:
207-
time.sleep(0.2)
208-
raise requests.exceptions.Timeout
209-
return original_request(*args, **kwargs) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
199+
"""Simulate a server that is too slow to respond."""
200+
assert kwargs["timeout"] == custom_timeout
201+
raise requests.exceptions.Timeout
210202

211203
with (
212204
patch.object(
@@ -224,47 +216,6 @@ def slow_request(
224216
application_metadata=None,
225217
)
226218

227-
@staticmethod
228-
def test_longer_timeout_succeeds(image: io.BytesIO | BinaryIO) -> None:
229-
"""A longer timeout allows slow responses to complete."""
230-
simulated_slow_threshold = 0.5
231-
232-
with MockVWS() as mock:
233-
database = VuforiaDatabase()
234-
mock.add_database(database=database)
235-
vws_client = VWS(
236-
server_access_key=database.server_access_key,
237-
server_secret_key=database.server_secret_key,
238-
request_timeout_seconds=1.0,
239-
)
240-
241-
original_request = requests.request
242-
243-
def slow_request(
244-
*args: object,
245-
**kwargs: float | None,
246-
) -> requests.Response:
247-
"""Simulate a slow server response."""
248-
timeout = kwargs.get("timeout")
249-
if timeout is not None and timeout < simulated_slow_threshold:
250-
time.sleep(0.2)
251-
raise requests.exceptions.Timeout
252-
return original_request(*args, **kwargs) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
253-
254-
with patch.object(
255-
target=requests,
256-
attribute="request",
257-
side_effect=slow_request,
258-
):
259-
# This should succeed because timeout is 1.0 > 0.5
260-
vws_client.add_target(
261-
name="x",
262-
width=1,
263-
image=image,
264-
active_flag=True,
265-
application_metadata=None,
266-
)
267-
268219

269220
class TestCustomBaseVWSURL:
270221
"""Tests for using a custom base VWS URL."""

0 commit comments

Comments
 (0)