Bring the system changes of lab 3 from 61c0c28..72a1e57 onto the lab 4 code, resolving each conflict by meaning and keeping everything lab 4 added: - a message is posted with one SQL statement under the chat row lock, and the attachment links go into the same statement; a foreign key violation on the attachment link still answers 404 and leaves nothing behind. Text and the attachment list are checked before the write, an outsider gets 404 for both, as before the carry over; - the request pipeline counts the body bytes actually received against the limit of the route, so a chunked body over 1 MiB gets 413 and the upload route keeps its MAX_UPLOAD_BYTES limit; - a request cancelled at shutdown gets 503 unavailable in the contract format, the worker waits a second less than the rest of the shutdown budget, SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks 7 s; - readiness sits behind a Readiness port with the draining flag in shared infrastructure, and the drain window is logged through Logger; - logout revokes through SessionRevocations and withdraws the mark when PostgreSQL rejects the delete; an empty cursor is 400; - EventBus.project is gone, the sweep test subscribes and waits on a queue; domain tests check attachment_list instead of Chat.post; - make up waits for every replica in the balancer, BASE_URL follows the lb port, the image is tagged by the compose project, retry-check and faults-evidence are added, detection runs three times, failover-each and rolling-update record the stand usage, CI checks numeric uids. down also takes the tools and reconcile profiles of lab 4. Track 1 tooling of lab 3 (scaling.py, its samplers, tools/ and the CPU pinning of api, lb, valkey and k6) stays out; stand-usage.sh queries the database size itself instead of the track 1 sampler. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
157 lines
6.1 KiB
Python
157 lines
6.1 KiB
Python
from urllib.parse import quote
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
from conftest import actor, keyed, make_chat, register
|
|
|
|
TOTAL = 12
|
|
|
|
|
|
async def fill(client: AsyncClient, chat: dict, sender: dict, count: int) -> list[str]:
|
|
texts = []
|
|
for index in range(count):
|
|
text = f"сообщение {index:02d}"
|
|
response = await client.post(
|
|
f"/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": text},
|
|
headers=keyed(sender),
|
|
)
|
|
assert response.status_code == 201, response.text
|
|
texts.append(text)
|
|
return texts
|
|
|
|
|
|
async def walk(client: AsyncClient, chat: dict, reader: dict, limits: list[int]) -> list[str]:
|
|
collected: list[str] = []
|
|
cursor: str | None = None
|
|
step = 0
|
|
while True:
|
|
limit = limits[min(step, len(limits) - 1)]
|
|
query = f"?limit={limit}"
|
|
if cursor:
|
|
query += f"&cursor={quote(cursor, safe='')}"
|
|
response = await client.get(
|
|
f"/api/v1/chats/{chat['id']}/messages{query}", headers=actor(reader)
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
page = response.json()
|
|
assert len(page["items"]) <= limit
|
|
collected.extend(item["text"] for item in page["items"])
|
|
cursor = page["next_cursor"]
|
|
step += 1
|
|
if cursor is None:
|
|
return collected
|
|
|
|
|
|
async def test_full_walk_has_no_gaps_or_repeats(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
bob = await register(client, "bob")
|
|
chat = await make_chat(client, alice, bob)
|
|
expected = await fill(client, chat, alice, TOTAL)
|
|
assert await walk(client, chat, bob, [5]) == expected
|
|
|
|
|
|
async def test_limit_may_change_between_pages(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
expected = await fill(client, chat, alice, TOTAL)
|
|
assert await walk(client, chat, alice, [1, 7, 3, 100]) == expected
|
|
|
|
|
|
async def test_last_page_closes_history(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
await fill(client, chat, alice, 2)
|
|
first = (
|
|
await client.get(f"/api/v1/chats/{chat['id']}/messages?limit=2", headers=actor(alice))
|
|
).json()
|
|
assert len(first["items"]) == 2
|
|
assert first["next_cursor"] is None
|
|
|
|
|
|
async def test_cursor_is_shared_between_members(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
bob = await register(client, "bob")
|
|
chat = await make_chat(client, alice, bob)
|
|
await fill(client, chat, alice, 3)
|
|
page = (
|
|
await client.get(f"/api/v1/chats/{chat['id']}/messages?limit=1", headers=actor(alice))
|
|
).json()
|
|
cursor = quote(page["next_cursor"], safe="")
|
|
response = await client.get(
|
|
f"/api/v1/chats/{chat['id']}/messages?limit=1&cursor={cursor}", headers=actor(bob)
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
async def test_cursor_from_another_chat_is_rejected(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
first = await make_chat(client, alice)
|
|
second = await make_chat(client, alice)
|
|
await fill(client, first, alice, 3)
|
|
await fill(client, second, alice, 3)
|
|
page = (
|
|
await client.get(f"/api/v1/chats/{first['id']}/messages?limit=1", headers=actor(alice))
|
|
).json()
|
|
cursor = quote(page["next_cursor"], safe="")
|
|
response = await client.get(
|
|
f"/api/v1/chats/{second['id']}/messages?limit=1&cursor={cursor}", headers=actor(alice)
|
|
)
|
|
assert response.status_code == 400
|
|
assert response.json()["error"]["code"] == "invalid_request"
|
|
|
|
|
|
@pytest.mark.parametrize("cursor", ["garbage", "1.2.3.4", "%20", ""])
|
|
async def test_broken_cursor_is_rejected(client: AsyncClient, cursor: str) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
await fill(client, chat, alice, 1)
|
|
for path in (f"/api/v1/chats/{chat['id']}/messages", "/api/v1/chats"):
|
|
response = await client.get(f"{path}?limit=1&cursor={cursor}", headers=actor(alice))
|
|
assert response.status_code == 400, f"{path}?cursor={cursor} → {response.status_code}"
|
|
assert response.json()["error"]["code"] == "invalid_request"
|
|
|
|
|
|
@pytest.mark.parametrize("limit", ["0", "-1", "101", "abc", "1.5", ""])
|
|
async def test_limit_out_of_range(client: AsyncClient, limit: str) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
for path in (f"/api/v1/chats/{chat['id']}/messages", "/api/v1/chats"):
|
|
response = await client.get(f"{path}?limit={limit}", headers=actor(alice))
|
|
assert response.status_code == 400, f"{path}?limit={limit} → {response.status_code}"
|
|
|
|
|
|
async def test_default_limit_is_50(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
await fill(client, chat, alice, 51)
|
|
page = (await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=actor(alice))).json()
|
|
assert len(page["items"]) == 50
|
|
assert page["next_cursor"] is not None
|
|
|
|
|
|
async def test_unknown_query_parameter_is_ignored(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
chat = await make_chat(client, alice)
|
|
await fill(client, chat, alice, 2)
|
|
for path in (f"/api/v1/chats/{chat['id']}/messages", "/api/v1/chats"):
|
|
response = await client.get(f"{path}?limit=1&unknown=x", headers=actor(alice))
|
|
assert response.status_code == 200, response.text
|
|
assert len(response.json()["items"]) == 1
|
|
|
|
|
|
async def test_chat_list_pagination(client: AsyncClient) -> None:
|
|
alice = await register(client, "alice")
|
|
created = [(await make_chat(client, alice))["id"] for _ in range(5)]
|
|
collected: list[str] = []
|
|
cursor: str | None = None
|
|
while True:
|
|
query = "?limit=2" + (f"&cursor={quote(cursor, safe='')}" if cursor else "")
|
|
page = (await client.get(f"/api/v1/chats{query}", headers=actor(alice))).json()
|
|
collected.extend(item["id"] for item in page["items"])
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
assert collected == created
|