|
| 1 | +""" |
| 2 | +Tests for configurable timeout and retry settings (issue #5). |
| 3 | +
|
| 4 | +Covers: |
| 5 | +* shade.timeout and shade.max_retries module-level settings |
| 6 | +* ShadeClient/Gateway per-instance overrides |
| 7 | +* Validation of out-of-range values |
| 8 | +* Timeout passed through to the HTTP transport layer |
| 9 | +* max_retries=0 disables retries entirely |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | +from unittest.mock import patch |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +import shade |
| 19 | +from shade import Gateway, ShadeClient |
| 20 | +from shade import config as _config |
| 21 | +from shade.config import validate_client_settings |
| 22 | +from shade.errors import RateLimitError |
| 23 | +from shade.http import SyncHTTPClient |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def _reset_client_settings(): |
| 28 | + original_timeout = _config.timeout |
| 29 | + original_max_retries = _config.max_retries |
| 30 | + yield |
| 31 | + _config.timeout = original_timeout |
| 32 | + _config.max_retries = original_max_retries |
| 33 | + |
| 34 | + |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +# Validation |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | + |
| 39 | +class TestValidateClientSettings: |
| 40 | + @pytest.mark.parametrize("timeout", [0, -1.0, -0.1]) |
| 41 | + def test_non_positive_timeout_raises(self, timeout): |
| 42 | + with pytest.raises(ValueError, match="timeout must be greater than 0"): |
| 43 | + validate_client_settings(timeout, 3) |
| 44 | + |
| 45 | + @pytest.mark.parametrize("max_retries", [-1, 11, 100]) |
| 46 | + def test_out_of_range_max_retries_raises(self, max_retries): |
| 47 | + with pytest.raises(ValueError, match="max_retries must be between 0 and 10"): |
| 48 | + validate_client_settings(30.0, max_retries) |
| 49 | + |
| 50 | + def test_boundary_values_are_valid(self): |
| 51 | + validate_client_settings(0.1, 0) |
| 52 | + validate_client_settings(30.0, 10) |
| 53 | + |
| 54 | + |
| 55 | +# --------------------------------------------------------------------------- |
| 56 | +# Module-level shade.timeout / shade.max_retries |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | + |
| 59 | +class TestModuleLevelClientSettings: |
| 60 | + def test_defaults(self): |
| 61 | + assert shade.timeout == 30.0 |
| 62 | + assert shade.max_retries == 3 |
| 63 | + |
| 64 | + def test_assignment_is_readable(self): |
| 65 | + shade.timeout = 15.0 |
| 66 | + shade.max_retries = 5 |
| 67 | + assert shade.timeout == 15.0 |
| 68 | + assert shade.max_retries == 5 |
| 69 | + |
| 70 | + def test_assignment_updates_config(self): |
| 71 | + shade.timeout = 12.0 |
| 72 | + shade.max_retries = 2 |
| 73 | + assert _config.timeout == 12.0 |
| 74 | + assert _config.max_retries == 2 |
| 75 | + |
| 76 | + def test_used_when_no_per_client_override(self): |
| 77 | + shade.timeout = 12.0 |
| 78 | + shade.max_retries = 2 |
| 79 | + client = ShadeClient(api_key="test-key") |
| 80 | + assert client._http.timeout == 12.0 |
| 81 | + assert client._http.max_retries == 2 |
| 82 | + assert client._async_http.timeout == 12.0 |
| 83 | + assert client._async_http.max_retries == 2 |
| 84 | + |
| 85 | + |
| 86 | +# --------------------------------------------------------------------------- |
| 87 | +# Per-client overrides |
| 88 | +# --------------------------------------------------------------------------- |
| 89 | + |
| 90 | +class TestPerClientSettings: |
| 91 | + def test_timeout_override(self): |
| 92 | + shade.timeout = 30.0 |
| 93 | + client = ShadeClient(api_key="test-key", timeout=5.0) |
| 94 | + assert client._http.timeout == 5.0 |
| 95 | + assert client._async_http.timeout == 5.0 |
| 96 | + |
| 97 | + def test_max_retries_override(self): |
| 98 | + shade.max_retries = 3 |
| 99 | + client = ShadeClient(api_key="test-key", max_retries=0) |
| 100 | + assert client._http.max_retries == 0 |
| 101 | + assert client._async_http.max_retries == 0 |
| 102 | + |
| 103 | + def test_per_client_beats_module_level(self): |
| 104 | + shade.timeout = 30.0 |
| 105 | + shade.max_retries = 3 |
| 106 | + client = ShadeClient(api_key="test-key", timeout=5.0, max_retries=1) |
| 107 | + assert client._http.timeout == 5.0 |
| 108 | + assert client._http.max_retries == 1 |
| 109 | + |
| 110 | + def test_invalid_timeout_on_client_raises(self): |
| 111 | + with pytest.raises(ValueError, match="timeout must be greater than 0"): |
| 112 | + ShadeClient(api_key="test-key", timeout=-1.0) |
| 113 | + |
| 114 | + def test_invalid_max_retries_on_client_raises(self): |
| 115 | + with pytest.raises(ValueError, match="max_retries must be between 0 and 10"): |
| 116 | + ShadeClient(api_key="test-key", max_retries=11) |
| 117 | + |
| 118 | + |
| 119 | +# --------------------------------------------------------------------------- |
| 120 | +# Acceptance criteria: timeout and retry behaviour |
| 121 | +# --------------------------------------------------------------------------- |
| 122 | + |
| 123 | +class TestTimeoutBehaviour: |
| 124 | + def test_shade_client_timeout_passed_to_urlopen(self): |
| 125 | + client = SyncHTTPClient( |
| 126 | + base_url="https://api.example.com", |
| 127 | + api_key="test-key", |
| 128 | + timeout=5.0, |
| 129 | + ) |
| 130 | + |
| 131 | + with patch.object(client, "_execute") as mock_execute: |
| 132 | + mock_execute.return_value = (200, {}, b'{"ok": true}') |
| 133 | + client.request("GET", "/payments") |
| 134 | + |
| 135 | + mock_execute.assert_called_once() |
| 136 | + req = mock_execute.call_args[0][0] |
| 137 | + with patch("urllib.request.urlopen") as mock_urlopen: |
| 138 | + mock_urlopen.return_value.__enter__.return_value.status = 200 |
| 139 | + mock_urlopen.return_value.__enter__.return_value.headers = {} |
| 140 | + mock_urlopen.return_value.__enter__.return_value.read.return_value = b"{}" |
| 141 | + client._execute(req) |
| 142 | + mock_urlopen.assert_called_once_with(req, timeout=5.0) |
| 143 | + |
| 144 | + |
| 145 | +class TestMaxRetriesBehaviour: |
| 146 | + def _fake_429_body(self) -> bytes: |
| 147 | + return json.dumps({"error": {"message": "rate limit"}}).encode() |
| 148 | + |
| 149 | + def test_max_retries_zero_disables_retries(self): |
| 150 | + client = SyncHTTPClient( |
| 151 | + base_url="https://api.example.com", |
| 152 | + api_key="test-key", |
| 153 | + max_retries=0, |
| 154 | + ) |
| 155 | + |
| 156 | + def fake_execute(req): |
| 157 | + return 429, {"Retry-After": "3"}, self._fake_429_body() |
| 158 | + |
| 159 | + with patch.object(client, "_execute", side_effect=fake_execute), patch( |
| 160 | + "time.sleep" |
| 161 | + ) as mock_sleep: |
| 162 | + with pytest.raises(RateLimitError): |
| 163 | + client.request("POST", "/payments", {}) |
| 164 | + |
| 165 | + mock_sleep.assert_not_called() |
| 166 | + |
| 167 | + def test_shade_client_max_retries_zero_disables_retries(self): |
| 168 | + client = ShadeClient(api_key="test-key", max_retries=0) |
| 169 | + |
| 170 | + def fake_execute(req): |
| 171 | + return 429, {"Retry-After": "3"}, self._fake_429_body() |
| 172 | + |
| 173 | + with patch.object(client._http, "_execute", side_effect=fake_execute), patch( |
| 174 | + "time.sleep" |
| 175 | + ) as mock_sleep: |
| 176 | + with pytest.raises(RateLimitError): |
| 177 | + client._http.request("POST", "/payments", {}) |
| 178 | + |
| 179 | + mock_sleep.assert_not_called() |
| 180 | + |
| 181 | + |
| 182 | +class TestShadeClientAlias: |
| 183 | + def test_shade_client_is_gateway(self): |
| 184 | + assert ShadeClient is Gateway |
0 commit comments