Files
mtusi2026lab6/student-tests/test_race.py
T
ricoromaandClaude Opus 5.5 9cc6c0c7a6 chore: port lab 5 solution into the lab 6 skeleton
Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e.
Contracts, public tests and evidence of lab 5 are not carried over.
The compose project, image and CI prefix are renamed to lab6; the first
broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the
OpenSpec changes of lab 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 04:46:56 +03:00

235 lines
8.8 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_instance_id,
assert_ok,
assert_replicas,
)
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
так же, как между репликами. Ответы разных реплик требует
`test_same_key_through_replicas`.
"""
results = race_one_key(api, alice, chat, base_url)
peak = max_in_flight([(start, end) for _, start, end in results])
assert peak >= 2, "requests did not overlap: race is not proven"
@pytest.mark.spec("idempotency", "Гонка одного ключа через реплики")
def test_same_key_through_replicas(
api: Api,
alice: Actor,
chat: dict[str, Any],
base_url: str,
replicas: int,
) -> None:
"""Та же гонка через балансировщик: среди ответов есть пара
пересекающихся по времени запросов, которые обслужили разные реплики.
Это и есть «одновременно через разные реплики», а не просто «разные
реплики за весь прогон». Против одного процесса — пропуск."""
results = race_one_key(api, alice, chat, base_url)
spans = [
(assert_instance_id(resp), start, end) for resp, start, end in results
]
served = Counter(instance for instance, _, _ in spans)
print(f"key race through replicas: {dict(served)}")
assert_replicas(served, replicas)
assert overlap_on_different_replicas(spans), (
f"no overlapping requests on different replicas: {dict(served)}"
)
def race_one_key(
api: Api,
alice: Actor,
chat: dict[str, Any],
base_url: str,
) -> list[tuple[httpx.Response, float, float]]:
"""`CLIENTS` клиентов сразу шлют одно тело с одним ключом: все 201 с
одним `Message`, в истории ровно одно сообщение. Возвращает ответы со
временем начала и конца."""
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 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]]
return results
def overlap_on_different_replicas(
spans: list[tuple[str, float, float]],
) -> bool:
"""Есть два запроса разных реплик, которые были в полёте одновременно."""
return any(
first[0] != second[0] and first[1] < second[2] and second[1] < first[2]
for number, first in enumerate(spans)
for second in spans[number + 1 :]
)