Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.
This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
import asyncio
|
|
from urllib.parse import quote
|
|
from uuid import uuid4
|
|
|
|
from httpx import AsyncClient
|
|
|
|
from conftest import PASSWORD, actor, keyed, make_chat, register
|
|
|
|
CLIENTS = 10
|
|
PER_CLIENT = 10
|
|
TOTAL = CLIENTS * PER_CLIENT
|
|
|
|
|
|
async def drain(client: AsyncClient, chat: dict, reader: dict) -> list[dict]:
|
|
collected: list[dict] = []
|
|
cursor: str | None = None
|
|
while True:
|
|
query = "?limit=100" + (f"&cursor={quote(cursor, safe='')}" if cursor else "")
|
|
page = (
|
|
await client.get(f"/api/v1/chats/{chat['id']}/messages{query}", headers=actor(reader))
|
|
).json()
|
|
collected.extend(page["items"])
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
return collected
|
|
|
|
|
|
async def flood(client: AsyncClient, chat: dict, sender: dict) -> list[int]:
|
|
async def writer(worker: int) -> list[int]:
|
|
codes = []
|
|
for index in range(PER_CLIENT):
|
|
response = await client.post(
|
|
f"/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": f"нагрузка-{worker}-{index}"},
|
|
headers=keyed(sender),
|
|
)
|
|
codes.append(response.status_code)
|
|
return codes
|
|
|
|
batches = await asyncio.gather(*(writer(worker) for worker in range(CLIENTS)))
|
|
return [code for batch in batches for code in batch]
|
|
|
|
|
|
async def test_hundred_messages_from_ten_clients(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
bob = await register(client, "bob")
|
|
chat = await make_chat(client, alice, bob)
|
|
|
|
codes = await flood(client, chat, alice)
|
|
assert codes.count(201) == TOTAL, f"неуспешные ответы: {set(codes) - {201}}"
|
|
|
|
history = await drain(client, chat, bob)
|
|
assert len(history) == TOTAL
|
|
assert len({item["id"] for item in history}) == TOTAL
|
|
assert len({item["text"] for item in history}) == TOTAL
|
|
|
|
keys = [(item["created_at"], item["id"]) for item in history]
|
|
assert keys == sorted(keys), "история не упорядочена по (created_at, id)"
|
|
stamps = [item["created_at"] for item in history]
|
|
assert len(set(stamps)) == TOTAL, "created_at внутри чата обязан строго возрастать"
|
|
|
|
|
|
async def test_parallel_registration_keeps_username_unique(client: AsyncClient) -> None:
|
|
body = {"username": f"race_{uuid4().hex[:12]}", "display_name": "Гонка", "password": PASSWORD}
|
|
responses = await asyncio.gather(*(client.post("/api/v1/users", json=body) for _ in range(20)))
|
|
codes = [response.status_code for response in responses]
|
|
assert codes.count(201) == 1
|
|
assert codes.count(409) == len(codes) - 1
|
|
|
|
|
|
async def test_parallel_chat_creation_is_atomic(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
bob = await register(client, "bob")
|
|
responses = await asyncio.gather(
|
|
*(
|
|
client.post(
|
|
"/api/v1/chats",
|
|
json={"title": f"Чат {index}", "member_ids": [alice["id"], bob["id"]]},
|
|
headers=actor(alice),
|
|
)
|
|
for index in range(20)
|
|
)
|
|
)
|
|
assert all(response.status_code == 201 for response in responses)
|
|
listed = (await client.get("/api/v1/chats?limit=100", headers=actor(bob))).json()
|
|
assert len(listed["items"]) == 20
|
|
keys = [(item["created_at"], item["id"]) for item in listed["items"]]
|
|
assert keys == sorted(keys)
|