feat: add replicated messenger and reproducible lab3 stand
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""Реплики за балансировщиком (лаба 3, уровень «3»).
|
||||
|
||||
Одна сессия обслуживается разными репликами без повторного входа — это
|
||||
видно по `X-Instance-Id`; подтверждённая запись видна следующему чтению
|
||||
через любую реплику; у процесса одно значение заголовка. Нужны ≥ 2
|
||||
реплики за `BASE_URL` (фикстура `replicas`): против одного процесса тесты
|
||||
пропускаются с причиной. Опираются на round-robin LB без sticky session.
|
||||
|
||||
Отказ реплики (остановка, kill, зависание, поочерёдная замена) — не
|
||||
pytest: ему нужно действие над контейнерами (`make test-failover`,
|
||||
`make failover-each`, профили `failover` и `rollout` стенда `bench/`).
|
||||
"""
|
||||
|
||||
from collections import Counter
|
||||
|
||||
import httpx
|
||||
|
||||
from helpers import (
|
||||
ME,
|
||||
assert_replicas,
|
||||
create_chat,
|
||||
headers,
|
||||
instance_id,
|
||||
messages_path,
|
||||
public,
|
||||
register,
|
||||
send,
|
||||
)
|
||||
|
||||
SESSION_REQUESTS = 20
|
||||
|
||||
|
||||
def test_one_session_on_several_replicas(client: httpx.Client, replicas: int) -> None:
|
||||
alice = register(client, "alice")
|
||||
served: Counter[str] = Counter()
|
||||
for _ in range(SESSION_REQUESTS):
|
||||
response = client.get(ME, headers=headers(alice))
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == public(alice)
|
||||
served[instance_id(response)] += 1
|
||||
print(f"one session, {SESSION_REQUESTS} requests: {dict(served)}")
|
||||
assert_replicas(served, replicas)
|
||||
|
||||
|
||||
def test_write_visible_through_other_replica(
|
||||
client: httpx.Client,
|
||||
replicas: int,
|
||||
) -> None:
|
||||
alice, bob = register(client, "alice"), register(client, "bob")
|
||||
chat = create_chat(client, alice, bob)
|
||||
response = send(client, alice, chat, "через LB")
|
||||
assert response.status_code == 201, response.text
|
||||
message, writer = response.json(), instance_id(response)
|
||||
|
||||
readers: Counter[str] = Counter()
|
||||
for _ in range(2 * replicas + 2):
|
||||
page = client.get(messages_path(chat), headers=headers(bob))
|
||||
assert page.status_code == 200, page.text
|
||||
assert message in page.json()["items"]
|
||||
readers[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}"
|
||||
|
||||
|
||||
def test_instance_id_is_stable_per_process(client: httpx.Client, replicas: int) -> None:
|
||||
"""Две одинаковые серии запросов: вторая не приносит новых значений, и
|
||||
каждое значение повторяется — заголовок стабилен на время жизни
|
||||
процесса, а не новый на каждый ответ."""
|
||||
rounds = [
|
||||
Counter(instance_id(client.get("/health/live")) for _ in range(6 * replicas))
|
||||
for _ in range(2)
|
||||
]
|
||||
print(f"series: {[dict(counter) for counter in rounds]}")
|
||||
assert set(rounds[1]) <= set(rounds[0])
|
||||
assert all(count >= 2 for count in rounds[0].values())
|
||||
assert_replicas(rounds[0], replicas)
|
||||
Reference in New Issue
Block a user