forked from mtusi2026/mtusi2026lab2
ЛР 2: PostgreSQL, сессии, Valkey и исследование восстановления
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Level-4 checks: concurrent unique registration and atomic chat creation."""
|
||||
import concurrent.futures
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
|
||||
from client import Client, Failure, arguments, check, run
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def db_count(table):
|
||||
# Fixed table allowlist: user input is never interpolated here.
|
||||
assert table in {"users", "chats", "chat_members", "messages"}
|
||||
output = subprocess.run(["docker", "compose", "--env-file", ".env", "exec", "-T", "postgres",
|
||||
"psql", "-X", "-U", "messenger", "-d", "messenger", "-At", "-c",
|
||||
"SELECT count(*) FROM " + table], cwd=ROOT, check=True, text=True,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout
|
||||
return int(output.strip())
|
||||
|
||||
def main():
|
||||
args = arguments("Check registration race and transaction rollback")
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, _, _ = c.bootstrap()
|
||||
username = "race_" + uuid.uuid4().hex[:16]
|
||||
body = {"username": username, "display_name": "Гонка регистрации", "password": "Test-password-" + uuid.uuid4().hex}
|
||||
def register(_):
|
||||
try:
|
||||
return c.call("POST", "/api/v1/users", [201, 409], body=body)["status"]
|
||||
except Failure:
|
||||
raise
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as pool:
|
||||
statuses = list(pool.map(register, range(12)))
|
||||
check(statuses.count(201) == 1 and statuses.count(409) == 11,
|
||||
"username race must create exactly one user and return 409 to all other requests")
|
||||
before = db_count("chats")
|
||||
after = c.call("POST", "/api/v1/chats", 400, actor=alice,
|
||||
body={"title": "rollback probe", "member_ids": [alice["id"], str(uuid.uuid4())]})
|
||||
check(after["status"] == 400 and db_count("chats") == before,
|
||||
"invalid multi-step chat creation left a partial chat row")
|
||||
plan = subprocess.run(["docker", "compose", "--env-file", ".env", "exec", "-T", "postgres",
|
||||
"psql", "-X", "-U", "messenger", "-d", "messenger", "-At", "-c",
|
||||
"EXPLAIN (ANALYZE, BUFFERS) SELECT id FROM messages WHERE chat_id=md5('chat-0')::uuid ORDER BY created_at,id LIMIT 50"],
|
||||
cwd=ROOT, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.splitlines()
|
||||
print(json.dumps({"registration_race": {"201": statuses.count(201), "409": statuses.count(409)},
|
||||
"chat_rollback": "PASS", "history_index_plan": plan}, ensure_ascii=False, indent=2))
|
||||
|
||||
if __name__ == "__main__": run(main)
|
||||
Reference in New Issue
Block a user