#!/usr/bin/env python3 import sys import threading import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from client import Client, arguments, check, run def main(): args = arguments("Lab3: concurrent idempotency and validation") c = Client(args.base_url, args.ca_file) c.call("POST", "/api/v1/users", 400, body={"username": "valid\n", "display_name": "Test", "password": "LongPassword123"}) registration = {"username": "race_" + uuid.uuid4().hex[:12], "display_name": "Race", "password": "LongPassword123"} registration_barrier = threading.Barrier(2) def register(_): local = Client(args.base_url, args.ca_file) registration_barrier.wait() return local.call("POST", "/api/v1/users", [201, 409], body=registration, schema="User")["status"] with ThreadPoolExecutor(max_workers=2) as pool: check(sorted(pool.map(register, range(2))) == [201, 409], "registration race") alice, bob, eve, chat = c.bootstrap() path = "/api/v1/chats/" + chat["id"] + "/messages" key = uuid.uuid4().hex barrier = threading.Barrier(12) def send(_): local = Client(args.base_url, args.ca_file) barrier.wait() return local.message(alice, chat, "Одна запись", key=key) with ThreadPoolExecutor(max_workers=12) as pool: responses = list(pool.map(send, range(12))) first = responses[0]["json"] check(all(r["json"] == first for r in responses), "concurrent replay differs") instances = {r["headers"]["X-Instance-Id"] for r in responses} check(len(instances) >= 2, "same key must reach at least two replicas") page = c.call("GET", path, actor=bob, schema="MessagePage")["json"] check(page["items"] == [first], "concurrent retry created duplicates") c.call("POST", path, 409, actor=alice, body={"text": "Изменённый текст"}, headers={"Idempotency-Key": key}) c.call("POST", path, 404, actor=eve, body={"text": "Одна запись"}, headers={"Idempotency-Key": key}) # The same key belongs to a different scope for Bob and for another chat. other = c.message(bob, chat, "Другая область", key=key)["json"] check(other["id"] != first["id"], "different users share an idempotency scope") second_chat = c.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Второй", "member_ids": [alice["id"]]}, schema="Chat")["json"] other = c.message(alice, second_chat, "Одна запись", key=key)["json"] check(other["id"] != first["id"], "different chats share an idempotency scope") for bad_key in ("", "bad key", "x" * 129): c.call("POST", path, 400, actor=alice, body={"text": "Текст"}, headers={"Idempotency-Key": bad_key}) for body in ({"text": " "}, {"text": 123}, {"text": "x", "sender_id": alice["id"]}): c.call("POST", path, 400, actor=alice, body=body, headers={"Idempotency-Key": uuid.uuid4().hex}) c.call("POST", path, 400, actor=alice, raw=b'{"text":', headers={"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex}) c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice) c.call("POST", path, 401, actor=alice, body={"text": "Одна запись"}, headers={"Idempotency-Key": key}) print("PASS: 12 concurrent requests across %d replicas, one message" % len(instances)) print("PASS: conflict, key scope, validation, access and revoked session") print("PASS: concurrent registration, exactly one 201 and one 409") if __name__ == "__main__": run(main)