Skip to content

Commit 8d082a6

Browse files
Merge pull request #895 from adamtheturtle/custom-timeout
Custom timeout
2 parents ebab5b9 + 87ec803 commit 8d082a6

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/vws/vws.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def wait_for_target_processed(
310310
self,
311311
target_id: str,
312312
seconds_between_requests: float = 0.2,
313+
timeout_seconds: float = 60 * 5,
313314
) -> None:
314315
"""
315316
Wait up to five minutes (arbitrary) for a target to get past the
@@ -322,18 +323,19 @@ def wait_for_target_processed(
322323
We wait 0.2 seconds by default, rather than less, than that to
323324
decrease the number of calls made to the API, to decrease the
324325
likelihood of hitting the request quota.
326+
timeout_seconds: The maximum number of seconds to wait for the
327+
target to be processed.
325328
326329
Raises:
327330
~vws.exceptions.AuthenticationFailure: The secret key is not
328331
correct.
329332
~vws.exceptions.Fail: There was an error with the request. For
330333
example, the given access key does not match a known database.
331334
TimeoutError: The target remained in the processing stage for more
332-
than five minutes.
335+
than ``timeout_seconds`` seconds.
333336
~vws.exceptions.UnknownTarget: The given target ID does not match a
334337
target in the database.
335338
"""
336-
timeout_seconds = 60 * 5
337339

338340
@timeout(seconds=timeout_seconds)
339341
def decorated() -> None:

tests/test_vws.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Optional
77

88
import pytest
9+
import timeout_decorator
910
from mock_vws import MockVWS
1011
from mock_vws.database import VuforiaDatabase
1112

@@ -364,6 +365,42 @@ def test_custom_seconds_between_requests(
364365
)
365366
assert report['request_usage'] == expected_requests
366367

368+
def test_custom_timeout(
369+
self,
370+
high_quality_image: io.BytesIO,
371+
) -> None:
372+
"""
373+
It is possible to set a maximum timeout.
374+
"""
375+
with MockVWS(processing_time_seconds=0.5) as mock:
376+
database = VuforiaDatabase()
377+
mock.add_database(database=database)
378+
client = VWS(
379+
server_access_key=database.server_access_key.decode(),
380+
server_secret_key=database.server_secret_key.decode(),
381+
)
382+
383+
target_id = client.add_target(
384+
name='x',
385+
width=1,
386+
image=high_quality_image,
387+
)
388+
389+
report = client.get_target_summary_report(target_id=target_id)
390+
assert report['status'] == 'processing'
391+
with pytest.raises(timeout_decorator.TimeoutError):
392+
client.wait_for_target_processed(
393+
target_id=target_id,
394+
timeout_seconds=0.1,
395+
)
396+
397+
client.wait_for_target_processed(
398+
target_id=target_id,
399+
timeout_seconds=0.5,
400+
)
401+
report = client.get_target_summary_report(target_id=target_id)
402+
assert report['status'] != 'processing'
403+
367404

368405
class TestGetDuplicateTargets:
369406
"""

0 commit comments

Comments
 (0)