forked from mtusi2026/mtusi2026lab3
35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
"""Grade-4 integrity checks: registration race and composite-op rollback."""
|
|
from __future__ import annotations
|
|
|
|
import concurrent.futures
|
|
import os
|
|
|
|
|
|
def test_concurrent_duplicate_username_single_winner(client):
|
|
username = "race_dup_" + os.urandom(4).hex()
|
|
password = "T3st-" + os.urandom(8).hex()
|
|
|
|
def register(_):
|
|
return client.register(username, password).status
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
|
|
statuses = list(pool.map(register, range(5)))
|
|
|
|
assert statuses.count(201) == 1, statuses
|
|
assert statuses.count(409) == 4, statuses
|
|
|
|
|
|
def test_failed_chat_creation_rolls_back(client, alice):
|
|
def chats_count():
|
|
return len(client.request("GET", "/api/v1/chats?limit=100", token=alice["token"]).json["items"])
|
|
|
|
before = chats_count()
|
|
unknown = "00000000-0000-4000-8000-000000000000"
|
|
response = client.create_chat(alice["token"], [alice["id"], unknown], title="should-rollback")
|
|
assert response.status == 400, response.body
|
|
after = chats_count()
|
|
assert after == before, "failed composite operation left a partial chat"
|
|
|
|
listed = client.request("GET", "/api/v1/chats?limit=100", token=alice["token"]).json["items"]
|
|
assert all(item["title"] != "should-rollback" for item in listed)
|