import base64 import hashlib import json import subprocess import time import urllib.error import urllib.parse import urllib.request import uuid import zlib import struct BASE = "http://127.0.0.1:8080" def docker(*args): subprocess.run( ["docker", "compose", *args], check=True, ) def api(method, path, token=None, body=None, headers=None): request_headers = dict(headers or {}) if token: request_headers["Authorization"] = f"Bearer {token}" if isinstance(body, dict): body = json.dumps(body).encode("utf-8") request_headers["Content-Type"] = "application/json" request = urllib.request.Request( BASE + path, data=body, headers=request_headers, method=method, ) with urllib.request.urlopen(request, timeout=10) as response: data = response.read() return ( response.status, json.loads(data) if data and "application/json" in response.headers.get( "Content-Type", "" ) else data, ) def png_chunk(name, data): value = name + data return ( struct.pack(">I", len(data)) + value + struct.pack(">I", zlib.crc32(value) & 0xFFFFFFFF) ) def make_png(): width, height = 320, 200 scanline = b"\x00" + bytes([220, 80, 40]) * width return ( b"\x89PNG\r\n\x1a\n" + png_chunk( b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0), ) + png_chunk( b"IDAT", zlib.compress(scanline * height) ) + png_chunk(b"IEND", b"") ) def wait_ready(): for _ in range(90): try: status, data = api("GET", "/health/ready") if status == 200 and data.get("status") == "ok": return except Exception: pass time.sleep(1) raise RuntimeError("API did not become ready") def main(): suffix = uuid.uuid4().hex[:12] username = "lab5_" + suffix password = "Lab5TestPassword_" + suffix print("Stopping worker...", flush=True) docker("stop", "worker") wait_ready() status, user = api( "POST", "/api/v1/users", body={ "username": username, "display_name": "Lab5 recovery test", "password": password, }, ) assert status == 201 credentials = base64.b64encode( f"{username}:{password}".encode() ).decode() status, session = api( "POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {credentials}"}, body=b"", ) assert status == 201 token = session["token"] status, chat = api( "POST", "/api/v1/chats", token, { "title": "Worker recovery", "member_ids": [user["id"]], }, ) assert status == 201 original = make_png() path = ( f"/api/v1/chats/{chat['id']}/attachments" "?filename=recovery.png" ) status, attachment = api( "POST", path, token, original, {"Content-Type": "image/png"}, ) assert status == 202, f"Expected 202, got {status}" assert attachment["status"] == "queued" attachment_id = attachment["id"] print("Upload: 202 queued", flush=True) status, message = api( "POST", f"/api/v1/chats/{chat['id']}/messages", token, {"text": "", "attachment_ids": [attachment_id]}, {"Idempotency-Key": "recovery-" + suffix}, ) assert status == 201 assert attachment_id in message["attachment_ids"] print("Message created while worker is stopped", flush=True) status, metadata = api( "GET", f"/api/v1/attachments/{attachment_id}", token ) assert status == 200 and metadata["status"] == "queued" print("Restarting RabbitMQ and API...", flush=True) docker("restart", "rabbitmq") docker("up", "-d", "--wait", "rabbitmq") docker("restart", "api1", "api2") wait_ready() status, metadata = api( "GET", f"/api/v1/attachments/{attachment_id}", token ) assert metadata["status"] == "queued" print("Queued attachment survived restart", flush=True) print("Starting worker...", flush=True) docker("up", "-d", "worker") deadline = time.monotonic() + 90 while time.monotonic() < deadline: status, metadata = api( "GET", f"/api/v1/attachments/{attachment_id}", token ) if metadata["status"] == "ready": break if metadata["status"] == "failed": raise RuntimeError( f"Processing failed: {metadata.get('error_code')}" ) time.sleep(1) else: raise RuntimeError("Attachment did not become ready") status, downloaded = api( "GET", f"/api/v1/attachments/{attachment_id}/content", token, ) assert status == 200 assert downloaded == original assert hashlib.sha256(downloaded).hexdigest() == ( hashlib.sha256(original).hexdigest() ) status, thumbnail = api( "GET", f"/api/v1/attachments/{attachment_id}/content" "?variant=thumbnail", token, ) assert status == 200 assert thumbnail.startswith(b"\xff\xd8") assert thumbnail.endswith(b"\xff\xd9") variants = metadata["variants"] assert any( v["name"] == "thumbnail" and v["width"] == 256 and v["height"] == 160 for v in variants ) print("Attachment: ready", flush=True) print("Original: SHA-256 verified", flush=True) print("Thumbnail: JPEG 256x160 verified", flush=True) print("PASS: durable worker recovery", flush=True) if __name__ == "__main__": try: main() finally: # Restore the worker even when the test fails. docker("up", "-d", "worker")