Files
mtusi2026lab2/student_tests/test_concurrent.py
T
dawuduk 8f1d7e27c7
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / application (pull_request) Canceled after 0s
reworked
2026-11-07 11:56:38 +03:00

49 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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