#!/usr/bin/env python3 """Same Idempotency-Key sent concurrently through the LB must hit several replicas and create one message.""" import argparse import json import sys import threading import uuid from collections import Counter from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, Failure, check, run # noqa: E402 def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--base-url', default=None) p.add_argument('--parallel', type=int, default=20) p.add_argument('--rounds', type=int, default=5) args = p.parse_args() c = Client(args.base_url) alice, bob, _, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] + '/messages' instances = Counter() for round_no in range(args.rounds): key = 'race-' + uuid.uuid4().hex text = 'Конкурентный повтор %d %s' % (round_no, key) barrier = threading.Barrier(args.parallel) results, errors = [], [] def send(): barrier.wait() try: r = c.call('POST', path, 201, actor=alice, body={'text': text}, headers={'Idempotency-Key': key}, schema='Message') results.append((r['headers']['X-Instance-Id'], r['json'])) except Failure as exc: errors.append(str(exc)) threads = [threading.Thread(target=send) for _ in range(args.parallel)] for t in threads: t.start() for t in threads: t.join() check(not errors, 'concurrent POST failed: ' + '; '.join(errors[:3])) bodies = {json.dumps(body, sort_keys=True) for _, body in results} check(len(bodies) == 1, 'replicas returned different messages for one key') served = Counter(instance for instance, _ in results) instances.update(served) print('round %d: %d responses, 1 message, replicas %s' % (round_no, len(results), dict(served))) c.call('POST', path, 409, actor=alice, body={'text': text + ' другой'}, headers={'Idempotency-Key': key}) page = c.call('GET', path + '?limit=100', actor=bob, schema='MessagePage')['json'] texts = [m['text'] for m in page['items']] check(len(texts) == args.rounds and len(set(texts)) == args.rounds, 'expected exactly one message per key') check(len(instances) >= 2, 'all requests hit one replica; LB does not balance') print('PASS: %d keys x %d parallel requests -> %d messages via %d replicas %s' % (args.rounds, args.parallel, len(texts), len(instances), dict(instances))) if __name__ == '__main__': run(main)