#!/usr/bin/env python3 import subprocess import sys import threading import uuid from collections import Counter 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 post(c, actor, chat, text, key, expected=201): return c.call("POST", "/api/v1/chats/" + chat["id"] + "/messages", expected, actor=actor, body={"text": text}, headers={"Idempotency-Key": key}, schema="Message" if expected == 201 else None) def history(c, actor, chat): return c.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100", actor=actor, schema="MessagePage")["json"]["items"] def db_count(chat_id): sql = "SELECT count(*) FROM messages WHERE chat_id = '%s'" % uuid.UUID(chat_id) out = subprocess.run(["docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-tAX", "-c", sql], cwd=ROOT, capture_output=True, text=True) return int(out.stdout.strip()) if out.returncode == 0 else None def main(): def configure(p): p.add_argument("--parallel", type=int, default=32) p.add_argument("--rounds", type=int, default=5) args = arguments("Concurrent idempotency through the LB", configure) c = Client(args.base_url, timeout=10) alice, bob, eve, chat = c.bootstrap() for rnd in range(args.rounds): key = "par-%d-%s" % (rnd, uuid.uuid4().hex) text = "одновременно " + key barrier = threading.Barrier(args.parallel) results, errors = [], [] def worker(): local = Client(args.base_url, timeout=10) barrier.wait() try: r = post(local, alice, chat, text, key) results.append((r["json"], r["headers"]["X-Instance-Id"])) except Failure as exc: errors.append(str(exc)) threads = [threading.Thread(target=worker) for _ in range(args.parallel)] for t in threads: t.start() for t in threads: t.join() check(not errors, "concurrent replay failed: %s" % errors[:3]) ids = {m["id"] for m, _ in results} instances = Counter(i for _, i in results) check(len(ids) == 1, "concurrent requests created %d messages" % len(ids)) check(all(m == results[0][0] for m, _ in results), "replays returned different bodies") check(len(instances) >= 2, "all requests hit one replica; LB not balancing") stored = [m for m in history(c, bob, chat) if m["text"] == text] check(len(stored) == 1, "history has %d copies" % len(stored)) print("ok - round %d: %d parallel requests, 1 message, replicas %s" % (rnd + 1, args.parallel, dict(instances))) key = uuid.uuid4().hex first = post(c, alice, chat, "оригинал", key)["json"] for _ in range(4): post(c, alice, chat, "другой текст", key, expected=409) check(post(c, alice, chat, "оригинал", key)["json"] == first, "replay changed") print("ok - different body with same key -> 409, original replays unchanged") other = post(c, bob, chat, "оригинал", key)["json"] check(other["id"] != first["id"] and other["sender_id"] == bob["id"], "key leaked across users") chat2 = c.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "второй", "member_ids": [alice["id"], bob["id"]]}, schema="Chat")["json"] third = post(c, alice, chat2, "оригинал", key)["json"] check(third["id"] != first["id"] and third["chat_id"] == chat2["id"], "key leaked across chats") print("ok - same key independent per user and per chat") for bad in ("", "with space", "кириллица", "x" * 129, "a/b"): try: post(c, alice, chat, "bad key", bad, expected=400) except UnicodeEncodeError: pass c.call("POST", "/api/v1/chats/" + chat["id"] + "/messages", 400, actor=alice, body={"text": "без ключа"}) post(c, alice, chat, "ok", "A-Za-z0-9._:-" + "k" * 115) print("ok - missing/invalid Idempotency-Key -> 400, max-length key accepted") post(c, eve, chat, "оригинал", key, expected=404) sid = Client(args.base_url) c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=bob) post(sid, bob, chat, "оригинал", key, expected=401) print("ok - replay needs a valid session and chat access (404 outsider, 401 revoked)") count = db_count(chat["id"]) texts = Counter(m["text"] for m in history(c, alice, chat)) check(all(n == 1 for t, n in texts.items() if t.startswith("одновременно")), "duplicates in history") print("ok - DB rows in chat: %s, history items: %d, no duplicate texts" % (count, sum(texts.values()))) print("PASS: shared idempotency") if __name__ == "__main__": run(main)