"""Concurrent retries through the LB must reach different real API processes.""" import sys 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 args = arguments('Shared idempotency across replicas') c = Client(args.base_url, args.ca_file) a, b, eve, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] + '/messages' key = uuid.uuid4().hex def send(i): client = Client(args.base_url, args.ca_file) body = {'text': 'Один результат 👋'} if i % 2: body['attachment_ids'] = [] return client.call('POST', path, 201, actor=a, body=body, headers={'Idempotency-Key': key}, schema='Message') with ThreadPoolExecutor(max_workers=16) as pool: responses = list(pool.map(send, range(32))) assert len({r['headers']['X-Instance-Id'] for r in responses}) >= 2 assert all(r['json'] == responses[0]['json'] for r in responses) page = c.call('GET', path, actor=b, schema='MessagePage')['json'] assert page['items'] == [responses[0]['json']] c.call('POST', path, 409, actor=a, body={'text': 'Другой текст'}, headers={'Idempotency-Key': key}) c.call('POST', path, 404, actor=eve, body={'text': 'Один результат 👋'}, headers={'Idempotency-Key': key}) other = c.message(b, chat, 'Другой пользователь', key=key)['json'] assert other['id'] != responses[0]['json']['id'] second_chat = c.call('POST', '/api/v1/chats', 201, actor=a, body={'title': 'Другой чат', 'member_ids': [a['id']]}, schema='Chat')['json'] assert c.message(a, second_chat, 'Другой чат', key=key)['json']['id'] != other['id'] for bad_key in ('', 'has spaces', 'x' * 129): c.call('POST', path, 400, actor=a, body={'text': 'Invalid key'}, headers={'Idempotency-Key': bad_key}) print('PASS: 32 concurrent retries across >=2 replicas: one message; conflict, access, key scope and validation')