refactor(downloader): build every progress speed string with format_speed

This commit is contained in:
imSp4rky
2026-12-04 15:50:22 -07:00
parent cc40deeb5c
commit a788f4d7b2
3 changed files with 23 additions and 9 deletions
@@ -1,9 +1,14 @@
import itertools
import math
import sys
import threading
import time
import types
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import pytest
import requests as rq
from rich import filesize
from unshackle.core.constants import DOWNLOAD_CANCELLED
from unshackle.core.downloaders.requests import download
@@ -91,3 +96,13 @@ def test_a_cut_body_bounds_the_following_requests(tmp_path, server):
assert spans and all(int(end) - int(start) + 1 <= CHUNK for start, end in spans)
# the bounded requests are cheap because they share one pooled connection
assert server.connections < len(server.ranges)
def test_each_speed_line_is_the_window_rate_rounded_up(tmp_path, server, monkeypatch):
clock = itertools.count(step=11.0)
monkeypatch.setattr(downloader, "time", types.SimpleNamespace(**{**vars(time), "monotonic": lambda: next(clock)}))
events = list(download(url=server.url, save_path=tmp_path / "0.mp4", session=rq.Session()))
pairs = [(events[i - 1]["advance"], e["downloaded"]) for i, e in enumerate(events) if "downloaded" in e]
assert any(n % 11 for n, _ in pairs)
assert all(text == f"{filesize.decimal(math.ceil(n / 11))}/s" for n, text in pairs)