Skip to content

Commit 69fac31

Browse files
Merge pull request #2845 from VWS-Python/adamtheturtle/test-default-timeout
Add tests for the default request timeout
2 parents 828ec2d + 2d64771 commit 69fac31

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

tests/test_query.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Tests for the ``CloudRecoService`` querying functionality."""
22

3+
import datetime
34
import io
45
import uuid
56
from typing import BinaryIO
67

8+
import pytest
9+
import requests
10+
from freezegun import freeze_time
711
from mock_vws import MockVWS
812
from mock_vws.database import VuforiaDatabase
913

@@ -42,6 +46,52 @@ def test_match(
4246
assert matching_target.target_id == target_id
4347

4448

49+
class TestDefaultRequestTimeout:
50+
"""Tests for the default request timeout."""
51+
52+
@staticmethod
53+
@pytest.mark.parametrize(
54+
argnames=("response_delay_seconds", "expect_timeout"),
55+
argvalues=[(29, False), (31, True)],
56+
)
57+
def test_default_timeout(
58+
image: io.BytesIO | BinaryIO,
59+
*,
60+
response_delay_seconds: int,
61+
expect_timeout: bool,
62+
) -> None:
63+
"""At 29 seconds there is no error; at 31 seconds there is a
64+
timeout.
65+
"""
66+
with (
67+
freeze_time() as frozen_datetime,
68+
MockVWS(
69+
response_delay_seconds=response_delay_seconds,
70+
sleep_fn=lambda seconds: (
71+
frozen_datetime.tick(
72+
delta=datetime.timedelta(seconds=seconds),
73+
),
74+
None,
75+
)[1],
76+
) as mock,
77+
):
78+
database = VuforiaDatabase()
79+
mock.add_database(database=database)
80+
cloud_reco_client = CloudRecoService(
81+
client_access_key=database.client_access_key,
82+
client_secret_key=database.client_secret_key,
83+
)
84+
85+
if expect_timeout:
86+
with pytest.raises(
87+
expected_exception=requests.exceptions.Timeout,
88+
):
89+
cloud_reco_client.query(image=image)
90+
else:
91+
matches = cloud_reco_client.query(image=image)
92+
assert not matches
93+
94+
4595
class TestCustomBaseVWQURL:
4696
"""Tests for using a custom base VWQ URL."""
4797

tests/test_vws.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import BinaryIO
99

1010
import pytest
11+
import requests
1112
from freezegun import freeze_time
1213
from mock_vws import MockVWS
1314
from mock_vws.database import VuforiaDatabase
@@ -92,6 +93,63 @@ def test_add_two_targets(
9293
)
9394

9495

96+
class TestDefaultRequestTimeout:
97+
"""Tests for the default request timeout."""
98+
99+
@staticmethod
100+
@pytest.mark.parametrize(
101+
argnames=("response_delay_seconds", "expect_timeout"),
102+
argvalues=[(29, False), (31, True)],
103+
)
104+
def test_default_timeout(
105+
image: io.BytesIO | BinaryIO,
106+
*,
107+
response_delay_seconds: int,
108+
expect_timeout: bool,
109+
) -> None:
110+
"""At 29 seconds there is no error; at 31 seconds there is a
111+
timeout.
112+
"""
113+
with (
114+
freeze_time() as frozen_datetime,
115+
MockVWS(
116+
response_delay_seconds=response_delay_seconds,
117+
sleep_fn=lambda seconds: (
118+
frozen_datetime.tick(
119+
delta=datetime.timedelta(seconds=seconds),
120+
),
121+
None,
122+
)[1],
123+
) as mock,
124+
):
125+
database = VuforiaDatabase()
126+
mock.add_database(database=database)
127+
vws_client = VWS(
128+
server_access_key=database.server_access_key,
129+
server_secret_key=database.server_secret_key,
130+
)
131+
132+
if expect_timeout:
133+
with pytest.raises(
134+
expected_exception=requests.exceptions.Timeout,
135+
):
136+
vws_client.add_target(
137+
name="x",
138+
width=1,
139+
image=image,
140+
active_flag=True,
141+
application_metadata=None,
142+
)
143+
else:
144+
vws_client.add_target(
145+
name="x",
146+
width=1,
147+
image=image,
148+
active_flag=True,
149+
application_metadata=None,
150+
)
151+
152+
95153
class TestCustomBaseVWSURL:
96154
"""Tests for using a custom base VWS URL."""
97155

0 commit comments

Comments
 (0)