feat: cache chat cards in Valkey
Valkey 9.1 in compose caches the immutable chat card (title, members,
created_at) used by GET /chats/{id} and by the membership check of the
message endpoints. Cache-aside with a write after commit, keys per
database, TTL plus allkeys-lru, persistence off. redis-py runs without
retries; on failure the cache is skipped for a few seconds and requests
go to PostgreSQL. Readiness does not depend on Valkey.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""Спек `chat-cache`: что видно по HTTP при кэше карточки чата.
|
||||
|
||||
Попадание в кэш, срок записи и отказ Valkey по HTTP не различить: это
|
||||
исключения в `spec_coverage.py` с evidence.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from api_client import API, Actor, Api
|
||||
from contract import assert_error, assert_ok
|
||||
|
||||
|
||||
@pytest.mark.spec("chat-cache", "Повторное чтение чата")
|
||||
def test_repeated_reads_match_creation(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
eve: Actor,
|
||||
) -> None:
|
||||
# порядок участников не совпадает ни с сортировкой, ни с порядком
|
||||
# регистрации: кэш обязан сохранить его как есть
|
||||
created = api.create_chat(bob, eve, alice, title="Кэш «карточки»")
|
||||
path = f"{API}/chats/{created['id']}"
|
||||
first = assert_ok(api.request("GET", path, actor=alice), 200, "Chat")
|
||||
second = assert_ok(api.request("GET", path, actor=eve), 200, "Chat")
|
||||
assert first == second == created
|
||||
assert first["member_ids"] == [bob.id, eve.id, alice.id]
|
||||
|
||||
|
||||
@pytest.mark.spec("chat-cache", "Проверка членства по карточке")
|
||||
def test_membership_from_new_chat(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
eve: Actor,
|
||||
) -> None:
|
||||
chat = api.create_chat(alice, bob)
|
||||
api.send_message(bob, chat["id"], "сразу после создания")
|
||||
path = f"{API}/chats/{chat['id']}"
|
||||
assert_error(api.request("GET", path, actor=eve), 404)
|
||||
assert_error(
|
||||
api.request(
|
||||
"POST",
|
||||
f"{path}/messages",
|
||||
actor=eve,
|
||||
json={"text": "посторонний"},
|
||||
),
|
||||
404,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.spec("chat-cache", "Сообщение видно сразу")
|
||||
def test_message_visible_at_once(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
# история уже прочитана: если бы она кэшировалась, второе чтение
|
||||
# отдало бы старую страницу
|
||||
assert api.all_messages(bob, chat["id"]) == []
|
||||
message = api.send_message(alice, chat["id"], "видно сразу")
|
||||
assert api.all_messages(bob, chat["id"]) == [message]
|
||||
|
||||
|
||||
@pytest.mark.spec("chat-cache", "Новый чат в списке")
|
||||
def test_new_chat_listed_at_once(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
bob: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
listed = [
|
||||
item["id"] for page in api.chat_pages(bob) for item in page["items"]
|
||||
]
|
||||
assert listed == [chat["id"]]
|
||||
second = api.create_chat(alice, bob, title="Второй")
|
||||
listed = [
|
||||
item["id"] for page in api.chat_pages(bob) for item in page["items"]
|
||||
]
|
||||
assert listed == [chat["id"], second["id"]]
|
||||
Reference in New Issue
Block a user