|
| 1 | +"""Tests for timeout and retry functionality in ArgusClient.""" |
| 2 | +from unittest.mock import patch |
| 3 | +from uuid import uuid4 |
| 4 | + |
| 5 | +from argus.client.base import ArgusClient |
| 6 | +from argus.client.sct.client import ArgusSCTClient |
| 7 | +from argus.client.generic.client import ArgusGenericClient |
| 8 | +from argus.client.driver_matrix_tests.client import ArgusDriverMatrixClient |
| 9 | +from argus.client.sirenada.client import ArgusSirenadaClient |
| 10 | + |
| 11 | + |
| 12 | +def test_argus_client_default_timeout(): |
| 13 | + """Test that ArgusClient uses default timeout of 60 seconds.""" |
| 14 | + client = ArgusClient( |
| 15 | + auth_token="test_token", |
| 16 | + base_url="https://test.example.com" |
| 17 | + ) |
| 18 | + assert client._timeout == 60 |
| 19 | + |
| 20 | + |
| 21 | +def test_argus_client_custom_timeout(): |
| 22 | + """Test that ArgusClient accepts custom timeout.""" |
| 23 | + client = ArgusClient( |
| 24 | + auth_token="test_token", |
| 25 | + base_url="https://test.example.com", |
| 26 | + timeout=120 |
| 27 | + ) |
| 28 | + assert client._timeout == 120 |
| 29 | + |
| 30 | + |
| 31 | +def test_argus_client_session_has_retry_adapter(): |
| 32 | + """Test that ArgusClient session is configured with retry adapter.""" |
| 33 | + client = ArgusClient( |
| 34 | + auth_token="test_token", |
| 35 | + base_url="https://test.example.com", |
| 36 | + max_retries=5 |
| 37 | + ) |
| 38 | + |
| 39 | + # Check that adapters are mounted |
| 40 | + assert "https://" in client.session.adapters |
| 41 | + assert "http://" in client.session.adapters |
| 42 | + |
| 43 | + # Check that the adapter has retry configuration |
| 44 | + adapter = client.session.get_adapter("https://test.example.com") |
| 45 | + assert adapter.max_retries.total == 5 |
| 46 | + assert adapter.max_retries.backoff_factor == 1 |
| 47 | + assert adapter.max_retries.status == 0 |
| 48 | + assert adapter.max_retries.status_forcelist == set() |
| 49 | + |
| 50 | + |
| 51 | +def test_get_request_uses_timeout(requests_mock): |
| 52 | + """Test that GET requests include timeout parameter.""" |
| 53 | + requests_mock.get( |
| 54 | + "https://test.example.com/api/v1/client/testrun/test-type/test-id/get", |
| 55 | + json={"status": "ok", "response": {}}, |
| 56 | + status_code=200 |
| 57 | + ) |
| 58 | + |
| 59 | + client = ArgusClient( |
| 60 | + auth_token="test_token", |
| 61 | + base_url="https://test.example.com", |
| 62 | + timeout=30 |
| 63 | + ) |
| 64 | + |
| 65 | + with patch.object(client.session, 'get', wraps=client.session.get) as mock_get: |
| 66 | + try: |
| 67 | + client.get( |
| 68 | + endpoint=ArgusClient.Routes.GET, |
| 69 | + location_params={"type": "test-type", "id": "test-id"} |
| 70 | + ) |
| 71 | + except Exception: |
| 72 | + pass # We're just checking the call arguments |
| 73 | + |
| 74 | + # Verify timeout was passed to the request |
| 75 | + assert mock_get.called |
| 76 | + call_kwargs = mock_get.call_args.kwargs |
| 77 | + assert 'timeout' in call_kwargs |
| 78 | + assert call_kwargs['timeout'] == 30 |
| 79 | + |
| 80 | + |
| 81 | +def test_post_request_uses_timeout(requests_mock): |
| 82 | + """Test that POST requests include timeout parameter.""" |
| 83 | + requests_mock.post( |
| 84 | + "https://test.example.com/api/v1/client/testrun/test-type/submit", |
| 85 | + json={"status": "ok"}, |
| 86 | + status_code=200 |
| 87 | + ) |
| 88 | + |
| 89 | + client = ArgusClient( |
| 90 | + auth_token="test_token", |
| 91 | + base_url="https://test.example.com", |
| 92 | + timeout=45 |
| 93 | + ) |
| 94 | + |
| 95 | + with patch.object(client.session, 'post', wraps=client.session.post) as mock_post: |
| 96 | + try: |
| 97 | + client.post( |
| 98 | + endpoint=ArgusClient.Routes.SUBMIT, |
| 99 | + location_params={"type": "test-type"}, |
| 100 | + body={"test": "data"} |
| 101 | + ) |
| 102 | + except Exception: |
| 103 | + pass # We're just checking the call arguments |
| 104 | + |
| 105 | + # Verify timeout was passed to the request |
| 106 | + assert mock_post.called |
| 107 | + call_kwargs = mock_post.call_args.kwargs |
| 108 | + assert 'timeout' in call_kwargs |
| 109 | + assert call_kwargs['timeout'] == 45 |
| 110 | + |
| 111 | + |
| 112 | +def test_retry_configuration_is_correct(): |
| 113 | + """Test that the retry adapter is correctly configured.""" |
| 114 | + client = ArgusClient( |
| 115 | + auth_token="test_token", |
| 116 | + base_url="https://test.example.com", |
| 117 | + max_retries=5 |
| 118 | + ) |
| 119 | + |
| 120 | + # Get the adapter |
| 121 | + adapter = client.session.get_adapter("https://test.example.com") |
| 122 | + |
| 123 | + # Verify retry configuration |
| 124 | + assert adapter.max_retries.total == 5 |
| 125 | + assert adapter.max_retries.backoff_factor == 1 |
| 126 | + assert adapter.max_retries.status == 0 |
| 127 | + assert adapter.max_retries.status_forcelist == set() |
| 128 | + assert "GET" in adapter.max_retries.allowed_methods |
| 129 | + assert "POST" not in adapter.max_retries.allowed_methods |
| 130 | + |
| 131 | + |
| 132 | +def test_sct_client_passes_timeout_and_retries(): |
| 133 | + """Test that ArgusSCTClient passes timeout and retry parameters to parent.""" |
| 134 | + run_id = uuid4() |
| 135 | + client = ArgusSCTClient( |
| 136 | + run_id=run_id, |
| 137 | + auth_token="test_token", |
| 138 | + base_url="https://test.example.com", |
| 139 | + timeout=90, |
| 140 | + max_retries=5 |
| 141 | + ) |
| 142 | + |
| 143 | + assert client._timeout == 90 |
| 144 | + adapter = client.session.get_adapter("https://test.example.com") |
| 145 | + assert adapter.max_retries.total == 5 |
| 146 | + |
| 147 | + |
| 148 | +def test_generic_client_passes_timeout_and_retries(): |
| 149 | + """Test that ArgusGenericClient passes timeout and retry parameters to parent.""" |
| 150 | + client = ArgusGenericClient( |
| 151 | + auth_token="test_token", |
| 152 | + base_url="https://test.example.com", |
| 153 | + timeout=75, |
| 154 | + max_retries=4 |
| 155 | + ) |
| 156 | + |
| 157 | + assert client._timeout == 75 |
| 158 | + adapter = client.session.get_adapter("https://test.example.com") |
| 159 | + assert adapter.max_retries.total == 4 |
| 160 | + |
| 161 | + |
| 162 | +def test_driver_matrix_client_passes_timeout_and_retries(): |
| 163 | + """Test that ArgusDriverMatrixClient passes timeout and retry parameters to parent.""" |
| 164 | + run_id = uuid4() |
| 165 | + client = ArgusDriverMatrixClient( |
| 166 | + run_id=run_id, |
| 167 | + auth_token="test_token", |
| 168 | + base_url="https://test.example.com", |
| 169 | + timeout=100, |
| 170 | + max_retries=2 |
| 171 | + ) |
| 172 | + |
| 173 | + assert client._timeout == 100 |
| 174 | + adapter = client.session.get_adapter("https://test.example.com") |
| 175 | + assert adapter.max_retries.total == 2 |
| 176 | + |
| 177 | + |
| 178 | +def test_sirenada_client_passes_timeout_and_retries(): |
| 179 | + """Test that ArgusSirenadaClient passes timeout and retry parameters to parent.""" |
| 180 | + client = ArgusSirenadaClient( |
| 181 | + auth_token="test_token", |
| 182 | + base_url="https://test.example.com", |
| 183 | + timeout=80, |
| 184 | + max_retries=6 |
| 185 | + ) |
| 186 | + |
| 187 | + assert client._timeout == 80 |
| 188 | + adapter = client.session.get_adapter("https://test.example.com") |
| 189 | + assert adapter.max_retries.total == 6 |
| 190 | + |
| 191 | + |
| 192 | +def test_client_uses_default_values_when_not_specified(): |
| 193 | + """Test that client uses defaults when timeout and retries not specified.""" |
| 194 | + run_id = uuid4() |
| 195 | + client = ArgusSCTClient( |
| 196 | + run_id=run_id, |
| 197 | + auth_token="test_token", |
| 198 | + base_url="https://test.example.com" |
| 199 | + ) |
| 200 | + |
| 201 | + # Should use default timeout of 60 and default retries of 3 |
| 202 | + assert client._timeout == 60 |
| 203 | + adapter = client.session.get_adapter("https://test.example.com") |
| 204 | + assert adapter.max_retries.total == 3 |
0 commit comments