Files
mtusi2026lab5/student-tests/unit/test_render_pool.py
T
ArtyomandClaude Opus 5.5 f2242eb39d Render variants in killable processes with time and memory limits
The renderer adapter keeps one single-process pool per WORKER_CONCURRENCY
slot. A render that exceeds RENDER_TIMEOUT_SECONDS is killed and its slot
is respawned, the render process has RLIMIT_AS = RENDER_MEMORY_MB on
Linux, so both limits end in ImageTooDemanding, a permanent
processing_failed. A render process killed from outside is a transient
error. A cancelled render (lost lease, shutdown) kills its process too.
The worker container limit grows to 1g: two 448 MiB render processes
plus the worker itself.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:02:16 +03:00

92 lines
2.7 KiB
Python

import asyncio
import os
import sys
import time
from collections.abc import AsyncIterator
import pytest
from messenger.attachments.application.ports import Rendition
from messenger.attachments.domain.errors import ImageTooDemanding
from messenger.attachments.infrastructure.images import (
ProcessVariantRenderer,
RenderLimits,
render_variants,
)
from messenger.shared.application.errors import Unavailable
from pictures import SAMPLE
LIMITS = RenderLimits(max_bytes=10_485_760, max_pixels=40_000_000)
HOG_BYTES = 512 << 20
def misbehaving(data: bytes, limits: RenderLimits) -> list[Rendition]:
if data == b"slow":
time.sleep(30)
if data == b"crash":
os._exit(1)
if data == b"oom":
raise MemoryError
if data == b"hog":
bytearray(HOG_BYTES)
return render_variants(data, limits)
@pytest.fixture
async def renderer() -> AsyncIterator[ProcessVariantRenderer]:
pool = ProcessVariantRenderer(
LIMITS,
concurrency=1,
timeout=3.0,
memory_bytes=256 << 20,
tasks_per_child=10,
render=misbehaving,
)
yield pool
pool.close()
async def test_renders_in_a_separate_process(renderer: ProcessVariantRenderer) -> None:
assert await renderer.render(SAMPLE, 1) == render_variants(SAMPLE, LIMITS)
async def test_timeout_kills_the_render_and_frees_the_slot(
renderer: ProcessVariantRenderer,
) -> None:
started = time.monotonic()
with pytest.raises(ImageTooDemanding):
await renderer.render(b"slow", 1)
assert time.monotonic() - started < 10
assert len(await renderer.render(SAMPLE, 1)) == 3
async def test_crashed_render_process_is_a_transient_error(
renderer: ProcessVariantRenderer,
) -> None:
with pytest.raises(Unavailable):
await renderer.render(b"crash", 1)
assert len(await renderer.render(SAMPLE, 1)) == 3
async def test_memory_error_is_a_permanent_error(renderer: ProcessVariantRenderer) -> None:
with pytest.raises(ImageTooDemanding):
await renderer.render(b"oom", 1)
assert len(await renderer.render(SAMPLE, 1)) == 3
async def test_cancelled_render_does_not_block_the_slot(renderer: ProcessVariantRenderer) -> None:
slow = asyncio.create_task(renderer.render(b"slow", 1))
await asyncio.sleep(1)
slow.cancel()
with pytest.raises(asyncio.CancelledError):
await slow
assert len(await asyncio.wait_for(renderer.render(SAMPLE, 1), 10)) == 3
@pytest.mark.skipif(sys.platform != "linux", reason="RLIMIT_AS применяется только в Linux")
async def test_address_space_limit_applies_to_the_render_process(
renderer: ProcessVariantRenderer,
) -> None:
with pytest.raises(ImageTooDemanding):
await renderer.render(b"hog", 1)