mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-16 14:05:10 +00:00
refactor(tests): drop leading underscores from test helper functions
Completes the core underscore sweep for the test files this branch added; make_ctx/make_track/seg_urls avoid shadowing locals.
This commit is contained in:
@@ -30,7 +30,7 @@ WINDOW = 10.0
|
||||
DT = 0.5
|
||||
|
||||
|
||||
def _fill_window(c: AdaptiveWorkerController, rate: float, now: float) -> float:
|
||||
def fill_window(c: AdaptiveWorkerController, rate: float, now: float) -> float:
|
||||
"""Record ``WINDOW`` seconds of samples at ``rate`` bytes/sec ending on the 0.5s grid.
|
||||
|
||||
Records a sample at ``now`` and every 0.5s up to ``now + WINDOW`` so the oldest kept
|
||||
@@ -44,7 +44,7 @@ def _fill_window(c: AdaptiveWorkerController, rate: float, now: float) -> float:
|
||||
return now
|
||||
|
||||
|
||||
def _run_tick(c: AdaptiveWorkerController, rate: float, now: float, errors: int = 0) -> tuple[int, float]:
|
||||
def run_tick(c: AdaptiveWorkerController, rate: float, now: float, errors: int = 0) -> tuple[int, float]:
|
||||
"""Advance one tick recording samples at ``rate``, optionally inject ``errors``, then update."""
|
||||
end = now + TICK
|
||||
while now < end - 1e-9:
|
||||
@@ -78,11 +78,11 @@ def test_ramps_up_while_speed_improves() -> None:
|
||||
now = 0.0
|
||||
c.update(now) # arm
|
||||
rate = 1_000_000.0
|
||||
now = _fill_window(c, rate, now)
|
||||
now = fill_window(c, rate, now)
|
||||
targets = [c.update(now)] # first evaluation: slow-start doubles 6 -> 12
|
||||
for _ in range(6):
|
||||
rate *= 2 # sustained >10% windowed gain each tick
|
||||
target, now = _run_tick(c, rate, now)
|
||||
target, now = run_tick(c, rate, now)
|
||||
targets.append(target)
|
||||
assert targets == sorted(targets) # monotonic non-decreasing
|
||||
assert targets[-1] == 16 # climbed to the cap
|
||||
@@ -94,13 +94,13 @@ def test_reverts_on_plateau_and_ends_slow_start() -> None:
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
rate = 1_000_000.0
|
||||
now = _fill_window(c, rate, now)
|
||||
now = fill_window(c, rate, now)
|
||||
assert c.update(now) == 12 # first increase slow-starts 6 -> 12
|
||||
# hold the rate flat: <10% gain -> the tick reverts the increase to its restore point
|
||||
target, now = _run_tick(c, rate, now)
|
||||
target, now = run_tick(c, rate, now)
|
||||
assert target == 6
|
||||
# slow-start is over: the next successful probe is additive (+2), not a double
|
||||
target, now = _run_tick(c, rate * 4, now)
|
||||
target, now = run_tick(c, rate * 4, now)
|
||||
assert target == 8
|
||||
|
||||
|
||||
@@ -108,17 +108,17 @@ def test_halves_and_cooldown_on_error_burst() -> None:
|
||||
c = AdaptiveWorkerController(cap=16, tick=TICK, window=WINDOW)
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
now = _fill_window(c, 1_000_000.0, now)
|
||||
now = fill_window(c, 1_000_000.0, now)
|
||||
for _ in range(3):
|
||||
c.record_error(now)
|
||||
assert c.update(now) == 3 # 3 errors -> multiplicative decrease 6 // 2
|
||||
|
||||
# cooldown tick holds even with improving throughput
|
||||
target, now = _run_tick(c, 2_000_000.0, now)
|
||||
target, now = run_tick(c, 2_000_000.0, now)
|
||||
assert target == 3
|
||||
|
||||
# cooldown consumed -> climbing resumes
|
||||
target, now = _run_tick(c, 4_000_000.0, now)
|
||||
target, now = run_tick(c, 4_000_000.0, now)
|
||||
assert target == 5
|
||||
|
||||
|
||||
@@ -128,11 +128,11 @@ def test_clamps_to_bounds_and_never_exceeds_cap() -> None:
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
rate = 1_000_000.0
|
||||
now = _fill_window(c, rate, now)
|
||||
now = fill_window(c, rate, now)
|
||||
seen = [c.update(now)]
|
||||
for _ in range(10):
|
||||
rate *= 2
|
||||
target, now = _run_tick(c, rate, now)
|
||||
target, now = run_tick(c, rate, now)
|
||||
seen.append(target)
|
||||
assert all(2 <= s <= 4 for s in seen)
|
||||
assert max(seen) == 4 # never exceeds the cap
|
||||
@@ -142,7 +142,7 @@ def test_error_burst_floors_at_min() -> None:
|
||||
c = AdaptiveWorkerController(cap=16, start=2, tick=TICK, window=WINDOW)
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
now = _fill_window(c, 1_000_000.0, now)
|
||||
now = fill_window(c, 1_000_000.0, now)
|
||||
for _ in range(5):
|
||||
c.record_error(now)
|
||||
assert c.update(now) == 2 # max(ADAPTIVE_MIN, 2 // 2)
|
||||
@@ -153,7 +153,7 @@ def test_tail_guard_suppresses_increase() -> None:
|
||||
c = AdaptiveWorkerController(cap=16, tick=TICK, window=WINDOW)
|
||||
now = 0.0
|
||||
c.update(now) # arm at target 6
|
||||
now = _fill_window(c, 1_000_000.0, now)
|
||||
now = fill_window(c, 1_000_000.0, now)
|
||||
assert c.update(now, 3) == 6 # 3 < target 6 -> holds at 6 rather than climbing to 8
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ def test_tail_guard_none_path_unchanged() -> None:
|
||||
c = AdaptiveWorkerController(cap=16, tick=TICK, window=WINDOW)
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
now = _fill_window(c, 1_000_000.0, now)
|
||||
now = fill_window(c, 1_000_000.0, now)
|
||||
assert c.update(now) == 12 # first evaluation slow-starts 6 -> 12
|
||||
|
||||
|
||||
@@ -171,7 +171,7 @@ def test_tail_guard_still_halves_on_error_burst() -> None:
|
||||
c = AdaptiveWorkerController(cap=16, tick=TICK, window=WINDOW)
|
||||
now = 0.0
|
||||
c.update(now)
|
||||
now = _fill_window(c, 1_000_000.0, now)
|
||||
now = fill_window(c, 1_000_000.0, now)
|
||||
for _ in range(3):
|
||||
c.record_error(now)
|
||||
assert c.update(now, 2) == 3 # guard active (2 < 6) but 3 errors -> 6 // 2
|
||||
|
||||
@@ -76,7 +76,7 @@ def server():
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clear_cancel():
|
||||
def clear_cancel():
|
||||
was_set = DOWNLOAD_CANCELLED.is_set()
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
yield
|
||||
@@ -86,16 +86,16 @@ def _clear_cancel():
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
|
||||
def _base(srv) -> str:
|
||||
def base(srv) -> str:
|
||||
return f"http://127.0.0.1:{srv.server_address[1]}/"
|
||||
|
||||
|
||||
def _collapse_mpd(srv) -> str:
|
||||
def collapse_mpd(srv) -> str:
|
||||
# SegmentList on one media.mp4: init is bytes [0, INIT_LEN), media ranges cover the rest
|
||||
# contiguously -> collapsible_single_url returns the URL and download_track collapses.
|
||||
return f"""<?xml version="1.0"?>
|
||||
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
|
||||
<BaseURL>{_base(srv)}</BaseURL>
|
||||
<BaseURL>{base(srv)}</BaseURL>
|
||||
<Period id="0">
|
||||
<AdaptationSet contentType="video" mimeType="video/mp4" lang="en">
|
||||
<Representation id="v0" codecs="avc1.640028" bandwidth="1000000" width="1920" height="1080">
|
||||
@@ -111,11 +111,11 @@ def _collapse_mpd(srv) -> str:
|
||||
</MPD>"""
|
||||
|
||||
|
||||
def _mixed_mpd(srv) -> str:
|
||||
def mixed_mpd(srv) -> str:
|
||||
# Two SegmentURLs on distinct paths -> mixed URLs -> predicate False -> segmented path.
|
||||
return f"""<?xml version="1.0"?>
|
||||
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" type="static" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
|
||||
<BaseURL>{_base(srv)}</BaseURL>
|
||||
<BaseURL>{base(srv)}</BaseURL>
|
||||
<Period id="0">
|
||||
<AdaptationSet contentType="video" mimeType="video/mp4" lang="en">
|
||||
<Representation id="v0" codecs="avc1.640028" bandwidth="1000000" width="1920" height="1080">
|
||||
@@ -130,11 +130,11 @@ def _mixed_mpd(srv) -> str:
|
||||
</MPD>"""
|
||||
|
||||
|
||||
def _track(mpd: str, srv):
|
||||
return DASH.from_text(mpd, url=f"{_base(srv)}manifest.mpd").to_tracks(language="en").videos[0]
|
||||
def make_track(mpd: str, srv):
|
||||
return DASH.from_text(mpd, url=f"{base(srv)}manifest.mpd").to_tracks(language="en").videos[0]
|
||||
|
||||
|
||||
def _ctx(tmp_path, name="out.mp4"):
|
||||
def make_ctx(tmp_path, name="out.mp4"):
|
||||
return DownloadContext(
|
||||
save_path=tmp_path / name,
|
||||
save_dir=tmp_path / "v0_segments",
|
||||
@@ -144,8 +144,8 @@ def _ctx(tmp_path, name="out.mp4"):
|
||||
|
||||
def test_collapse_downloads_parent_whole_in_one_direct_request(server, tmp_path):
|
||||
server.routes["/media.mp4"] = PARENT
|
||||
track = _track(_collapse_mpd(server), server)
|
||||
ctx = _ctx(tmp_path, "collapsed.mp4")
|
||||
track = make_track(collapse_mpd(server), server)
|
||||
ctx = make_ctx(tmp_path, "collapsed.mp4")
|
||||
|
||||
DASH.download_track(track, ctx)
|
||||
|
||||
@@ -166,8 +166,8 @@ def test_non_collapse_takes_segmented_merge_path(server, tmp_path):
|
||||
server.routes["/init.bin"] = INIT2
|
||||
server.routes["/part0.bin"] = PART0
|
||||
server.routes["/part1.bin"] = PART1
|
||||
track = _track(_mixed_mpd(server), server)
|
||||
ctx = _ctx(tmp_path, "merged.mp4")
|
||||
track = make_track(mixed_mpd(server), server)
|
||||
ctx = make_ctx(tmp_path, "merged.mp4")
|
||||
|
||||
DASH.download_track(track, ctx)
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ from requests import Session
|
||||
dl = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
|
||||
|
||||
def _consume(gen) -> list:
|
||||
def consume(gen) -> list:
|
||||
return list(gen)
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ class _FaultServer:
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _fast_retries(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
def fast_retries(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(dl, "RETRY_WAIT", 0)
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ def test_truncated_segment_is_not_finalized(tmp_path: Path, monkeypatch: pytest.
|
||||
with _FaultServer(body) as srv:
|
||||
srv.truncate_first = 99 # every attempt under-delivers
|
||||
with pytest.raises(Exception):
|
||||
_consume(dl.download(url=srv.url, save_path=save, session=Session(), segmented=True))
|
||||
consume(dl.download(url=srv.url, save_path=save, session=Session(), segmented=True))
|
||||
assert not save.exists(), "truncated segment must not be finalized as complete"
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ def test_interrupted_download_resumes_from_bytes_written(tmp_path: Path) -> None
|
||||
save = tmp_path / "file.bin"
|
||||
with _FaultServer(body) as srv:
|
||||
srv.truncate_first = 1 # first attempt delivers half then closes; retry completes
|
||||
_consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
assert save.read_bytes() == body
|
||||
# attempt 1 had no Range; attempt 2 must resume partway in, not from 0 or past the end
|
||||
assert srv.ranges[0] is None
|
||||
@@ -139,7 +139,7 @@ def test_resumes_from_preexisting_dev_partial(tmp_path: Path) -> None:
|
||||
tmp = save.with_name("file.bin.!dev")
|
||||
tmp.write_bytes(body[:1500]) # correct prefix the interrupted run flushed to disk
|
||||
with _FaultServer(body) as srv:
|
||||
_consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
assert save.read_bytes() == body
|
||||
assert not tmp.exists()
|
||||
assert srv.ranges[0] == "bytes=1500-", f"expected resume from 1500, got {srv.ranges[0]}"
|
||||
@@ -198,7 +198,7 @@ def test_byte_range_slice_segments_never_probed_or_boosted(tmp_path: Path, monke
|
||||
url = f"http://{host}:{port}/media.mp4"
|
||||
urls = [{"url": url, "headers": {"Range": f"bytes={i * seg}-{(i + 1) * seg - 1}"}} for i in range(nseg)]
|
||||
try:
|
||||
_consume(
|
||||
consume(
|
||||
dl.requests(
|
||||
urls=urls,
|
||||
output_dir=tmp_path,
|
||||
@@ -226,6 +226,6 @@ def test_oversized_resume_416_restarts_clean(tmp_path: Path) -> None:
|
||||
tmp = save.with_name(f"{save.name}.!dev")
|
||||
tmp.write_bytes(b"X" * 4096) # poisoned partial, bigger than the server's body
|
||||
with _FaultServer(body) as srv:
|
||||
_consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
consume(dl.download(url=srv.url, save_path=save, session=Session()))
|
||||
assert save.read_bytes() == body
|
||||
assert srv.ranges[0] is not None and "4096" in srv.ranges[0] # first request over-ranged → 416
|
||||
|
||||
Reference in New Issue
Block a user