forked from mtusi2026/mtusi2026lab2
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.
This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
92 lines
2.7 KiB
Python
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)
|