Carry over the lab 2 fixes made after 8cddb09

Brings the system changes of lab 2 from 8cddb09..102579e onto the lab 3
code, resolving each conflict by meaning:

- the request pipeline counts the body bytes actually received, so a
  chunked body over the limit gets 413 instead of reaching the app;
- logout revokes through a SessionRevocations port and withdraws the
  mark when PostgreSQL rejects the delete, so 503 means the session
  still works; PostgresSessionDirectory loses its no-op invalidate;
- an empty cursor is 400 on history and on the chat list;
- EventBus.project and the test that allowed importing an event across
  zones are gone, the config rules speak of the schema foreign key;
- the worker waits one second less than the rest of the shutdown
  budget, so it cancels a stuck request with 500 and exits 0 instead
  of being killed by the gunicorn master. Lab 3 keeps 3 s after the
  drain window for the database timeouts, so SHUTDOWN_TIMEOUT_SECONDS
  becomes 6 s and the stop hooks wait 7 s, still under the 8 s grace;
- the lab 3 single statement post already locks the chat only for a
  member, so lab 2's LOCK_CHAT edit is not needed; its test is kept;
- lab 2 tests come along with Idempotency-Key where lab 3 needs it;
  the short TTL after restart runs without api-shortttl by shortening
  the session in the database; test_drain adds a stuck request;
- make validate uses .venv with openapi-spec-validator, PYTEST_ARGS,
  faults-evidence, down with the bench profile, the image is tagged by
  the compose project, start probes every 200 ms, cache evidence fails
  without a tombstone and checks the key expiry, kdf.txt shows the
  seed stub apart.

The track 2 tooling of lab 2 (track.py, postgres-restore and the
database argument of migrate) and its evidence scripts stay out.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
This commit is contained in:
Artyom
2026-10-30 02:32:49 +03:00
co-authored by Claude Opus 5.5
parent 79a52bab58
commit a59e3a39a8
37 changed files with 543 additions and 120 deletions
+44 -1
View File
@@ -1,4 +1,5 @@
import asyncio
import time
from collections.abc import AsyncIterator
from uuid import uuid4
@@ -18,6 +19,7 @@ from stand import (
new_user,
psql,
report,
restart_stack,
started_at,
timed,
valkey,
@@ -55,7 +57,8 @@ async def test_chat_creation_rolls_back_on_unknown_member(http: AsyncClient) ->
json={"title": title, "member_ids": [alice["id"], str(uuid4())]},
headers=bearer(token),
)
print(f"чат с несуществующим участником: {response.status_code}")
code = response.json()["error"]["code"]
print(f"чат с несуществующим участником: {response.status_code} {code}")
assert response.status_code == 400
assert response.json()["error"]["code"] == "invalid_request"
assert psql("SELECT count(*) FROM chats WHERE title = :'title'", title=title) == "0"
@@ -227,3 +230,43 @@ async def test_api_restart_keeps_session_without_extending_it(http: AsyncClient)
print(f"expires_at при входе {session['expires_at']}, в БД после рестарта {stored}")
assert me.status_code == 200
assert stored.rstrip("Z").rstrip("0") == session["expires_at"].rstrip("Z").rstrip("0")
async def test_full_restart_keeps_session_until_logout(http: AsyncClient) -> None:
user, token = await enroll(http, "fullrestart")
restart_stack()
recovered = await wait_ready(http)
me = await http.get("/api/v1/users/me", headers=bearer(token))
logout = await http.delete("/api/v1/auth/sessions/current", headers=bearer(token))
after = await http.get("/api/v1/users/me", headers=bearer(token))
print(
f"restart.sh: готовность через {recovered:.1f} с, прежний токен {me.status_code}, "
f"выход {logout.status_code}, затем {after.status_code}"
)
assert me.json() == user
assert logout.status_code == 204
assert after.status_code == 401
async def test_session_expires_after_restart_at_original_time(http: AsyncClient) -> None:
user, token = await enroll(http, "shortrestart")
expires_at = float(
psql(
"UPDATE sessions s SET expires_at = now() + interval '20 seconds' FROM users u "
"WHERE u.id = s.user_id AND u.username = :'name' "
"RETURNING extract(epoch FROM s.expires_at)",
name=user["username"],
).splitlines()[0]
)
compose("restart", "api")
recovered = await wait_ready(http)
fresh = await http.get("/api/v1/users/me", headers=bearer(token))
await asyncio.sleep(max(0.0, expires_at - time.time()) + 0.5)
stale = await http.get("/api/v1/users/me", headers=bearer(token))
print(
f"срок сессии сокращён до 20 с, рестарт api: готовность через {recovered:.1f} с, "
f"до срока {fresh.status_code}, после срока {stale.status_code}"
)
assert fresh.status_code == 200
assert stale.status_code == 401
assert stale.headers["WWW-Authenticate"] == BEARER