"""Concurrent shared-key requests through LB, real instance headers and history.""" from concurrent.futures import ThreadPoolExecutor import json import threading import uuid from client import Client, arguments, check, run def main(): args = arguments(__doc__) c = Client(args.base_url) alice, bob, eve, chat = c.bootstrap() key = uuid.uuid4().hex barrier = threading.Barrier(16) def worker(_): local = Client(args.base_url) barrier.wait() return local.message(alice, chat, ' concurrent body ', key=key) with ThreadPoolExecutor(max_workers=16) as pool: responses = list(pool.map(worker, range(16))) ids = {r['json']['id'] for r in responses} instances = {r['headers']['X-Instance-Id'] for r in responses} check(len(ids) == 1, 'concurrent duplicate messages') check(len(instances) >= 2, 'concurrent requests did not reach two replicas') path = '/api/v1/chats/' + chat['id'] + '/messages' c.call('POST', path, 409, actor=alice, body={'text': 'concurrent body'}, headers={'Idempotency-Key': key}) again = c.call('POST', path, 201, actor=alice, body={'attachment_ids': [], 'text': ' concurrent body '}, headers={'Idempotency-Key': key}, schema='Message') check(again['json'] == responses[0]['json'], 'semantic replay differs') page = c.call('GET', path, actor=bob, schema='MessagePage')['json'] check(len(page['items']) == 1, 'history contains duplicate effects') c.message(bob, chat, 'other user', key=key) print(json.dumps({'parallel_requests': 16, 'observed_instances': sorted(instances), 'unique_messages': len(ids), 'conflict': 409, 'semantic_replay': True, 'user_scope': True})) if __name__ == '__main__': run(main)