Files
mtusi2026lab2/student_tests/test_grade4.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

45 lines
1.4 KiB
Python

"""Grade-4: username race and atomic chat rollback."""
import threading
import uuid
from client import Client
from student_tests.test_api import PASSWORD, actor_of
def test_register_race(live: Client):
username = "r_" + uuid.uuid4().hex[:12]
body = {"username": username, "display_name": "Race", "password": PASSWORD}
results = []
def once():
local = Client(live.base, timeout=20)
try:
results.append(local.call("POST", "/api/v1/users", [201, 409], body=body)["status"])
except Exception as exc:
results.append(str(exc))
threads = [threading.Thread(target=once) for _ in range(16)]
for t in threads:
t.start()
for t in threads:
t.join()
assert results.count(201) == 1, results
assert results.count(409) == 15, results
def test_chat_rollback(live: Client):
alice = actor_of(live)
before = live.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"]
missing = str(uuid.uuid4())
live.call(
"POST",
"/api/v1/chats",
400,
actor=alice,
body={"title": "rollback-should-not-exist", "member_ids": [alice["id"], missing]},
)
after = live.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"]
assert len(after) == len(before)
assert all(x["title"] != "rollback-should-not-exist" for x in after)