Files
mtusi2026lab3/student-tests/test_race.py
T
ricoromaandClaude Opus 5.5 af4bca08c9 feat: carry lab 2 over and add X-Instance-Id and idempotency keys
- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec
  change instance-id-idempotency)
- rename the compose project to mtusi-lab3 and the image to :lab3
- add X-Instance-Id to every response and instance_id to every log
  record ([app] instance_id, hostname by default)
- require Idempotency-Key on POST messages: keys table in Postgres,
  key and message in one transaction, replay returns the same 201,
  another body gets 409, TTL from [idempotency] ttl_seconds
- send the key from student-tests, k6 scenarios and the skill driver;
  add idempotency, X-Instance-Id, key race and fault tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 19:42:36 +03:00

180 lines
6.6 KiB
Python

"""Гонки: одновременные запросы по отдельным соединениям."""
import threading
import time
from collections import Counter
from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from typing import Any
import httpx
import pytest
from api_client import (
API,
Actor,
Api,
new_key,
order_key,
registration,
unique_username,
)
from contract import assert_error, assert_ok
CLIENTS = 10
MESSAGES_PER_CLIENT = 10
HISTORY_PAGE = 30
def run_together[T](work: Callable[[Api, int], T], base_url: str) -> list[T]:
"""`CLIENTS` потоков со своим соединением стартуют за барьером."""
barrier = threading.Barrier(CLIENTS)
def client(number: int) -> T:
with Api(base_url) as own:
# соединение открыто до барьера: после него сразу запросы
own.request("GET", "/health/live")
barrier.wait()
return work(own, number)
with ThreadPoolExecutor(max_workers=CLIENTS) as pool:
return list(pool.map(client, range(CLIENTS)))
def max_in_flight(spans: list[tuple[float, float]]) -> int:
"""Сколько запросов максимум было в полёте одновременно."""
# при равенстве времени конец считается раньше начала
events = sorted(
[(start, 1) for start, _ in spans] + [(end, -1) for _, end in spans],
)
current = peak = 0
for _, delta in events:
current += delta
peak = max(peak, current)
return peak
@pytest.mark.spec("data-storage", "100 сообщений от 10 клиентов")
def test_hundred_messages_from_ten_clients(api: Api, base_url: str) -> None:
actors = [
api.register(display_name=f"Racer {number}")
for number in range(CLIENTS)
]
chat = api.create_chat(actors[0], *actors[1:], title="Race")
def send_all(
own: Api,
number: int,
) -> list[tuple[str, httpx.Response, float, float]]:
results = []
for index in range(MESSAGES_PER_CLIENT):
text = f"race-{number}-{index}"
start = time.monotonic()
# у каждой отправки свой ключ: это 100 разных сообщений
resp = own.post_message(
actors[number],
chat["id"],
json={"text": text},
)
results.append((text, resp, start, time.monotonic()))
return results
results = [
row for rows in run_together(send_all, base_url) for row in rows
]
statuses = Counter(resp.status_code for _, resp, _, _ in results)
peak = max_in_flight([(start, end) for _, _, start, end in results])
assert statuses == {201: CLIENTS * MESSAGES_PER_CLIENT}, statuses
created = {}
for text, resp, _, _ in results:
message = assert_ok(resp, 201, "Message")
assert message["text"] == text
created[message["id"]] = message
pages = api.message_pages(actors[0], chat["id"], limits=(HISTORY_PAGE,))
items = [item for page in pages for item in page["items"]]
ids = [item["id"] for item in items]
print(
f"race: {CLIENTS} clients x {MESSAGES_PER_CLIENT} messages, "
f"statuses {dict(statuses)}, max in flight {peak}; "
f"history: {len(items)} messages, {len(set(ids))} unique ids, "
f"{len(pages)} pages by {HISTORY_PAGE}",
)
assert peak >= 2, "requests did not overlap: race is not proven"
assert len(items) == CLIENTS * MESSAGES_PER_CLIENT
assert len(set(ids)) == len(ids)
assert set(ids) == created.keys()
assert items == sorted(created.values(), key=order_key)
senders = {actor.id for actor in actors}
assert {item["sender_id"] for item in items} == senders
@pytest.mark.spec("users", "Одновременная регистрация")
@pytest.mark.spec("data-storage", "Гонка за username")
def test_same_username_at_once(base_url: str) -> None:
username = unique_username()
def register(own: Api, number: int) -> tuple[httpx.Response, float, float]:
start = time.monotonic()
resp = own.request(
"POST",
f"{API}/users",
json=registration(username, f"Racer {number}"),
)
return resp, start, time.monotonic()
results = run_together(register, base_url)
statuses = Counter(resp.status_code for resp, _, _ in results)
peak = max_in_flight([(start, end) for _, start, end in results])
print(
f"username race: {CLIENTS} clients, statuses {dict(statuses)}, "
f"max in flight {peak}",
)
assert peak >= 2, "requests did not overlap: race is not proven"
assert statuses == {201: 1, 409: CLIENTS - 1}, statuses
for resp, _, _ in results:
if resp.status_code == 409:
assert_error(resp, 409)
else:
assert assert_ok(resp, 201, "User")["username"] == username
@pytest.mark.spec("idempotency", "Гонка одного ключа")
def test_same_key_at_once(
api: Api,
alice: Actor,
chat: dict[str, Any],
base_url: str,
) -> None:
"""Спек `student-tests`, сценарий «Гонка одного ключа»: одно тело с
одним ключом от `CLIENTS` клиентов сразу — ровно одно сообщение.
Против одного процесса API гонку решает PostgreSQL так же, как между
репликами; ответы разных реплик требует change `replicas-lb`.
"""
key = new_key()
body = {"text": "один раз"}
def send(own: Api, _number: int) -> tuple[httpx.Response, float, float]:
start = time.monotonic()
resp = own.post_message(alice, chat["id"], key=key, json=body)
return resp, start, time.monotonic()
results = run_together(send, base_url)
statuses = Counter(resp.status_code for resp, _, _ in results)
peak = max_in_flight([(start, end) for _, start, end in results])
history = api.all_messages(alice, chat["id"])
print(
f"key race: {CLIENTS} clients, statuses {dict(statuses)}, "
f"max in flight {peak}; history: {len(history)} messages",
)
assert peak >= 2, "requests did not overlap: race is not proven"
assert statuses == {201: CLIENTS}, statuses
messages = [assert_ok(resp, 201, "Message") for resp, _, _ in results]
assert all(message == messages[0] for message in messages)
assert history == [messages[0]]