|
1 | 1 | """Tests for the ``CloudRecoService`` querying functionality.""" |
2 | 2 |
|
| 3 | +import datetime |
3 | 4 | import io |
4 | 5 | import uuid |
5 | 6 | from typing import BinaryIO |
6 | 7 |
|
| 8 | +import pytest |
| 9 | +import requests |
| 10 | +from freezegun import freeze_time |
7 | 11 | from mock_vws import MockVWS |
8 | 12 | from mock_vws.database import VuforiaDatabase |
9 | 13 |
|
@@ -42,6 +46,52 @@ def test_match( |
42 | 46 | assert matching_target.target_id == target_id |
43 | 47 |
|
44 | 48 |
|
| 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 | + |
45 | 95 | class TestCustomBaseVWQURL: |
46 | 96 | """Tests for using a custom base VWQ URL.""" |
47 | 97 |
|
|
0 commit comments