Files
unshackle/tests/remote/unit/test_worker_speed_limit.py
T

97 lines
3.5 KiB
Python

import asyncio
import importlib
import json
import tempfile
from types import SimpleNamespace
import click
import pytest
import unshackle.commands.dl as dl_module
from unshackle.core.api import download_manager, download_worker, handlers
from unshackle.core.api.download_manager import DownloadQueueManager
from unshackle.core.config import config
from unshackle.core.services import Services
requests_downloader = importlib.import_module("unshackle.core.downloaders.requests")
pytestmark = pytest.mark.unit
@pytest.mark.parametrize(("limit", "expected"), [("8M", 2_000_000.0), (None, None), ("off", None)])
def test_worker_share_of_limit(monkeypatch, limit, expected):
monkeypatch.setattr(config, "serve", {"global_speed_limit": limit})
assert DownloadQueueManager(max_concurrent_downloads=4).worker_speed_limit() == expected
assert requests_downloader.speed_limiter is None
def test_run_download_async_sends_share_in_payload(monkeypatch, tmp_path):
monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))
monkeypatch.setattr(config, "serve", {"global_speed_limit": "8M"})
class Stop(Exception):
pass
seen = {}
async def fake_spawn(*args, **kwargs):
with open(args[3], encoding="utf-8") as handle:
seen["payload"] = json.load(handle)
raise Stop
monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_spawn)
job = SimpleNamespace(job_id="j1", service="SVC", title_id="t", parameters={})
with pytest.raises(Stop):
asyncio.run(DownloadQueueManager(max_concurrent_downloads=4).run_download_async(job))
assert seen["payload"]["speed_limit"] == 2_000_000.0
@pytest.mark.parametrize("limit", [500_000, None])
def test_worker_main_passes_payload_limit_to_the_download(monkeypatch, tmp_path, limit):
seen = {}
def fake_perform(*args, **kwargs):
seen.update(kwargs)
return []
monkeypatch.setattr(download_worker, "perform_download", fake_perform)
monkeypatch.setattr(download_worker, "relay_prompts", lambda progress: None)
payload = tmp_path / "payload.json"
payload.write_text(
json.dumps({"job_id": "j1", "service": "SVC", "title_id": "t", "parameters": {}, "speed_limit": limit}),
encoding="utf-8",
)
assert download_worker.main(["w", str(payload), str(tmp_path / "result.json")]) == 0
assert seen["speed_limit"] == limit
assert requests_downloader.speed_limiter is None
@pytest.mark.parametrize("share", [500_000.0, None])
@pytest.mark.parametrize("params", [{}, {"speed_limit": "1M"}])
def test_perform_download_gives_dl_result_the_job_share(monkeypatch, tmp_path, share, params):
"""The share is the job's only speed limit: the API drops a client speed_limit and the dl config one."""
monkeypatch.setattr(config, "dl", {"speed_limit": "2M"})
seen = {}
class FakeDl:
cli = dl_module.dl.cli
def __init__(self, ctx, **kwargs):
pass
def result(self, **kwargs):
seen.update(kwargs)
raise RuntimeError("stop")
class FakeService:
cli = click.Command("svc", callback=lambda title: object())
monkeypatch.setattr(dl_module, "dl", FakeDl)
monkeypatch.setattr(Services, "get_path", staticmethod(lambda s: tmp_path))
monkeypatch.setattr(Services, "load", staticmethod(lambda s: FakeService))
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
with pytest.raises(RuntimeError, match="stop"):
download_manager.perform_download("job-1", "EXAMPLE", "t1", dict(params), speed_limit=share)
assert seen["speed_limit"] == share