Skip to content

Commit 2d14379

Browse files
committed
fix replay fallback for missing content-encoding
1 parent e8b8d84 commit 2d14379

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

tardis_dev/_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async def _download(
118118
content_encoding = response.headers.get("Content-Encoding")
119119
if content_encoding == "zstd":
120120
final_path = f"{dest_path}.zst"
121-
elif content_encoding == "gzip":
121+
elif content_encoding is None or content_encoding == "gzip":
122122
final_path = f"{dest_path}.gz"
123123
else:
124124
raise urllib.error.HTTPError(

tests/test_http.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ async def test_reliable_download_appends_zstd_extension_for_replay_cache(tmp_pat
3030
assert not destination.exists()
3131

3232

33+
@pytest.mark.asyncio
34+
async def test_reliable_download_falls_back_to_gzip_extension_when_content_encoding_is_missing(tmp_path: Path):
35+
destination = tmp_path / "slice.json"
36+
url = "https://example.com/data"
37+
38+
with aioresponses() as mocked:
39+
mocked.get(url, body=b"")
40+
41+
async with await create_session("", 5) as session:
42+
final_path = await reliable_download(session, url, str(destination), append_content_encoding_extension=True)
43+
44+
assert final_path.endswith(".gz")
45+
assert Path(final_path).read_bytes() == b""
46+
assert not destination.exists()
47+
48+
3349
@pytest.mark.asyncio
3450
async def test_reliable_download_uses_node_backoff_for_500(tmp_path: Path, monkeypatch):
3551
destination = tmp_path / "slice.json.gz"

tests/test_replay.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
LIVE_REPLAY_EXCHANGE = "bitmex"
2121
LIVE_REPLAY_FROM = "2019-05-01T00:00:00.000Z"
2222
LIVE_REPLAY_TO = "2019-05-01T00:01:00.000Z"
23+
LIVE_EMPTY_REPLAY_EXCHANGE = "binance"
24+
LIVE_EMPTY_REPLAY_FROM = "2019-06-01T00:00:00.000Z"
25+
LIVE_EMPTY_REPLAY_TO = "2019-06-01T00:01:00.000Z"
2326

2427

2528
def _live_replay_filters():
2629
return [Channel("trade", ["BTCUSD"])]
2730

2831

32+
def _live_empty_replay_filters():
33+
return [Channel("trade", ["batpax"])]
34+
35+
2936
class _FakeSession:
3037
async def __aenter__(self):
3138
return object()
@@ -90,6 +97,23 @@ async def test_replay_auto_cleanup_removes_live_processed_slices(tmp_path: Path)
9097
assert not day_dir.exists()
9198

9299

100+
@pytest.mark.live
101+
@pytest.mark.asyncio
102+
async def test_replay_live_empty_slice_yields_no_messages(tmp_path: Path):
103+
cache_dir = tmp_path / "cache"
104+
results = []
105+
async for item in replay(
106+
exchange=LIVE_EMPTY_REPLAY_EXCHANGE,
107+
from_date=LIVE_EMPTY_REPLAY_FROM,
108+
to_date=LIVE_EMPTY_REPLAY_TO,
109+
filters=_live_empty_replay_filters(),
110+
cache_dir=str(cache_dir),
111+
):
112+
results.append(item)
113+
114+
assert results == []
115+
116+
93117
def test_replay_rejects_invalid_date_order():
94118
async def collect():
95119
async for _ in replay(exchange="bitmex", from_date="2019-06-02", to_date="2019-06-01"):

0 commit comments

Comments
 (0)