Fix remaining queue races Refs #6

This commit is contained in:
2026-07-30 10:40:39 +02:00
parent 7bdab60d6f
commit c5cc0901c5
3 changed files with 59 additions and 12 deletions
+51 -7
View File
@@ -1,3 +1,4 @@
import asyncio
import threading
from importarr.config import Settings
@@ -316,7 +317,7 @@ def test_ignore_loses_atomic_race_with_worker_claim(tmp_path, monkeypatch):
assert main.state.get_queue_item(row["id"])["claimed_by"] == "worker"
def test_shutdown_waits_for_active_import_before_releasing_claims(tmp_path, monkeypatch):
def test_shutdown_timeout_does_not_release_live_worker_claim(tmp_path, monkeypatch):
main, download, _movies, _tv = configure_main(tmp_path, monkeypatch)
source = download / "A.mkv"
source.parent.mkdir(parents=True)
@@ -337,21 +338,64 @@ def test_shutdown_waits_for_active_import_before_releasing_claims(tmp_path, monk
worker = threading.Thread(target=active_worker)
monkeypatch.setattr(main, "_worker_thread", worker)
monkeypatch.setattr(main, "WORKER_SHUTDOWN_TIMEOUT_SECONDS", 0.01)
worker.start()
assert started.wait(1)
shutdown = threading.Thread(target=main.shutdown_queue_worker)
shutdown.start()
shutdown.join(0.05)
assert shutdown.is_alive()
main.shutdown_queue_worker()
assert worker.is_alive()
assert main.state.get_queue_item(row["id"])["claimed_by"] == main.WORKER_ID
finish.set()
shutdown.join(1)
assert not shutdown.is_alive()
worker.join(1)
assert not worker.is_alive()
assert main.state.get_queue_item(row["id"])["claimed_by"] is None
def test_manual_sync_preserves_claim_after_source_is_unlinked(tmp_path, monkeypatch):
main, download, _movies, _tv = configure_main(tmp_path, monkeypatch)
batch = download / "Release"
batch.mkdir(parents=True)
source = batch / "A.mkv"
source.write_bytes(b"a")
created_batch = main.state.add_manual_batch(batch)
row = main.state.upsert_queue_item(source_type="manual", source_id=str(source), source_path=source, name=source.name, state="ready", batch_id=created_batch["id"])
main.state.claim_queue_item(row["id"], main.WORKER_ID, {"ready"})
source.unlink()
main.sync_manual_queue()
preserved = main.state.get_queue_item(row["id"])
assert preserved["state"] == "importing"
assert preserved["claimed_by"] == main.WORKER_ID
def test_bulk_sab_import_preserves_job_metadata(tmp_path, monkeypatch):
main, download, movies, tv = configure_main(tmp_path, monkeypatch)
release = download / "Release"
release.mkdir(parents=True)
source = release / "Movie.mkv"
source.write_bytes(b"movie")
class FakeSabnzbdClient:
def __init__(self, *args, **kwargs):
pass
async def active_nzo_ids(self):
return set()
async def history(self):
return {"history": {"slots": [{"nzo_id": "SAB-123", "name": "Release", "category": "manual", "status": "Completed", "storage": str(release)}]}}
monkeypatch.setattr(main, "SabnzbdClient", FakeSabnzbdClient)
assert asyncio.run(main._import_ready_sab_jobs(main.Importer(movies, tv))) == 1
row = main.state.list_queue_items(active_only=False)[0]
assert row["job_id"] == "SAB-123"
assert row["sab_category"] == "manual"
def test_remove_conflicts_when_item_is_claimed(tmp_path, monkeypatch):
main, _download, _movies, _tv = configure_main(tmp_path, monkeypatch)
row = main.state.upsert_queue_item(source_type="manual", source_id="a", name="A.mkv", state="ready")