forked from mtusi2026/mtusi2026lab2
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Проверки гонки регистрации и rollback создания чата."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
import sys
|
|
import uuid
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
|
from client import Client, check, run # noqa: E402
|
|
|
|
|
|
def main() -> None:
|
|
client = Client()
|
|
alice, bob, _eve, chat = client.bootstrap()
|
|
|
|
username = "race_" + uuid.uuid4().hex[:12]
|
|
body = {
|
|
"username": username,
|
|
"display_name": "Гонка регистрации",
|
|
"password": "Race-password-" + uuid.uuid4().hex,
|
|
}
|
|
|
|
def register_once() -> int:
|
|
local = Client()
|
|
return local.call("POST", "/api/v1/users", expected=[201, 409], body=body)["status"]
|
|
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
statuses = list(pool.map(lambda _unused: register_once(), range(8)))
|
|
check(statuses.count(201) == 1 and statuses.count(409) == 7, "username race was not serialized")
|
|
|
|
title = "rollback-" + uuid.uuid4().hex
|
|
client.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
400,
|
|
actor=alice,
|
|
body={"title": title, "member_ids": [alice["id"], str(uuid.uuid4())]},
|
|
)
|
|
chats = client.call(
|
|
"GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage"
|
|
)["json"]["items"]
|
|
check(all(chat["title"] != title for chat in chats), "failed chat creation left a chat")
|
|
|
|
first = client.message(alice, chat, "before cached read")["json"]
|
|
client.call(
|
|
"GET",
|
|
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
|
actor=alice,
|
|
schema="MessagePage",
|
|
)
|
|
second = client.message(bob, chat, "after cached read")["json"]
|
|
history = client.call(
|
|
"GET",
|
|
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
|
actor=alice,
|
|
schema="MessagePage",
|
|
)["json"]["items"]
|
|
check(
|
|
{message["id"] for message in history} == {first["id"], second["id"]},
|
|
"history cache returned stale data after write",
|
|
)
|
|
print("PASS: registration race, chat rollback and cache read-after-write")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|