#!/usr/bin/env python3 """Общая идемпотентность POST /chats/{id}/messages через LB и несколько реплик. Ключ — заголовок Idempotency-Key, область: пользователь + метод + путь чата + ключ, срок ≥ 24 ч. Проверки: повтор и конкурентный повтор через разные реплики → ровно одно сообщение; другой body → 409; области ключа; формат ключа; неуспешный запрос не «занимает» ключ; повтор снова проверяет сессию и доступ. """ import json import threading import uuid from collections import Counter from common import BASE_URL, Client, Suite, check, login, new_user, sql c = Client(BASE_URL) s = Suite("idempotency across replicas (" + BASE_URL + ")") alice = new_user(c, "ia"); login(c, alice) bob = new_user(c, "ib"); login(c, bob) eve = new_user(c, "ie"); login(c, eve) def chat_of(*actors): return c.call("POST", "/api/v1/chats", 201, actor=actors[0], schema="Chat", body={"title": "idem", "member_ids": [a["id"] for a in actors]})["json"] chat = chat_of(alice, bob) PATH = "/api/v1/chats/" + chat["id"] + "/messages" def post(actor, key, body=None, raw=None, expected=201, client=None, path=PATH): h = {"Idempotency-Key": key} if key is not None else {} if raw is not None: h["Content-Type"] = "application/json" return (client or c).call("POST", path, expected, actor=actor, body=body, raw=raw, headers=h, schema="Message" if expected == 201 else None) def count_text(text): return int(sql("SELECT count(*) FROM messages WHERE text = '%s'" % text.replace("'", "''"))[0]) @s.case("sequential replays: same 201 + same Message from different replicas") def _(): key, text = uuid.uuid4().hex, "seq-" + uuid.uuid4().hex first = post(alice, key, {"text": text}) instances = {first["headers"]["X-Instance-Id"]} for _ in range(10): r = post(alice, key, {"text": text}) check(r["json"] == first["json"], "replay differs") check(r["headers"].get("Idempotent-Replayed") == "true", "replay not marked") instances.add(r["headers"]["X-Instance-Id"]) check(len(instances) >= 2, "replays were not served by >=2 replicas: %s" % instances) check(count_text(text) == 1, "duplicate message") return "replicas=%d, rows=1" % len(instances) @s.case("30 concurrent POSTs, same key + body, via LB -> exactly one message") def _(): key, text = uuid.uuid4().hex, "conc-" + uuid.uuid4().hex results, lock, barrier = [], threading.Lock(), threading.Barrier(30) def worker(): local = Client(BASE_URL, timeout=15) # отдельное соединение → LB распределяет по репликам barrier.wait() r = post(alice, key, {"text": text}, client=local) with lock: results.append((r["status"], json.dumps(r["json"], sort_keys=True), r["headers"]["X-Instance-Id"])) threads = [threading.Thread(target=worker) for _ in range(30)] [t.start() for t in threads]; [t.join() for t in threads] check(len(results) == 30 and all(st == 201 for st, _, _ in results), "not all 201") check(len({b for _, b, _ in results}) == 1, "different Message bodies returned") per_instance = Counter(i for _, _, i in results) check(len(per_instance) >= 2, "concurrency did not span replicas: %s" % dict(per_instance)) check(count_text(text) == 1, "duplicates: %d" % count_text(text)) return "per-replica responses %s, rows=1" % dict(per_instance) @s.case("concurrent same key, two different bodies -> one message, others 201-same or 409") def _(): key, a, b = uuid.uuid4().hex, "race-a-" + uuid.uuid4().hex, "race-b-" + uuid.uuid4().hex out, lock, barrier = [], threading.Lock(), threading.Barrier(20) def worker(i): local = Client(BASE_URL, timeout=15) barrier.wait() r = post(alice, key, {"text": a if i % 2 else b}, client=local, expected=[201, 409]) with lock: out.append((i % 2, r["status"], r["json"])) threads = [threading.Thread(target=worker, args=(i,)) for i in range(20)] [t.start() for t in threads]; [t.join() for t in threads] rows = count_text(a) + count_text(b) check(rows == 1, "expected exactly one message, got %d" % rows) winner = a if count_text(a) else b for parity, status, body in out: mine = a if parity else b check(status == (201 if mine == winner else 409), "status %d for %s" % (status, "winner" if mine == winner else "loser")) return "201x%d, 409x%d" % (sum(1 for o in out if o[1] == 201), sum(1 for o in out if o[1] == 409)) @s.case("after commit: different body -> 409; reordered/whitespace JSON -> replay") def _(): key, text = uuid.uuid4().hex, "fmt-" + uuid.uuid4().hex first = post(alice, key, {"text": text}) r = post(alice, key, {"text": text + "!"}, expected=409) check(r["json"]["error"]["code"] == "conflict", "409 code") raw = ('{ \n "text" : ' + json.dumps(text) + ' \n}').encode() check(post(alice, key, raw=raw)["json"] == first["json"], "semantically equal JSON not replayed") @s.case("scope: user + chat + key (same key elsewhere is independent)") def _(): key = "shared-key:" + uuid.uuid4().hex[:8] m1 = post(alice, key, {"text": "scope-1"})["json"] m2 = post(bob, key, {"text": "scope-1"})["json"] # другой пользователь other = chat_of(alice, bob) m3 = post(alice, key, {"text": "scope-1"}, path="/api/v1/chats/" + other["id"] + "/messages")["json"] check(len({m1["id"], m2["id"], m3["id"]}) == 3, "keys leaked across scope") check(m2["sender_id"] == bob["id"], "sender") @s.case("key format: missing/empty/129 chars/invalid -> 400; 128 chars ok") def _(): for key in (None, "", "a" * 129, "bad key", "k%y", "a/b", "k\"q"): post(alice, key, {"text": "x"}, expected=400) post(alice, "A" * 120 + "._:-z019", {"text": "len-128"}) @s.case("failed attempt does not claim the key (400/404 not recorded)") def _(): key = uuid.uuid4().hex post(alice, key, {"text": " "}, expected=400) m = post(alice, key, {"text": "after-400"})["json"] check(m["text"] == "after-400", "key was claimed by failed request") key2 = uuid.uuid4().hex post(eve, key2, {"text": "intruder"}, expected=404) # посторонний post(alice, key2, {"text": "after-404"}) @s.case("replay re-checks session and access") def _(): u = dict(alice); login(c, u) key, text = uuid.uuid4().hex, "relogin-" + uuid.uuid4().hex post(u, key, {"text": text}) c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=u) post(u, key, {"text": text}, expected=401) post(eve, key, {"text": text}, expected=404) # чужой ключ/чат не раскрывается check(post(alice, key, {"text": text})["json"]["text"] == text, "new session must replay") @s.case("key older than IDEMPOTENCY_TTL_SECONDS is reusable (24 h window)") def _(): key, text = uuid.uuid4().hex, "ttl-" + uuid.uuid4().hex first = post(alice, key, {"text": text})["json"] sql("UPDATE idempotency_keys SET created_at = created_at - interval '25 hours' WHERE key = '%s'" % key) second = post(alice, key, {"text": text})["json"] check(second["id"] != first["id"], "expired key must create a new message") check(count_text(text) == 2, "rows") if __name__ == "__main__": s.run()