"""Проверка границ worker: checksum и остановка зависшего декодера""" import asyncio import hashlib import os from datetime import UTC, datetime from uuid import uuid4 import pytest from fakes import FakeObjectStorage import infrastructure.services.image_pipeline.pipeline as pipeline_module from domain.jobs import ClaimedJobSchema, JobMessageSchema, ProcessingError from infrastructure.services.image_pipeline import ImagePipeline def stand(data=b"source", checksum=None): storage = FakeObjectStorage() storage.objects["staging/source"] = (data, datetime.now(UTC)) job = ClaimedJobSchema( message=JobMessageSchema( job_id=uuid4(), attachment_id=uuid4(), attempt=1, created_at=datetime.now(UTC), ), lease_token=uuid4(), object_key="originals/final", staging_key="staging/source", size_bytes=len(data), sha256=checksum or hashlib.sha256(data).hexdigest(), content_type="image/png", ) return ImagePipeline(None, storage, 1024, 40000000, 768, 60), storage, job def test_checksum_mismatch_cannot_publish_original(): pipeline, storage, job = stand(checksum="0" * 64) with pytest.raises(ProcessingError, match="checksum_mismatch"): asyncio.run(pipeline.process(job)) assert set(storage.objects) == {"staging/source"} def test_timeout_kills_child_and_leaves_no_effect(tmp_path, monkeypatch): pid_file = tmp_path / "pid" decoder = tmp_path / "decoder.py" decoder.write_text( f"import os, time\nfrom pathlib import Path\nPath({str(pid_file)!r}).write_text(str(os.getpid()))\ntime.sleep(30)\n" ) monkeypatch.setattr(pipeline_module, "TRANSFORM_SCRIPT", decoder) pipeline, storage, job = stand() async def run(): async with asyncio.timeout(0.5): await pipeline.process(job) with pytest.raises(TimeoutError): asyncio.run(run()) pid = int(pid_file.read_text()) with pytest.raises(ProcessLookupError): os.kill(pid, 0) assert set(storage.objects) == {"staging/source"}