|
13 | 13 | import shlex |
14 | 14 | import subprocess |
15 | 15 | import sys |
| 16 | +import time |
| 17 | +from datetime import datetime, timezone |
| 18 | +from http.server import BaseHTTPRequestHandler, HTTPServer |
16 | 19 | from pathlib import Path |
| 20 | +from threading import Thread |
17 | 21 | from types import SimpleNamespace |
18 | 22 |
|
19 | 23 | import pytest |
@@ -128,6 +132,158 @@ def test_existing_hf_download_accepts_matching_hash(tmp_path, monkeypatch): |
128 | 132 | ) == str(tmp_path) |
129 | 133 |
|
130 | 134 |
|
| 135 | +@pytest.mark.parametrize( |
| 136 | + "headers, expected_wait", |
| 137 | + [ |
| 138 | + ( |
| 139 | + { |
| 140 | + "RateLimit": '"api";r=0;t=36', |
| 141 | + "RateLimit-Policy": '"fixed window";"api";q=500;w=300', |
| 142 | + }, |
| 143 | + 37, |
| 144 | + ), |
| 145 | + ({"Retry-After": "36"}, 37), |
| 146 | + ({"Retry-After": "Mon, 19 Oct 2026 17:50:36 GMT"}, 37), |
| 147 | + ({"RateLimit": '"api";r=0;t=36', "Retry-After": "50"}, 51), |
| 148 | + ({"RateLimit": '"api";r=0;t=36', "Retry-After": "invalid"}, 37), |
| 149 | + ({"RateLimit": '"api";r=10;t=900', "Retry-After": "36"}, 37), |
| 150 | + ], |
| 151 | +) |
| 152 | +def test_hf_snapshot_retries_rate_limited_repo_tree( |
| 153 | + tmp_path, monkeypatch, capsys, headers, expected_wait |
| 154 | +): |
| 155 | + from huggingface_hub import HfApi |
| 156 | + |
| 157 | + module = load_existing_module("examples/models/llama/hf_download.py") |
| 158 | + requests = [] |
| 159 | + calls = [] |
| 160 | + waits = [] |
| 161 | + monkeypatch.setenv("NO_PROXY", "127.0.0.1,localhost") |
| 162 | + monkeypatch.setattr(time, "sleep", waits.append) |
| 163 | + monkeypatch.setattr( |
| 164 | + time, |
| 165 | + "time", |
| 166 | + lambda: datetime(2026, 10, 19, 17, 50, tzinfo=timezone.utc).timestamp(), |
| 167 | + ) |
| 168 | + |
| 169 | + class Handler(BaseHTTPRequestHandler): |
| 170 | + def do_GET(self): |
| 171 | + requests.append(self.path) |
| 172 | + self.send_response(429 if len(requests) == 1 else 200) |
| 173 | + if len(requests) == 1: |
| 174 | + for name, value in headers.items(): |
| 175 | + self.send_header(name, value) |
| 176 | + self.send_header("Content-Type", "application/json") |
| 177 | + self.end_headers() |
| 178 | + self.wfile.write( |
| 179 | + b'{"error": "Rate limited"}' if len(requests) == 1 else b"[]" |
| 180 | + ) |
| 181 | + |
| 182 | + def log_message(self, *args): |
| 183 | + pass |
| 184 | + |
| 185 | + server = HTTPServer(("127.0.0.1", 0), Handler) |
| 186 | + thread = Thread(target=server.serve_forever) |
| 187 | + thread.start() |
| 188 | + api = HfApi(endpoint=f"http://127.0.0.1:{server.server_port}") |
| 189 | + |
| 190 | + def snapshot(**kwargs): |
| 191 | + calls.append(kwargs) |
| 192 | + list( |
| 193 | + api.list_repo_tree( |
| 194 | + kwargs["repo_id"], |
| 195 | + revision=kwargs["revision"], |
| 196 | + token=kwargs["token"], |
| 197 | + recursive=True, |
| 198 | + ) |
| 199 | + ) |
| 200 | + return str(tmp_path) |
| 201 | + |
| 202 | + monkeypatch.setattr(module, "snapshot_download", snapshot) |
| 203 | + try: |
| 204 | + assert module.download_hf_snapshot("test/model", "r" * 40) == str(tmp_path) |
| 205 | + finally: |
| 206 | + server.shutdown() |
| 207 | + server.server_close() |
| 208 | + thread.join() |
| 209 | + assert len(requests) == 2 and requests[0] == requests[1] |
| 210 | + assert ( |
| 211 | + calls |
| 212 | + == [ |
| 213 | + { |
| 214 | + "repo_id": "test/model", |
| 215 | + "revision": "r" * 40, |
| 216 | + "allow_patterns": ["*.safetensors", "*.json"], |
| 217 | + "token": False, |
| 218 | + } |
| 219 | + ] |
| 220 | + * 2 |
| 221 | + ) |
| 222 | + assert waits == [expected_wait] |
| 223 | + output = capsys.readouterr() |
| 224 | + assert output.out == "" |
| 225 | + assert str(expected_wait) in output.err |
| 226 | + |
| 227 | + |
| 228 | +@pytest.mark.parametrize( |
| 229 | + "headers", [{}, {"Retry-After": "invalid"}, {"RateLimit": '"api";r=10;t=900'}] |
| 230 | +) |
| 231 | +def test_hf_snapshot_rate_limit_retry_exhaustion(monkeypatch, headers): |
| 232 | + import httpx |
| 233 | + from huggingface_hub.errors import HfHubHTTPError |
| 234 | + |
| 235 | + module = load_existing_module("examples/models/llama/hf_download.py") |
| 236 | + response = httpx.Response( |
| 237 | + 429, |
| 238 | + headers=headers, |
| 239 | + request=httpx.Request("GET", "https://example.com"), |
| 240 | + ) |
| 241 | + error = HfHubHTTPError("Rate limited", response=response) |
| 242 | + calls = [] |
| 243 | + waits = [] |
| 244 | + |
| 245 | + def rate_limited(**kwargs): |
| 246 | + calls.append(kwargs) |
| 247 | + raise error |
| 248 | + |
| 249 | + monkeypatch.setattr(module, "snapshot_download", rate_limited) |
| 250 | + monkeypatch.setattr(time, "sleep", waits.append) |
| 251 | + with pytest.raises(HfHubHTTPError) as raised: |
| 252 | + module.download_hf_snapshot("test/model", "r" * 40) |
| 253 | + assert raised.value is error |
| 254 | + assert len(calls) == 6 |
| 255 | + assert waits == [60, 120, 240, 300, 300] |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.parametrize("status", [401, 404, 503, None]) |
| 259 | +def test_hf_snapshot_does_not_retry_other_http_errors(monkeypatch, status): |
| 260 | + import httpx |
| 261 | + from huggingface_hub.errors import HfHubHTTPError |
| 262 | + |
| 263 | + module = load_existing_module("examples/models/llama/hf_download.py") |
| 264 | + response = httpx.Response( |
| 265 | + status or 429, |
| 266 | + request=httpx.Request("GET", "https://example.com"), |
| 267 | + ) |
| 268 | + error = HfHubHTTPError("Download failed", response=response) |
| 269 | + if status is None: |
| 270 | + error.response = None |
| 271 | + calls = [] |
| 272 | + waits = [] |
| 273 | + |
| 274 | + def failed(**kwargs): |
| 275 | + calls.append(kwargs) |
| 276 | + raise error |
| 277 | + |
| 278 | + monkeypatch.setattr(module, "snapshot_download", failed) |
| 279 | + monkeypatch.setattr(time, "sleep", waits.append) |
| 280 | + with pytest.raises(HfHubHTTPError) as raised: |
| 281 | + module.download_hf_snapshot("test/model", "r" * 40) |
| 282 | + assert raised.value is error |
| 283 | + assert len(calls) == 1 |
| 284 | + assert waits == [] |
| 285 | + |
| 286 | + |
131 | 287 | def test_snapshot_cli_exposes_pinned_download_and_checksum( |
132 | 288 | tmp_path, monkeypatch, capsys |
133 | 289 | ): |
|
0 commit comments