Исправить обработку крупных результатов worker

This commit is contained in:
2026-11-17 19:24:40 +03:00
parent e8becaf761
commit 3ed3046df1
4 changed files with 64 additions and 5 deletions
+28
View File
@@ -2,11 +2,14 @@ from __future__ import annotations
import hashlib
from io import BytesIO
import random
from types import SimpleNamespace
import pytest
from PIL import Image
from app.jobs.pipeline import PermanentImageError, process_image
from app.jobs.worker import Worker, _run_isolated
def encoded(width: int, height: int, mode: str = 'RGBA') -> bytes:
@@ -87,3 +90,28 @@ def test_exif_orientation_is_applied_and_removed() -> None:
assert (variants['thumbnail'].width, variants['thumbnail'].height) == (160, 256)
with Image.open(BytesIO(variants['thumbnail'].content)) as transformed:
assert not transformed.getexif()
def test_isolated_pipeline_returns_results_larger_than_pipe_buffer() -> None:
"""Drain child output before join so a large valid result cannot deadlock the pipe."""
pixels = random.Random(0).randbytes(512 * 512 * 3)
image = Image.frombytes('RGB', (512, 512), pixels)
output = BytesIO()
image.save(output, 'PNG')
source = output.getvalue()
variants = _run_isolated(
(source, 'image/png', hashlib.sha256(source).hexdigest(), 40_000_000, 85, 85, 82),
timeout=10,
memory_bytes=512 * 1024 * 1024,
)
assert {item.name for item in variants} == {'thumbnail', 'crop', 'optimized'}
assert sum(len(item.content) for item in variants) > 64 * 1024
def test_retry_delay_uses_capped_exponential_backoff_and_jitter(monkeypatch) -> None:
"""Calculate capped exponential backoff and add the configured random jitter."""
settings = SimpleNamespace(worker_concurrency=1, retry_base_seconds=2, retry_max_seconds=8)
worker = Worker(settings, jobs=None, storage=None)
monkeypatch.setattr('app.jobs.worker.random.uniform', lambda start, end: end / 2)
assert worker._delay(1) == 3
assert worker._delay(4) == 12