forked from mtusi2026/mtusi2026lab5
Complete lab 5 asynchronous image processing
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import io
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from app.services.image_processor import (
|
||||
ImageProcessingTimeout,
|
||||
create_variants_with_timeout,
|
||||
)
|
||||
|
||||
|
||||
def make_test_image() -> bytes:
|
||||
output = io.BytesIO()
|
||||
|
||||
image = Image.new("RGB", (640, 400), (120, 160, 200))
|
||||
image.save(output, format="PNG")
|
||||
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
image_data = make_test_image()
|
||||
|
||||
print("Test 1: normal image processing")
|
||||
|
||||
variants = create_variants_with_timeout(
|
||||
image_data,
|
||||
timeout_seconds=30,
|
||||
)
|
||||
|
||||
names = sorted(variant.name for variant in variants)
|
||||
|
||||
assert names == ["crop", "optimized", "thumbnail"], names
|
||||
|
||||
print(f"PASS: created variants: {names}")
|
||||
|
||||
print("Test 2: forced processing timeout")
|
||||
|
||||
try:
|
||||
create_variants_with_timeout(
|
||||
image_data,
|
||||
timeout_seconds=0.001,
|
||||
)
|
||||
except ImageProcessingTimeout:
|
||||
print("PASS: child process was terminated after timeout")
|
||||
else:
|
||||
raise AssertionError("Expected ImageProcessingTimeout")
|
||||
|
||||
print("ALL TESTS PASSED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user