#!/usr/bin/env python3 """Exercise 202 handoff while RabbitMQ and worker are unavailable.""" import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure, arguments, check, run # noqa: E402 def compose(*args): subprocess.run( ["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, check=True, stdout=subprocess.DEVNULL, ) def ready_after_restart(client): deadline = time.monotonic() + 90 while time.monotonic() < deadline: try: client.call("GET", "/health/ready", schema="Health") return except Failure: time.sleep(1) raise Failure("API did not recover after restart") def main(): args = arguments("Lab5 durable upload handoff with stopped broker and worker") client = Client(args.base_url, args.ca_file) compose("stop", "worker", "publisher", "broker") try: alice, bob, _eve, chat = client.bootstrap() attachment, original = client.attachment(alice, chat) client.call("GET", f"/api/v1/attachments/{attachment['id']}/content", 409, actor=bob) message = client.message(alice, chat, "", attachments=[attachment["id"]])["json"] check(message["attachment_ids"] == [attachment["id"]], "queued attachment cannot be linked") compose("up", "-d", "broker") compose("restart", "api") ready_after_restart(client) check( client.call("GET", f"/api/v1/attachments/{attachment['id']}", actor=bob, schema="Attachment")["json"]["status"] == "queued", "accepted job disappeared before publisher restart", ) compose("up", "-d", "publisher", "worker") client.wait_ready(bob, attachment) result = client.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob) check(result["bytes"] == original, "accepted original changed after recovery") finally: compose("up", "-d", "broker", "publisher", "worker") print("PASS: accepted upload survived broker/API outage and reached ready") if __name__ == "__main__": run(main)