"""10 clients × 10 messages = 100 unique messages.""" import threading import uuid from client import Client from student_tests.test_api import actor_of def test_concurrent_messages(live: Client): users = [actor_of(live) for _ in range(10)] member_ids = [u["id"] for u in users] chat = live.call( "POST", "/api/v1/chats", 201, actor=users[0], body={"title": "load", "member_ids": member_ids}, schema="Chat", )["json"] errors = [] lock = threading.Lock() def worker(user): local = Client(live.base, timeout=15) for _ in range(10): try: local.message(user, chat, uuid.uuid4().hex) except Exception as exc: with lock: errors.append(str(exc)) threads = [threading.Thread(target=worker, args=(u,)) for u in users] for thread in threads: thread.start() for thread in threads: thread.join(timeout=60) assert not errors, "unexpected failures: %s" % errors[:5] page = live.call( "GET", "/api/v1/chats/%s/messages?limit=100" % chat["id"], actor=users[0], schema="MessagePage", )["json"] ids = [item["id"] for item in page["items"]] assert len(ids) == len(set(ids)) == 100