"""Run inside the worker image; deliberately small limits, no stack changes. PowerShell: Get-Content -Raw student-tests/lab5_limits.py | docker compose exec -T worker python Linux: docker compose exec -T worker python < student-tests/lab5_limits.py """ import asyncio import os from pathlib import Path import tempfile import time from PIL import Image from app import worker async def main(): with tempfile.TemporaryDirectory() as temporary: directory = Path(temporary) source = directory / "large.png" with Image.new("RGB", (4000, 4000), "green") as image: image.save(source) old_memory = os.environ.get("WORKER_MEMORY_MB") old_timeout = worker.IMAGE_TIMEOUT try: os.environ["WORKER_MEMORY_MB"] = "48" try: await worker.generate(source, directory) raise AssertionError("decoder exceeded its deliberately small memory budget") except worker.PermanentFailure as error: assert str(error) == "image_resource_limit", str(error) print("PASS memory: real 16MP image decoder stopped at RLIMIT_AS=48MiB", flush=True) os.environ["WORKER_MEMORY_MB"] = "768" worker.IMAGE_TIMEOUT = 0.001 started = time.monotonic() try: await worker.generate(source, directory) raise AssertionError("decoder exceeded wall timeout") except worker.PermanentFailure as error: assert str(error) == "image_timeout", str(error) assert time.monotonic() - started < 3, "timed-out child was not promptly reaped" print("PASS timeout: decoder killed and reaped; parent continues", flush=True) finally: worker.IMAGE_TIMEOUT = old_timeout if old_memory is None: os.environ.pop("WORKER_MEMORY_MB", None) else: os.environ["WORKER_MEMORY_MB"] = old_memory print("PASS: lab5 decoder resource limits", flush=True) asyncio.run(main())