Complete Lab 5 grade 4 async attachment pipeline
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Verify a queued stream event survives broker restart with its volume."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Failure, arguments, run, Client # noqa: E402
|
||||
|
||||
|
||||
STREAM = "messenger:attachment-jobs:v1"
|
||||
|
||||
|
||||
def compose(*args: str, expected: int = 0, timeout: int = 90) -> subprocess.CompletedProcess:
|
||||
result = subprocess.run(
|
||||
["docker", "compose", "--env-file", ".env", *args],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout,
|
||||
)
|
||||
if result.returncode != expected:
|
||||
operation = args[0] if args else "compose"
|
||||
raise Failure("Docker Compose operation failed: " + operation + " (exit " + str(result.returncode) + ")")
|
||||
return result
|
||||
|
||||
|
||||
def stream_contains(attachment_id: str) -> bool:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"docker", "compose", "--env-file", ".env", "exec", "-T", "broker", "sh", "-ec",
|
||||
"REDISCLI_AUTH=\"$(cat /run/secrets/broker_password)\" redis-cli --raw XREVRANGE "
|
||||
+ STREAM + " + - COUNT 500",
|
||||
],
|
||||
cwd=ROOT,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
return result.returncode == 0 and attachment_id in result.stdout
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = arguments(__doc__, lambda parser: parser.add_argument("--output", default="evidence/broker-persistence.json"))
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
compose("stop", "worker")
|
||||
compose("up", "-d", "dispatcher")
|
||||
try:
|
||||
alice, _bob, _eve, chat = client.bootstrap()
|
||||
attachment, _content = client.attachment(alice, chat)
|
||||
client.message(alice, chat, "Attachment queued for broker persistence check", attachments=[attachment["id"]])
|
||||
client.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=alice)
|
||||
|
||||
deadline = time.monotonic() + 30
|
||||
while time.monotonic() < deadline and not stream_contains(attachment["id"]):
|
||||
time.sleep(0.5)
|
||||
if not stream_contains(attachment["id"]):
|
||||
raise Failure("dispatcher did not publish the queued job to Redis Streams")
|
||||
compose("stop", "dispatcher")
|
||||
compose("restart", "broker")
|
||||
deadline = time.monotonic() + 30
|
||||
while time.monotonic() < deadline and not stream_contains(attachment["id"]):
|
||||
time.sleep(1)
|
||||
if not stream_contains(attachment["id"]):
|
||||
raise Failure("published stream event was lost when broker restarted with its volume")
|
||||
|
||||
compose("up", "-d", "dispatcher", "worker")
|
||||
ready = client.wait_ready(alice, attachment, wait_seconds=90)
|
||||
if ready["status"] != "ready":
|
||||
raise Failure("worker did not complete the preserved broker event")
|
||||
result = {
|
||||
"result": "PASS",
|
||||
"observed_at_utc": datetime.now(timezone.utc).isoformat(),
|
||||
"attachment_id": attachment["id"],
|
||||
"status_after_broker_restart": ready["status"],
|
||||
"stream_entry_survived_volume_restart": True,
|
||||
"worker_completed_same_attachment": True,
|
||||
}
|
||||
output_path = ROOT / args.output
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps(result, separators=(",", ":")))
|
||||
finally:
|
||||
compose("up", "-d", "dispatcher", "worker")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user