mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
fix(api): time the rate-limit window on the monotonic clock and make three flaky tests deterministic
This commit is contained in:
@@ -4,6 +4,7 @@ run that never failed, and the second run fetches and decrypts only what the fir
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import threading
|
||||
from collections import Counter
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
@@ -134,10 +135,15 @@ def part(parser, index: int) -> str:
|
||||
|
||||
|
||||
def fail_once(server, parser, make, ctx) -> None:
|
||||
"""Run a download that fails on segment FAIL_AT."""
|
||||
"""Run a download that fails on segment FAIL_AT, and wait for the pool workers its failure leaves running."""
|
||||
body = server.routes.pop(part(parser, FAIL_AT))
|
||||
before = set(threading.enumerate())
|
||||
with pytest.raises(requests.HTTPError):
|
||||
parser.download_track(make(), ctx)
|
||||
leaked = [thread for thread in set(threading.enumerate()) - before if thread.name.startswith("ThreadPoolExecutor")]
|
||||
for thread in leaked:
|
||||
thread.join(10)
|
||||
assert not any(thread.is_alive() for thread in leaked)
|
||||
server.routes[part(parser, FAIL_AT)] = body
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
@@ -258,7 +264,9 @@ def test_kept_decrypted_segments_past_the_checkpoint_are_merged_without_a_reques
|
||||
|
||||
ctx = context(tmp_path, "resumed.mp4", KEY_A)
|
||||
probes = Counter({path: hits - 1 for path, hits in routes.hits.items()})
|
||||
decrypted.clear()
|
||||
fail_once(server, DASH, make, ctx)
|
||||
first = segment_indices(decrypted)
|
||||
# segment 4 finished before the failure: its decrypt replaced it, but a lock kept the downloaded file
|
||||
raw = routes[part(DASH, 4)]
|
||||
(ctx.save_dir / "4.clear").write_bytes(b"[aa]" + raw)
|
||||
@@ -274,7 +282,7 @@ def test_kept_decrypted_segments_past_the_checkpoint_are_merged_without_a_reques
|
||||
assert {path for path, hits in routes.hits.items() if path.startswith("/part") and hits > probes[path]} == {
|
||||
part(DASH, FAIL_AT)
|
||||
}
|
||||
assert [name for name in decrypted if name[0].isdigit()] == ["3.mp4"]
|
||||
assert sorted(first + segment_indices(decrypted)) == list(range(FAIL_AT + 1))
|
||||
|
||||
|
||||
def test_a_rolling_output_crosses_the_run_boundary_in_the_stash(server, routes, tmp_path, monkeypatch, decrypted):
|
||||
|
||||
Reference in New Issue
Block a user