forked from mtusi2026/mtusi2026lab5
81 lines
4.5 KiB
Python
81 lines
4.5 KiB
Python
"""lab5, уровень 3: повторная доставка — нормальная ситуация, она не создаёт второй набор вариантов.
|
|
|
|
make redelivery (= python student-tests/redelivery.py)
|
|
|
|
1. upload -> ready: запоминаем варианты (SHA-256), объекты под variants/<id>/ и строки в БД;
|
|
2. в очередь ещё дважды отправляется ТО ЖЕ сообщение (тот же job_id и attempt) — так выглядит
|
|
повторная доставка (например, relay упал после подтверждения брокера, но до отметки в outbox);
|
|
3. worker получает оба дубля и подтверждает их без обработки: в журнале задания duplicate_ignored,
|
|
вариантов по-прежнему 3, объектов 3, SHA-256 те же, статус ready, попытка 1;
|
|
4. сообщение не по схеме jobs.schema.json (лишнее поле с URL; число вместо UUID; не JSON) worker
|
|
не обрабатывает и не падает: reject -> DLQ (attachments.dead), обычная работа продолжается.
|
|
"""
|
|
import json
|
|
import time
|
|
|
|
import ops
|
|
from filegen import upload_when_ready, wait_status
|
|
from helpers import Client, check, run
|
|
|
|
|
|
def wait_events(attachment_id, name, count, seconds=30):
|
|
deadline = time.monotonic() + seconds
|
|
while True:
|
|
job = ops.job(attachment_id)
|
|
found = [e for e in job["events"] if e["event"] == name]
|
|
if len(found) >= count:
|
|
return job
|
|
check(time.monotonic() < deadline, f"expected {count} {name} events, got {len(found)}")
|
|
time.sleep(1)
|
|
|
|
|
|
def main():
|
|
api = Client(timeout=15)
|
|
ops.wait_healthy("worker", 1)
|
|
alice, bob, _eve, chat = api.bootstrap()
|
|
att = upload_when_ready(api, alice, chat, name="redelivery.png")["json"]
|
|
ready = wait_status(api, bob, att["id"])
|
|
before = ops.job(att["id"])
|
|
keys = ops.objects(f"variants/{att['id']}/")
|
|
check(len(before["variants"]) == 3 and len(keys) == 3, "one set of variants expected")
|
|
print(f"1. attachment {att['id']} ready: 3 variant rows, objects {keys}")
|
|
|
|
message = {"schema_version": 1, "job_id": before["job"]["job_id"], "type": "attachment.process.v1",
|
|
"attachment_id": att["id"], "pipeline_version": 1, "attempt": 1,
|
|
"created_at": "2026-10-25T12:00:00Z"}
|
|
for _ in range(2):
|
|
ops.publish(message)
|
|
print("2. the same message (same job_id and attempt) published twice more")
|
|
after = wait_events(att["id"], "duplicate_ignored", 2)
|
|
keys_after = ops.objects(f"variants/{att['id']}/")
|
|
check(after["variants"] == before["variants"], "variants changed after a duplicate")
|
|
check(sorted(keys_after) == sorted(keys), "a second set of objects appeared")
|
|
check(after["job"]["attempt"] == 1 and after["attachment_status"] == "ready", "duplicate changed the job")
|
|
meta = api.call("GET", f"/api/v1/attachments/{att['id']}", actor=bob, schema="Attachment")["json"]
|
|
check(meta == ready, "metadata changed after a duplicate")
|
|
print("3. both duplicates acknowledged without processing: " +
|
|
", ".join(f"{e['event']}({e['detail']})" for e in after["events"] if e["event"] == "duplicate_ignored"))
|
|
print(f" variants: {[(v['name'], v['sha256'][:12]) for v in after['variants']]} — same rows, same objects, "
|
|
f"status ready, attempt 1")
|
|
|
|
dead_before = ops.queues().get("attachments.dead", (0, 0))[0]
|
|
bad = [dict(message, url="http://example.invalid/steal"), dict(message, job_id=42), None]
|
|
for body in bad:
|
|
ops.publish_raw(b"not json at all" if body is None else json.dumps(body).encode())
|
|
deadline = time.monotonic() + 30
|
|
while ops.queues().get("attachments.dead", (0, 0))[0] < dead_before + len(bad):
|
|
check(time.monotonic() < deadline, "invalid messages did not reach the DLQ")
|
|
time.sleep(1)
|
|
dead_after = ops.queues().get("attachments.dead", (0, 0))[0]
|
|
print(f"4. 3 messages that do not match jobs.schema.json -> reject -> DLQ attachments.dead: "
|
|
f"{dead_before} -> {dead_after}")
|
|
second = upload_when_ready(api, alice, chat, name="after-bad.png")["json"]
|
|
wait_status(api, bob, second["id"])
|
|
print(" the worker keeps working: a new upload became ready")
|
|
print("PASS: redelivery of a finished job is a no-op with ack (no second set of variants); "
|
|
"malformed messages go to the DLQ without crashing the worker")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|