- carry the lab 3 solution over from mtusi2026lab3-safronov (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests, bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts, Dockerfile, compose, pyproject and uv lock - merge Makefile, .gitignore and .dockerignore (the lab 3 versions only add lines to the skeleton, which is the same in lab 4) and .env.example (lab 4 skeleton names listed in the header) - rename the compose project to mtusi-lab4 and the image to :lab4 so the stand does not reuse the lab 3 containers and volumes - update AGENTS.md for lab 4: current lab, origin commit, contract and test files of the new skeleton Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.5 KiB
Python
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}"
|
|
)
|