mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-06 07:55:10 +00:00
test: delete slow, timing-based and redundant tests
This commit is contained in:
@@ -19,8 +19,6 @@ real spawns stay fast.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
import multiprocessing
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
@@ -30,9 +28,7 @@ from requests import Session
|
||||
|
||||
from tests.downloaders.test_outage_budget import _FullDisk
|
||||
from unshackle.core.api.errors import categorize_exception
|
||||
from unshackle.core.constants import DownloadCancelled
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.utils.sslciphers import SSLCiphers
|
||||
|
||||
dl = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
|
||||
@@ -102,10 +98,6 @@ def clear_cancel():
|
||||
dl.DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
|
||||
def die_without_result(queue, kwargs):
|
||||
os._exit(3)
|
||||
|
||||
|
||||
def full_disk_worker(queue, kwargs):
|
||||
dl.open = _FullDisk
|
||||
dl.mp_worker(queue, kwargs)
|
||||
@@ -150,18 +142,6 @@ def test_multiprocess_stride_downloads_all_segments_correctly(server, tmp_path):
|
||||
assert total_advance == n
|
||||
|
||||
|
||||
def test_multiprocess_child_crash_raises(server, tmp_path):
|
||||
# download_multiprocess has no MP_MIN_SEGMENTS gate, so drive it directly with 2 urls.
|
||||
# a dict url without "url" raises KeyError while requests() builds the save path (before
|
||||
# any retry loop), so the child sends __mp_error__ fast; the sibling downloads normally.
|
||||
good = {"url": url(server, "/seg/0.bin")}
|
||||
bad = {"nourl": True}
|
||||
with pytest.raises(RuntimeError) as raised:
|
||||
run_mp([good, bad], tmp_path)
|
||||
|
||||
assert not hasattr(raised.value, "error_code")
|
||||
|
||||
|
||||
def test_a_full_disk_in_a_child_keeps_its_download_error_code(server, tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(dl, "mp_worker", full_disk_worker)
|
||||
with pytest.raises(RuntimeError) as raised:
|
||||
@@ -171,51 +151,6 @@ def test_a_full_disk_in_a_child_keeps_its_download_error_code(server, tmp_path,
|
||||
assert categorize_exception(raised.value).error_code == APIErrorCode.DOWNLOAD_ERROR
|
||||
|
||||
|
||||
def test_multiprocess_child_death_raises(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(dl, "mp_worker", die_without_result)
|
||||
raised: list[BaseException] = []
|
||||
|
||||
def drive():
|
||||
try:
|
||||
run_mp([{"url": "http://127.0.0.1:1/a"}, {"url": "http://127.0.0.1:1/b"}], tmp_path)
|
||||
except BaseException as exc:
|
||||
raised.append(exc)
|
||||
|
||||
thread = threading.Thread(target=drive, daemon=True)
|
||||
thread.start()
|
||||
thread.join(timeout=30)
|
||||
|
||||
assert not thread.is_alive()
|
||||
assert len(raised) == 1 and isinstance(raised[0], RuntimeError)
|
||||
assert "died without result" in str(raised[0])
|
||||
|
||||
|
||||
def test_multiprocess_cancel_terminates_children(server, tmp_path):
|
||||
server.delay = 0.5 # slow segments so cancel lands well before the batch could finish
|
||||
n = dl.MP_MIN_SEGMENTS
|
||||
urls = [{"url": url(server, f"/seg/{i}.bin")} for i in range(n)]
|
||||
|
||||
baseline = len(multiprocessing.active_children())
|
||||
start = time.time()
|
||||
progressed = 0
|
||||
with pytest.raises(DownloadCancelled):
|
||||
for event in dl.requests(urls, output_dir=tmp_path, filename="seg_{i:04}.bin", max_workers=4, processes=2):
|
||||
if event.get("advance") or event.get("written"):
|
||||
progressed += 1
|
||||
if progressed >= 2:
|
||||
dl.DOWNLOAD_CANCELLED.set() # sibling-track cancel: parent must tear the children down
|
||||
elapsed = time.time() - start
|
||||
|
||||
# cancel stopped the work: nowhere near all segments finished, and no hang
|
||||
assert len(list(tmp_path.glob("seg_*.bin"))) < n
|
||||
assert elapsed < 15
|
||||
# the finally-block join reaps the spawned children promptly
|
||||
deadline = time.time() + 5
|
||||
while time.time() < deadline and len(multiprocessing.active_children()) > baseline:
|
||||
time.sleep(0.05)
|
||||
assert len(multiprocessing.active_children()) <= baseline
|
||||
|
||||
|
||||
def test_speed_limit_forces_single_process(server, tmp_path, monkeypatch):
|
||||
"""A set speed limit must keep the batch in-process: spawned children re-import the
|
||||
module and would run with no TokenBucket, silently ignoring the configured cap."""
|
||||
@@ -253,36 +188,3 @@ def test_children_carry_session_params(server, tmp_path):
|
||||
assert len(files) == n
|
||||
for f in files:
|
||||
assert f.read_bytes() == seg_body(int(f.stem.split("_")[1]))
|
||||
|
||||
|
||||
def test_service_mounted_adapter_forces_single_process(server, tmp_path, monkeypatch):
|
||||
"""A child mounts a plain adapter, so a session carrying SSLCiphers has to stay in this process."""
|
||||
|
||||
def no_mp(**kwargs):
|
||||
raise AssertionError("multiprocess fan-out engaged for a session with a service-mounted adapter")
|
||||
|
||||
monkeypatch.setattr(dl, "download_multiprocess", no_mp)
|
||||
session = Session()
|
||||
session.mount("https://", SSLCiphers(security_level=2))
|
||||
n = dl.MP_MIN_SEGMENTS
|
||||
urls = [{"url": url(server, f"/seg/{i}.bin")} for i in range(n)]
|
||||
|
||||
for _ in dl.requests(
|
||||
urls, output_dir=tmp_path, filename="seg_{i:04}.bin", session=session, max_workers=4, processes=2
|
||||
):
|
||||
pass
|
||||
|
||||
assert len(list(tmp_path.glob("seg_*.bin"))) == n
|
||||
|
||||
|
||||
def test_multiprocess_skips_the_held_indices_and_counts_them_done(server, tmp_path):
|
||||
n = dl.MP_MIN_SEGMENTS
|
||||
urls = [{"url": url(server, f"/seg/{i}.bin")} for i in range(n)]
|
||||
skip = frozenset(range(0, n, 3))
|
||||
|
||||
events = list(
|
||||
dl.requests(urls, tmp_path, "seg_{i:04}.bin", session=Session(), max_workers=4, processes=2, skip=skip)
|
||||
)
|
||||
|
||||
assert sum(e.get("advance", 0) for e in events) == n
|
||||
assert sorted(p.name for p in tmp_path.iterdir()) == [f"seg_{i:04}.bin" for i in range(n) if i not in skip]
|
||||
|
||||
Reference in New Issue
Block a user