PostgreSQL как источник истины, непрозрачные сессии в Redis с TTL и AOF, миграции под advisory lock, пул и таймауты, проверки гонки/отката/отказов и отчёт с evidence.
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Concurrent registration with the same username: one 201, the rest 409."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from common import HttpClient, assert_status, error_code
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--base-url", default="http://localhost:8080")
|
|
parser.add_argument("--workers", type=int, default=8)
|
|
args = parser.parse_args()
|
|
client = HttpClient(args.base_url)
|
|
username = "race_" + uuid.uuid4().hex[:16]
|
|
body = {
|
|
"username": username,
|
|
"display_name": "Race candidate",
|
|
"password": "T3st-" + uuid.uuid4().hex,
|
|
}
|
|
|
|
def register(_index: int):
|
|
status, _, payload, _ = client.call("POST", "/api/v1/users", body)
|
|
return status, error_code(payload)
|
|
|
|
with ThreadPoolExecutor(max_workers=args.workers) as executor:
|
|
results = list(executor.map(register, range(args.workers)))
|
|
|
|
statuses = [status for status, _code in results]
|
|
if statuses.count(201) != 1 or statuses.count(409) != args.workers - 1:
|
|
raise AssertionError(f"expected one 201 and {args.workers - 1} conflicts, got {statuses}")
|
|
for status, code in results:
|
|
if status == 409 and code != "conflict":
|
|
raise AssertionError("409 must carry conflict code")
|
|
|
|
session = client.login(body)
|
|
assert session["user"]["username"] == username
|
|
print(f"PASS: {args.workers} concurrent registrations produced exactly one user and one session")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|