"""Check that a queued upload survives restarting API and RabbitMQ.""" 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 def compose(*args): subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True) def main(): args = arguments(__doc__) client = Client(args.base_url, args.ca_file) owner = client.user("queue") chat = client.call("POST", "/api/v1/chats", 201, actor=owner, body={ "title": "Queue restart", "member_ids": [owner["id"]], }, schema="Chat")["json"] compose("stop", "-t", "5", "worker") try: attachment, original = client.attachment(owner, chat) content_path = "/api/v1/attachments/" + attachment["id"] + "/content" client.call("GET", content_path, 409, actor=owner) compose("restart", "-t", "5", "api", "rabbitmq") finally: compose("start", "worker") deadline = time.monotonic() + 60 while True: try: client.call("GET", "/health/ready", schema="Health") break except Failure: check(time.monotonic() < deadline, "API did not recover after restart") time.sleep(0.5) client.wait_ready(owner, attachment) restored = client.call("GET", content_path, 200, actor=owner)["bytes"] check(restored == original, "Recovered attachment differs from upload") print("PASS: queued upload survived API and RabbitMQ restart with worker stopped") if __name__ == "__main__": run(main)