from collections import Counter from concurrent.futures import ThreadPoolExecutor import json from pathlib import Path import sys import threading import time import uuid ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, check, run from resilience import history WORKERS = 12 ROUNDS = 3 def main(): args = arguments("Concurrent cross-replica idempotency") control = Client( args.base_url, args.ca_file, timeout=10, ) alice, bob, eve, chat = control.bootstrap() path = f"/api/v1/chats/{chat['id']}/messages" check( history(control, alice, chat) == [], "fresh test chat is not empty", ) clients = [ Client(args.base_url, args.ca_file, timeout=10) for _ in range(WORKERS) ] saved_messages = [] round_results = [] first_key = None for round_number in range(1, ROUNDS + 1): key = uuid.uuid4().hex text = f"Один ключ — одно сообщение: {round_number} {key}" if first_key is None: first_key = key barrier = threading.Barrier(WORKERS) def send(index): if index % 3 == 0: body = {"text": text} elif index % 3 == 1: body = { "attachment_ids": [], "text": text, } else: body = { "text": text, "attachment_ids": [], } raw = json.dumps( body, ensure_ascii=False, indent=2 if index % 2 else None, ).encode("utf-8") barrier.wait(timeout=15) return clients[index].call( "POST", path, expected=201, actor=alice, raw=raw, headers={ "Content-Type": "application/json", "Idempotency-Key": key, }, schema="Message", ) started = time.monotonic() with ThreadPoolExecutor(max_workers=WORKERS) as executor: responses = list(executor.map(send, range(WORKERS))) elapsed = time.monotonic() - started original = responses[0]["json"] check( all( response["json"] == original for response in responses ), "same key returned different Message objects", ) instances = Counter( response["headers"]["X-Instance-Id"] for response in responses ) check( len(instances) >= 2, "concurrent requests did not reach two replicas", ) messages = history(control, bob, chat) same_text = [ message for message in messages if message["text"] == text ] check( same_text == [original], "concurrent repeats created zero or multiple messages", ) check( len(messages) == round_number, "unexpected message count after concurrent round", ) saved_messages.append(original) result = { "round": round_number, "requests": WORKERS, "responses_201": len(responses), "observed_instances": len(instances), "responses_by_instance": dict(instances), "unique_message_ids": len( { response["json"]["id"] for response in responses } ), "matching_history_messages": len(same_text), "duration_seconds": round(elapsed, 3), } round_results.append(result) print(json.dumps(result, ensure_ascii=False)) conflict = control.call( "POST", path, expected=409, actor=alice, body={"text": "Другой текст с прежним ключом"}, headers={"Idempotency-Key": first_key}, ) check( conflict["json"]["error"]["code"] == "conflict", "wrong error code for body conflict", ) control.call( "POST", path, expected=400, actor=alice, body={"text": "Без ключа"}, ) for invalid_key in ("bad key", "x" * 129): control.call( "POST", path, expected=400, actor=alice, body={"text": "Некорректный ключ"}, headers={"Idempotency-Key": invalid_key}, ) control.call( "POST", path, expected=404, actor=eve, body={"text": saved_messages[0]["text"]}, headers={"Idempotency-Key": first_key}, ) bob_message = control.message( bob, chat, "Тот же ключ другого пользователя", key=first_key, )["json"] check( bob_message["sender_id"] == bob["id"], "wrong sender for another user's scope", ) check( bob_message["id"] != saved_messages[0]["id"], "key incorrectly shared between users", ) second_chat = control.call( "POST", "/api/v1/chats", expected=201, actor=alice, body={ "title": "Другой чат для области ключа", "member_ids": [alice["id"], bob["id"]], }, schema="Chat", )["json"] other_chat_message = control.message( alice, second_chat, "Тот же ключ в другом чате", key=first_key, )["json"] check( other_chat_message["chat_id"] == second_chat["id"], "wrong chat in scoped result", ) check( other_chat_message["id"] != saved_messages[0]["id"], "key incorrectly shared between chats", ) replay = control.message( alice, chat, saved_messages[0]["text"], key=first_key, )["json"] check( replay == saved_messages[0], "original result changed after scope tests", ) messages = history(control, bob, chat) expected = saved_messages + [bob_message] check( len(messages) == len(expected), "unexpected final message count", ) check( {message["id"] for message in messages} == {message["id"] for message in expected}, "missing or extra messages in first chat", ) check( history(control, bob, second_chat) == [other_chat_message], "unexpected history in second chat", ) control.call( "DELETE", "/api/v1/auth/sessions/current", expected=204, actor=alice, ) control.call( "POST", path, expected=401, actor=alice, body={"text": saved_messages[0]["text"]}, headers={"Idempotency-Key": first_key}, ) print( json.dumps( { "rounds": ROUNDS, "concurrent_requests_per_round": WORKERS, "concurrent_requests_total": WORKERS * ROUNDS, "body_conflict_409": True, "missing_and_invalid_key_400": True, "foreign_user_404": True, "user_scope_independent": True, "chat_scope_independent": True, "replay_preserved": True, "revoked_session_401": True, "unexpected_messages": 0, }, ensure_ascii=False, ) ) print( "PASS: concurrent cross-replica idempotency, " "scope, validation and authorization checks." ) if __name__ == "__main__": run(main)