#!/usr/bin/env python3 """Конкурентная идемпотентность через LB: N параллельных POST с ОДНИМ Idempotency-Key. Ожидание: все ответы 201 с одним и тем же Message; запросы обслужили >= 2 разные реплики; в истории ровно одно сообщение. Затем тот же ключ с другим телом -> 409, а одинаковый ключ у другого пользователя/чата независим. Повторяется --rounds раз с новыми ключами. """ from concurrent.futures import ThreadPoolExecutor import json from pathlib import Path import sys import threading import uuid sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, Failure, arguments, check, run # noqa: E402 def history(c, actor, chat): return c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', actor=actor, schema='MessagePage')['json']['items'] def main(): def configure(p): p.add_argument('--parallel', type=int, default=16) p.add_argument('--rounds', type=int, default=10) p.add_argument('--output', default=None) args = arguments('Concurrent Idempotency-Key check through the load balancer', configure) c = Client(args.base_url, args.ca_file) alice, bob, _eve, chat = c.bootstrap() other_chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Второй чат', 'member_ids': [alice['id']]}, schema='Chat')['json'] rounds = [] for r in range(args.rounds): key = 'race-' + uuid.uuid4().hex text = 'Гонка %d %s' % (r, key) barrier = threading.Barrier(args.parallel) def send(_): local = Client(args.base_url, args.ca_file) barrier.wait() resp = local.message(alice, chat, text, key=key) return resp['json'], resp['headers']['X-Instance-Id'], resp['headers'].get('Idempotent-Replayed') with ThreadPoolExecutor(max_workers=args.parallel) as pool: results = list(pool.map(send, range(args.parallel))) ids = {msg['id'] for msg, _, _ in results} instances = {inst for _, inst, _ in results} replays = sum(1 for _, _, rep in results if rep == 'true') check(len(ids) == 1, 'round %d: concurrent requests created %d different messages' % (r, len(ids))) check(all(msg == results[0][0] for msg, _, _ in results), 'round %d: responses differ' % r) check(replays == args.parallel - 1, 'round %d: expected exactly one original response' % r) same_text = [m for m in history(c, bob, chat) if m['text'] == text] check(len(same_text) == 1, 'round %d: history has %d copies' % (r, len(same_text))) # Тот же ключ с другим телом — конфликт; тот же ключ у Боба и в другом чате — независимы. c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice, body={'text': 'другое тело'}, headers={'Idempotency-Key': key}) bob_msg = c.message(bob, chat, text, key=key)['json'] check(bob_msg['id'] not in ids and bob_msg['sender_id'] == bob['id'], 'key scope must include user') other = c.message(alice, other_chat, text, key=key)['json'] check(other['id'] not in ids, 'key scope must include chat') rounds.append({'round': r, 'parallel': args.parallel, 'distinct_messages': len(ids), 'instances': len(instances), 'replayed_responses': replays}) multi = sum(1 for x in rounds if x['instances'] >= 2) check(multi > 0, 'concurrent requests never reached two replicas; check LB') report = {'test': 'concurrent idempotency through LB', 'parallel_requests_per_round': args.parallel, 'rounds': args.rounds, 'rounds_spanning_two_or_more_replicas': multi, 'duplicates_created': 0, 'per_round': rounds} text = json.dumps(report, ensure_ascii=False, indent=2) if args.output: Path(args.output).write_text(text + '\n', encoding='utf-8') print(text) print('PASS: %d rounds × %d parallel requests with one key -> exactly one message each' % (args.rounds, args.parallel)) if __name__ == '__main__': run(main)