-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_http.py
More file actions
161 lines (112 loc) · 5.42 KB
/
test_http.py
File metadata and controls
161 lines (112 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import urllib.error
from pathlib import Path
import pytest
from aioresponses import aioresponses
from tardis_dev._http import create_session, reliable_download
@pytest.mark.asyncio
async def test_create_session_uses_requested_accept_encoding_and_omits_authorization_header_when_api_key_missing():
async with await create_session("", 5, "zstd, gzip") as session:
assert "Authorization" not in session.headers
assert session.headers["Accept-Encoding"] == "zstd, gzip"
@pytest.mark.asyncio
async def test_reliable_download_appends_zstd_extension_for_replay_cache(tmp_path: Path):
destination = tmp_path / "slice.json"
url = "https://example.com/data"
with aioresponses() as mocked:
mocked.get(url, body=b"payload", headers={"Content-Encoding": "zstd"})
async with await create_session("", 5) as session:
final_path = await reliable_download(session, url, str(destination), append_content_encoding_extension=True)
assert final_path.endswith(".zst")
assert Path(final_path).read_bytes() == b"payload"
assert not destination.exists()
@pytest.mark.asyncio
async def test_reliable_download_falls_back_to_gzip_extension_when_content_encoding_is_missing(tmp_path: Path):
destination = tmp_path / "slice.json"
url = "https://example.com/data"
with aioresponses() as mocked:
mocked.get(url, body=b"")
async with await create_session("", 5) as session:
final_path = await reliable_download(session, url, str(destination), append_content_encoding_extension=True)
assert final_path.endswith(".gz")
assert Path(final_path).read_bytes() == b""
assert not destination.exists()
@pytest.mark.asyncio
async def test_reliable_download_uses_node_backoff_for_500(tmp_path: Path, monkeypatch):
destination = tmp_path / "slice.json.gz"
url = "https://example.com/data"
delays = []
async def fake_sleep(delay: float) -> None:
delays.append(delay)
return None
monkeypatch.setattr("tardis_dev._http.asyncio.sleep", fake_sleep)
monkeypatch.setattr("tardis_dev._http.random.random", lambda: 0.0)
with aioresponses() as mocked:
mocked.get(url, status=500)
mocked.get(url, body=b"payload")
async with await create_session("", 5) as session:
await reliable_download(session, url, str(destination))
assert destination.read_bytes() == b"payload"
assert delays == [4.0]
@pytest.mark.asyncio
async def test_reliable_download_retries_iso_400_for_gz_with_retry_attempt_query(tmp_path: Path, monkeypatch):
destination = tmp_path / "dataset.csv.gz"
url = "https://example.com/dataset.csv.gz"
delays = []
async def fake_sleep(delay: float) -> None:
delays.append(delay)
return None
monkeypatch.setattr("tardis_dev._http.asyncio.sleep", fake_sleep)
monkeypatch.setattr("tardis_dev._http.random.random", lambda: 0.0)
with aioresponses() as mocked:
mocked.get(url, status=400, body="Please provide valid ISO 8601 format.")
mocked.get(f"{url}?retryAttempt=1", body=b"payload")
async with await create_session("", 5) as session:
await reliable_download(session, url, str(destination))
assert destination.read_bytes() == b"payload"
assert delays == [2.0]
@pytest.mark.asyncio
async def test_reliable_download_does_not_retry_400(tmp_path: Path, monkeypatch):
destination = tmp_path / "slice.json.gz"
url = "https://example.com/data"
async def no_sleep(_: float) -> None:
return None
monkeypatch.setattr("tardis_dev._http.asyncio.sleep", no_sleep)
with aioresponses() as mocked:
mocked.get(url, status=400, body="bad request")
async with await create_session("", 5) as session:
with pytest.raises(urllib.error.HTTPError) as exc_info:
await reliable_download(session, url, str(destination))
assert exc_info.value.code == 400
assert not destination.exists()
@pytest.mark.asyncio
async def test_reliable_download_uses_429_delay(tmp_path: Path, monkeypatch):
destination = tmp_path / "slice.json.gz"
url = "https://example.com/data"
delays = []
async def fake_sleep(delay: float) -> None:
delays.append(delay)
return None
monkeypatch.setattr("tardis_dev._http.asyncio.sleep", fake_sleep)
monkeypatch.setattr("tardis_dev._http.random.random", lambda: 0.0)
with aioresponses() as mocked:
mocked.get(url, status=429, body="rate limited")
mocked.get(url, body=b"payload")
async with await create_session("", 5) as session:
await reliable_download(session, url, str(destination))
assert destination.read_bytes() == b"payload"
assert delays == [62.0]
@pytest.mark.asyncio
async def test_reliable_download_cleans_temp_file_on_replace_error(tmp_path: Path, monkeypatch):
destination = tmp_path / "slice.json.gz"
url = "https://example.com/data"
def raising_replace(src: str, dst: str) -> None:
raise OSError("replace failed")
monkeypatch.setattr("tardis_dev._http.os.replace", raising_replace)
with aioresponses() as mocked:
mocked.get(url, body=b"payload")
async with await create_session("", 5) as session:
with pytest.raises(OSError):
await reliable_download(session, url, str(destination), max_attempts=1)
temp_files = list(tmp_path.glob("*.unconfirmed"))
assert not destination.exists()
assert temp_files == []