Files
mtusi2026lab2/student-tests/unit/test_timeouts.py
T
ArtyomandClaude Opus 5.5 a59e3a39a8 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
2026-10-30 02:32:49 +03:00

62 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
from pathlib import Path
from typing import Any
import yaml
from messenger.shared.infrastructure.postgres import PING_TIMEOUT_SECONDS
ROOT = Path(__file__).resolve().parents[2]
DEFAULT = re.compile(r"\$\{\w+:?-([^}]*)\}")
UNITS = {"ms": 0.001, "s": 1.0, "": 1.0}
def seconds(value: object) -> float:
found = re.fullmatch(r"([\d.]+)(ms|s|)", str(value))
assert found is not None, value
return float(found[1]) * UNITS[found[2]]
def services() -> dict[str, Any]:
return yaml.safe_load(DEFAULT.sub(r"\1", (ROOT / "compose.yaml").read_text()))["services"]
def balancer() -> dict[str, float]:
text = (ROOT / "infra/haproxy/haproxy.cfg").read_text()
found = {
f"timeout {name}": seconds(value)
for name, value in re.findall(r"timeout ([\w-]+) (\S+)\n", text)
}
found["hard-stop-after"] = seconds(re.findall(r"hard-stop-after (\S+)", text)[0])
check = re.findall(r"default-server .*", text)[0]
for key in ("inter", "fastinter", "fall", "rise"):
found[key] = seconds(re.findall(rf"\b{key} (\S+)", check)[0])
return found
def test_timeouts_shrink_inwards_and_shutdown_fits_the_stop_budget() -> None:
lb, stand = balancer(), services()
api = {k: seconds(v) for k, v in stand["api"]["environment"].items() if k.endswith("SECONDS")}
hook = seconds(
re.findall(r"STOP_TIMEOUT:-(\d+)", (ROOT / "scripts/stop-one.sh").read_text())[0]
)
grace = seconds(stand["api"]["stop_grace_period"])
detection = lb["inter"] + (lb["fall"] - 1) * lb["fastinter"]
hung = detection + lb["fall"] * lb["timeout check"]
database = api["DB_ACQUIRE_TIMEOUT_SECONDS"] + api["DB_COMMAND_TIMEOUT_SECONDS"]
drain, shutdown = api["DRAIN_SECONDS"], api["SHUTDOWN_TIMEOUT_SECONDS"]
print(
f"обнаружение {detection} с (зависание {hung} с), окно {drain} с,"
f" дообработка {shutdown - drain - 1} с, остановка {shutdown} с, хук {hook} с,"
f" stop_grace_period {grace} с; LB server {lb['timeout server']} с >"
f" запрос {api['REQUEST_TIMEOUT_SECONDS']} с > БД {database} с;"
f" readiness {2 * PING_TIMEOUT_SECONDS} с < check {lb['timeout check']} с"
)
assert lb["timeout server"] > api["REQUEST_TIMEOUT_SECONDS"] > database
assert lb["timeout check"] > 2 * PING_TIMEOUT_SECONDS
assert drain > detection
assert shutdown - drain - 1 >= database
assert shutdown < hook < grace < 10
assert lb["hard-stop-after"] < seconds(stand["lb"]["stop_grace_period"])
assert (detection, hung) == (1.5, 3.5)