forked from mtusi2026/mtusi2026lab4
Реализовать lab4 на оценку 4: SeaweedFS, вложения и согласованность БД/S3.
Перенесён стенд lab3 (API, Redis-сессии, идемпотентность, HAProxy, реплики API). Добавлены приватный bucket SeaweedFS с отдельным ключом API, загрузка PNG/JPEG с лимитами и полной проверкой, компенсация и reconciler, плавная остановка API, тесты и evidence.
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
"""Lab3: shared Idempotency-Key and multi-replica visibility."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import concurrent.futures
|
||||
import uuid
|
||||
|
||||
from helpers import call, register
|
||||
|
||||
|
||||
def _chat_pair():
|
||||
a = register("idem_a")
|
||||
b = register("idem_b")
|
||||
chat = call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"title": "idem", "member_ids": [a["id"], b["id"]]},
|
||||
)["json"]
|
||||
return a, b, chat
|
||||
|
||||
|
||||
def test_missing_idempotency_key_400():
|
||||
a, _b, chat = _chat_pair()
|
||||
call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
400,
|
||||
token=a["token"],
|
||||
body={"text": "no-key"},
|
||||
headers={"Idempotency-Key": ""},
|
||||
)
|
||||
|
||||
|
||||
def test_replay_same_key_same_body():
|
||||
a, _b, chat = _chat_pair()
|
||||
key = uuid.uuid4().hex
|
||||
first = call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": "same"},
|
||||
headers={"Idempotency-Key": key},
|
||||
)["json"]
|
||||
second = call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": "same"},
|
||||
headers={"Idempotency-Key": key},
|
||||
)["json"]
|
||||
assert first == second
|
||||
|
||||
|
||||
def test_empty_attachment_ids_equals_absent():
|
||||
# Peer remark: attachment_ids: [] ≡ absent field; must not 400; same idempotency hash.
|
||||
a, _b, chat = _chat_pair()
|
||||
key = uuid.uuid4().hex
|
||||
first = call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": "with-empty-attachments", "attachment_ids": []},
|
||||
headers={"Idempotency-Key": key},
|
||||
)["json"]
|
||||
second = call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": "with-empty-attachments"},
|
||||
headers={"Idempotency-Key": key},
|
||||
)["json"]
|
||||
assert first == second
|
||||
|
||||
|
||||
def test_same_key_different_body_409():
|
||||
a, _b, chat = _chat_pair()
|
||||
key = uuid.uuid4().hex
|
||||
call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": "one"},
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
409,
|
||||
token=a["token"],
|
||||
body={"text": "two"},
|
||||
headers={"Idempotency-Key": key},
|
||||
)
|
||||
|
||||
|
||||
def test_key_scoped_to_user_and_chat():
|
||||
# Bob reusing Alice's key must get his own message, not her stored response.
|
||||
a, b, chat = _chat_pair()
|
||||
key = uuid.uuid4().hex
|
||||
first = call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, token=a["token"],
|
||||
body={"text": "same"}, headers={"Idempotency-Key": key})["json"]
|
||||
other = call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, token=b["token"],
|
||||
body={"text": "same"}, headers={"Idempotency-Key": key})["json"]
|
||||
assert other["id"] != first["id"] and other["sender_id"] == b["id"]
|
||||
second_chat = call("POST", "/api/v1/chats", 201, token=a["token"],
|
||||
body={"title": "idem2", "member_ids": [a["id"], b["id"]]})["json"]
|
||||
elsewhere = call("POST", f"/api/v1/chats/{second_chat['id']}/messages", 201, token=a["token"],
|
||||
body={"text": "same"}, headers={"Idempotency-Key": key})["json"]
|
||||
assert elsewhere["id"] != first["id"] and elsewhere["chat_id"] == second_chat["id"]
|
||||
|
||||
|
||||
def test_invalid_key_format_400():
|
||||
a, _b, chat = _chat_pair()
|
||||
for key in ("has space", "x" * 129, "a/b", "a,b", "ключ".encode().decode("latin-1")):
|
||||
resp = call("POST", f"/api/v1/chats/{chat['id']}/messages", 400, token=a["token"],
|
||||
body={"text": "bad key"}, headers={"Idempotency-Key": key})
|
||||
assert resp["json"]["error"]["code"] == "invalid_request", key
|
||||
call("POST", f"/api/v1/chats/{chat['id']}/messages", 201, token=a["token"],
|
||||
body={"text": "ok key"}, headers={"Idempotency-Key": "A-z0.9_:" + "x" * 120})
|
||||
|
||||
|
||||
def test_parallel_same_key_one_message():
|
||||
a, b, chat = _chat_pair()
|
||||
key = uuid.uuid4().hex
|
||||
text = "parallel-idem-" + key
|
||||
|
||||
def send(_):
|
||||
return call(
|
||||
"POST",
|
||||
f"/api/v1/chats/{chat['id']}/messages",
|
||||
201,
|
||||
token=a["token"],
|
||||
body={"text": text},
|
||||
headers={"Idempotency-Key": key},
|
||||
)["json"]
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:
|
||||
results = list(pool.map(send, range(8)))
|
||||
ids = {m["id"] for m in results}
|
||||
assert len(ids) == 1, ids
|
||||
page = call(
|
||||
"GET",
|
||||
f"/api/v1/chats/{chat['id']}/messages?limit=100",
|
||||
200,
|
||||
token=b["token"],
|
||||
)["json"]
|
||||
assert sum(1 for m in page["items"] if m["text"] == text) == 1
|
||||
|
||||
|
||||
def test_multiple_instance_ids_observed():
|
||||
a = register("inst")
|
||||
seen = set()
|
||||
for _ in range(40):
|
||||
seen.add(
|
||||
call("GET", "/api/v1/users/me", 200, token=a["token"])["headers"].get(
|
||||
"X-Instance-Id"
|
||||
)
|
||||
)
|
||||
assert len(seen) >= 2, f"expected ≥2 replicas via LB, got {seen}"
|
||||
Reference in New Issue
Block a user