Files
mtusi2026lab7-safronov/student-tests/test_load_balancing.py
T
ricoromaandClaude Opus 5.5 089d78609d chore: port lab 6 solution into the lab 7 skeleton
Source: mtusi2026lab6-safronov, solution/bvt2301-safronov, 6c6a065.
Files follow AGENTS.md, "Переход в следующую лабу". Contracts, public
tests, README, REPORT and evidence of lab 6 are not carried over; infra
and scripts of the lab 7 skeleton stay as they are.
Makefile is the lab 6 one with the lab 7 skeleton part restored
(HTTPS BASE_URL on 8443, test-security); .gitignore and .dockerignore
are taken from lab 6 because both skeletons have the same files;
.env.example is the lab 6 one without changes.
The compose project, image and CI prefix are renamed to lab7, and
references to the previous lab now name lab6.

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

58 lines
2.5 KiB
Python

"""Спек `load-balancing`: запросы одной сессии и запись через реплики.
Capability `load-balancing` вне сверки покрытия (`spec_coverage.py`):
почти все её сценарии требуют действий над контейнерами и доказываются
evidence. Сценарии, которые проверяются по HTTP, названы в докстрингах.
Тесты нужны ≥ 2 реплики за `BASE_URL` (фикстура `replicas`).
"""
from collections import Counter
from typing import Any
from api_client import API, Actor, Api
from contract import assert_instance_id, assert_ok, assert_replicas
SESSION_REQUESTS = 20
def test_one_session_on_several_replicas(
api: Api,
alice: Actor,
replicas: int,
) -> None:
"""Сценарий «Одна сессия на разных репликах»: один вход, 20 запросов
с одним токеном — все 200 с этим пользователем, ≥ 2 реплики."""
served: Counter[str] = Counter()
for _ in range(SESSION_REQUESTS):
resp = api.request("GET", f"{API}/users/me", actor=alice)
assert assert_ok(resp, 200, "User") == alice.user
served[assert_instance_id(resp)] += 1
print(f"one session, {SESSION_REQUESTS} requests: {dict(served)}")
assert_replicas(served, replicas)
def test_write_visible_through_other_replica(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
replicas: int,
) -> None:
"""Сценарий «Запись видна через другую реплику»: сообщение,
отправленное через одну реплику, есть в каждом следующем чтении
истории, в том числе через другие реплики."""
resp = api.post_message(alice, chat["id"], json={"text": "через LB"})
message = assert_ok(resp, 201, "Message")
writer = assert_instance_id(resp)
readers: Counter[str] = Counter()
for _ in range(2 * replicas + 2):
page = api.messages_page(bob, chat["id"])
assert message in assert_ok(page, 200, "MessagePage")["items"]
readers[assert_instance_id(page)] += 1
print(f"written via {writer}, read via {dict(readers)}")
assert_replicas(readers, replicas)
assert readers.keys() - {writer}, (
f"every read was served by the writer {writer}"
)