From a5f02bebce39f6f02b21a75342a2d0850cfa4d7f Mon Sep 17 00:00:00 2001 From: kaktuz20000 <149298757+kaktuz20000@users.noreply.github.com> Date: Thu, 15 Oct 2026 13:03:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B8=D0=B7=20=D0=BC=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 1 + README.md | 2 +- REPORT.md | 3 +- codes.md | 15 + contracts/README.md | 2 +- evidence/track5_query_restore.json | 500 +++++++++++------------ evidence/track5_query_restore.md | 12 +- src/handlers.py | 12 +- src/store.py | 93 +++-- student-tests/test_session_cache_race.py | 107 +++++ tests/README.md | 2 +- tests/smoke.py | 49 ++- 12 files changed, 505 insertions(+), 293 deletions(-) create mode 100644 student-tests/test_session_cache_race.py diff --git a/Makefile b/Makefile index a13be840..82a58ce8 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ test-persistence: grade4: $(PYTHON) student-tests/test_level4.py --base-url "$(BASE_URL)" + docker compose --env-file .env exec -T api python3 student-tests/test_session_cache_race.py ./scripts/test_expiry.sh track5: diff --git a/README.md b/README.md index 893baf1c..8321061e 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ make grade4 make track5 ``` -`make grade4` временно перезапускает API с двухсекундным TTL для проверки expiry, запускает интеграционные проверки Redis/PostgreSQL и возвращает штатный TTL. `make track5` создаёт отдельные случайно именованные временные БД, измеряет два варианта индекса на skewed-наборе по 100 000 сообщений на вариант, затем проверяет custom-format backup/restore и удаляет только созданные им БД. Численные результаты и планы сохраняются в `evidence/track5_query_restore.*`. +`make grade4` временно перезапускает API с двухсекундным TTL для проверки expiry, запускает интеграционные проверки Redis/PostgreSQL, включая конкурентное заполнение кэша сессии одновременно с logout, и возвращает штатный TTL. `make track5` создаёт отдельные случайно именованные временные БД, измеряет два варианта индекса на skewed-наборе по 100 000 сообщений на вариант, затем проверяет custom-format backup/restore и удаляет только созданные им БД. Численные результаты и планы сохраняются в `evidence/track5_query_restore.*`. Примеры запросов доступны в `examples/requests.http`, примеры JSON — в `contracts/examples.json`. Для полной проверки схемы (по желанию локально; CI делает её автоматически): diff --git a/REPORT.md b/REPORT.md index fa72ddd6..fce16234 100644 --- a/REPORT.md +++ b/REPORT.md @@ -45,7 +45,8 @@ docker compose --env-file .env down --volumes - Перенесены и адаптированы REST-поведение, Dockerfile и ignore-правила из lab1. Сервер разделён по обязанностям: `config.py` — настройки, `errors.py` — API-ошибки, `security.py` — password KDF/UUID/time helpers, `store.py` — PostgreSQL и Redis, `handlers.py` — HTTP endpoints, `app.py` — сборка/жизненный цикл сервера, `server.py` — совместимая точка входа. Публичные `tests/` и `contracts/` текущей лабораторной сохранены без подмены. - PostgreSQL — источник истины для пользователей, чатов, членства, сообщений и сессий. Именованный volume `postgres_data` сохраняет их при перезапуске. - Redis кэширует результат проверки opaque session token по SHA-256 digest ключа и хранит только публичный объект пользователя. Redis key имеет TTL, рассчитанный по оставшемуся `sessions.expires_at`; пароль, сырой токен и его текст не попадают в кэш. -- Создание и отзыв сессии записываются в PostgreSQL. Logout удаляет Redis-ключ до удаления записи в БД; при недоступном Redis logout завершается ограниченным `503`, а не сообщает об успехе без инвалидации кэша. При чтении недоступность Redis логируется, запрос безопасно переходит на PostgreSQL; после восстановления промах кэша заполняется из БД. При недоступной БД readiness и операции, требующие её, возвращают ограниченную ошибку. +- Создание и отзыв сессии записываются в PostgreSQL. Чтение сессии при промахе кэша и logout сериализуются транзакционной advisory lock на digest токена: конкурентное заполнение не может записать сессию в Redis уже после logout. Logout удаляет Redis-ключ до удаления записи в БД; при недоступном Redis logout завершается ограниченным `503`, а не сообщает об успехе без инвалидации кэша. При чтении недоступность Redis логируется, запрос безопасно переходит на PostgreSQL; после восстановления промах кэша заполняется из БД. При недоступной БД readiness и операции, требующие её, возвращают ограниченную ошибку. +- Список чатов использует keyset-пагинацию по `(created_at, id)`: курсор привязан к пользователю, страницы не повторяют и не пропускают чаты при обходе неизменённого списка. - Индекс истории в рабочей схеме — `(chat_id, created_at, id)`, соответствующий keyset-пагинации. Все SQL-запросы параметризованы; пул ограничен 16 соединениями, timeout ожидания пула/подключения — 5 секунд, `statement_timeout` — 5 секунд, `lock_timeout` — 2 секунды. - Миграция создаёт ключи и ограничения таблиц. Создание чата и всех участников — одна транзакция; уникальный индекс `username` сериализует гонку регистрации. - Пароли хранятся как scrypt-хеши с индивидуальной солью (`N=32768`, `r=8`, `p=1`). Сессии имеют expiry и отзываются logout-запросом. `X-User-Id` не используется для авторизации. diff --git a/codes.md b/codes.md index 034997ab..10437895 100644 --- a/codes.md +++ b/codes.md @@ -118,3 +118,18 @@ make track5 ```sh docker compose --env-file .env down ``` + + +docker compose --env-file .env up -d +curl --fail http://127.0.0.1:8080/health/ready +Затем публичные тесты из tests/ можно запустить напрямую: +python3 tests/smoke.py +Он проверяет основной сценарий API: регистрацию и вход, чаты, отправку и чтение сообщений, доступы и пагинацию. В конце должен вывести PASS. Тест создаёт записи в базе, они автоматически не удаляются. +python3 tests/resilience.py --mode persistence --action-script scripts/restart.sh +Он проверяет, что данные и существующая сессия доступны после перезапуска. Скрипт перезапуска должен быть исполняемым; если нужно: +python3 tests/resilience.py --mode persistence --action-script scripts/restart.sh +Он проверяет, что данные и существующая сессия доступны после перезапуска. Скрипт перезапуска должен быть исполняемым; если нужно: +chmod +x scripts/restart.sh +python3 tests/load.py --duration 30 --concurrency 4 +Это тест нагрузки: отправляет сообщения несколько секунд и показывает успешные запросы в секунду, ошибки и задержки. Он тоже создаёт тестовые сообщения. +tests/client.py — вспомогательный модуль для этих проверок, отдельно его запускать не нужно. То же самое можно запускать короче командами Make: make test, make test-persistence ACTION_SCRIPT=scripts/restart.sh. С описанием публичных проверок можно свериться в tests/README.md. \ No newline at end of file diff --git a/contracts/README.md b/contracts/README.md index 8c58b32f..8d527e20 100644 --- a/contracts/README.md +++ b/contracts/README.md @@ -14,7 +14,7 @@ ## Пользователи, чаты и сообщения - `POST /users` публичен; возвращает `201 User`. Пароль/его hash и сессионные данные никогда не входят в User. -- `POST /chats` принимает title и явный список существующих `member_ids`, включая вызывающего. Дубликаты, неизвестные участники и отсутствие вызывающего — 400. Чат создаётся атомарно. GET списка чатов сортируется по `(created_at, id)` по возрастанию. +- `POST /chats` принимает title и явный список существующих `member_ids`, включая вызывающего. Дубликаты, неизвестные участники и отсутствие вызывающего — 400. Чат создаётся атомарно. GET списка чатов использует keyset-пагинацию по `(created_at, id)` по возрастанию: `limit` — целое 1–100 (по умолчанию 50), `cursor` привязан к пользователю и последнему чату предыдущей страницы. Последний элемент не повторяется; `next_cursor` равен null, если следующей страницы нет. Непустой cursor с неверным форматом или другого пользователя — 400. - Отправитель сообщения выводится из текущей идентификации, поле `sender_id` в запросе не принимается. Текст — до 4000 Unicode code points. Нельзя отправить пустой/пробельный текст без вложений. - История использует keyset cursor: `(created_at, id)`, по возрастанию; `limit` — целое 1–100, по умолчанию 50. Ответ `{"items":[...],"next_cursor":null}`. Непустой cursor — непрозрачная для клиента строка; последний элемент предыдущей страницы не повторяется. Cursor привязан к ресурсу/запросу: от другого чата — 400. Изменять `limit` при продолжении можно. - `next_cursor` равен null, если на момент чтения следующей записи нет. Гарантируется отсутствие повторов при обходе неизменной истории; snapshot изоляция между несколькими HTTP-запросами не требуется. При конкурентных вставках опишите выбранную семантику и ограничения часов. diff --git a/evidence/track5_query_restore.json b/evidence/track5_query_restore.json index e6f8957a..f79f9d51 100644 --- a/evidence/track5_query_restore.json +++ b/evidence/track5_query_restore.json @@ -1,5 +1,5 @@ { - "run_utc": "2026-10-07T06:01:48.126545+00:00", + "run_utc": "2026-10-09T13:21:05.722546+00:00", "environment": { "host_platform": "macOS-27.0-arm64-arm-64bit-Mach-O", "host_architecture": "arm64", @@ -24,8 +24,8 @@ "index": "(chat_id)", "samples": [ { - "execution_ms": 0.308, - "planning_ms": 0.492, + "execution_ms": 0.265, + "planning_ms": 0.384, "actual_rows": 25, "shared_hit_blocks": 206, "shared_read_blocks": 2, @@ -33,12 +33,12 @@ "Node Type": "Limit", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.42, + "Startup Cost": 26.61, + "Total Cost": 26.67, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.233, - "Actual Total Time": 0.236, + "Actual Startup Time": 0.217, + "Actual Total Time": 0.22, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 206, @@ -57,12 +57,12 @@ "Parent Relationship": "Outer", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.43, - "Plan Rows": 29, + "Startup Cost": 26.61, + "Total Cost": 26.68, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.232, - "Actual Total Time": 0.234, + "Actual Startup Time": 0.216, + "Actual Total Time": 0.217, "Actual Rows": 25, "Actual Loops": 1, "Sort Key": [ @@ -93,11 +93,11 @@ "Relation Name": "messages_chat_only", "Alias": "messages_chat_only", "Startup Cost": 0.29, - "Total Cost": 25.65, - "Plan Rows": 29, + "Total Cost": 25.87, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.07, - "Actual Total Time": 0.194, + "Actual Startup Time": 0.073, + "Actual Total Time": 0.182, "Actual Rows": 150, "Actual Loops": 1, "Index Cond": "(chat_id = 2)", @@ -121,8 +121,8 @@ } }, { - "execution_ms": 0.29, - "planning_ms": 0.41, + "execution_ms": 0.233, + "planning_ms": 0.353, "actual_rows": 25, "shared_hit_blocks": 208, "shared_read_blocks": 0, @@ -130,12 +130,12 @@ "Node Type": "Limit", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.42, + "Startup Cost": 26.61, + "Total Cost": 26.67, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.236, - "Actual Total Time": 0.239, + "Actual Startup Time": 0.195, + "Actual Total Time": 0.198, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 208, @@ -154,12 +154,12 @@ "Parent Relationship": "Outer", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.43, - "Plan Rows": 29, + "Startup Cost": 26.61, + "Total Cost": 26.68, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.235, - "Actual Total Time": 0.236, + "Actual Startup Time": 0.194, + "Actual Total Time": 0.195, "Actual Rows": 25, "Actual Loops": 1, "Sort Key": [ @@ -190,11 +190,11 @@ "Relation Name": "messages_chat_only", "Alias": "messages_chat_only", "Startup Cost": 0.29, - "Total Cost": 25.65, - "Plan Rows": 29, + "Total Cost": 25.87, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.059, - "Actual Total Time": 0.187, + "Actual Startup Time": 0.055, + "Actual Total Time": 0.166, "Actual Rows": 150, "Actual Loops": 1, "Index Cond": "(chat_id = 2)", @@ -218,8 +218,8 @@ } }, { - "execution_ms": 0.297, - "planning_ms": 0.407, + "execution_ms": 0.245, + "planning_ms": 0.363, "actual_rows": 25, "shared_hit_blocks": 208, "shared_read_blocks": 0, @@ -227,12 +227,12 @@ "Node Type": "Limit", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.42, + "Startup Cost": 26.61, + "Total Cost": 26.67, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.248, - "Actual Total Time": 0.252, + "Actual Startup Time": 0.198, + "Actual Total Time": 0.2, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 208, @@ -251,12 +251,12 @@ "Parent Relationship": "Outer", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.43, - "Plan Rows": 29, + "Startup Cost": 26.61, + "Total Cost": 26.68, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.247, - "Actual Total Time": 0.249, + "Actual Startup Time": 0.197, + "Actual Total Time": 0.198, "Actual Rows": 25, "Actual Loops": 1, "Sort Key": [ @@ -287,11 +287,11 @@ "Relation Name": "messages_chat_only", "Alias": "messages_chat_only", "Startup Cost": 0.29, - "Total Cost": 25.65, - "Plan Rows": 29, + "Total Cost": 25.87, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.063, - "Actual Total Time": 0.208, + "Actual Startup Time": 0.051, + "Actual Total Time": 0.158, "Actual Rows": 150, "Actual Loops": 1, "Index Cond": "(chat_id = 2)", @@ -315,8 +315,8 @@ } }, { - "execution_ms": 0.334, - "planning_ms": 0.414, + "execution_ms": 0.218, + "planning_ms": 0.285, "actual_rows": 25, "shared_hit_blocks": 208, "shared_read_blocks": 0, @@ -324,12 +324,12 @@ "Node Type": "Limit", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.42, + "Startup Cost": 26.61, + "Total Cost": 26.67, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.259, - "Actual Total Time": 0.262, + "Actual Startup Time": 0.184, + "Actual Total Time": 0.186, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 208, @@ -348,12 +348,12 @@ "Parent Relationship": "Outer", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.43, - "Plan Rows": 29, + "Startup Cost": 26.61, + "Total Cost": 26.68, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.258, - "Actual Total Time": 0.259, + "Actual Startup Time": 0.183, + "Actual Total Time": 0.184, "Actual Rows": 25, "Actual Loops": 1, "Sort Key": [ @@ -384,11 +384,11 @@ "Relation Name": "messages_chat_only", "Alias": "messages_chat_only", "Startup Cost": 0.29, - "Total Cost": 25.65, - "Plan Rows": 29, + "Total Cost": 25.87, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.061, - "Actual Total Time": 0.191, + "Actual Startup Time": 0.048, + "Actual Total Time": 0.155, "Actual Rows": 150, "Actual Loops": 1, "Index Cond": "(chat_id = 2)", @@ -412,8 +412,8 @@ } }, { - "execution_ms": 0.271, - "planning_ms": 0.454, + "execution_ms": 0.268, + "planning_ms": 0.365, "actual_rows": 25, "shared_hit_blocks": 208, "shared_read_blocks": 0, @@ -421,12 +421,12 @@ "Node Type": "Limit", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.42, + "Startup Cost": 26.61, + "Total Cost": 26.67, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.223, - "Actual Total Time": 0.226, + "Actual Startup Time": 0.228, + "Actual Total Time": 0.23, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 208, @@ -445,12 +445,12 @@ "Parent Relationship": "Outer", "Parallel Aware": false, "Async Capable": false, - "Startup Cost": 26.36, - "Total Cost": 26.43, - "Plan Rows": 29, + "Startup Cost": 26.61, + "Total Cost": 26.68, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.222, - "Actual Total Time": 0.223, + "Actual Startup Time": 0.227, + "Actual Total Time": 0.228, "Actual Rows": 25, "Actual Loops": 1, "Sort Key": [ @@ -481,11 +481,11 @@ "Relation Name": "messages_chat_only", "Alias": "messages_chat_only", "Startup Cost": 0.29, - "Total Cost": 25.65, - "Plan Rows": 29, + "Total Cost": 25.87, + "Plan Rows": 30, "Plan Width": 41, - "Actual Startup Time": 0.059, - "Actual Total Time": 0.183, + "Actual Startup Time": 0.049, + "Actual Total Time": 0.186, "Actual Rows": 150, "Actual Loops": 1, "Index Cond": "(chat_id = 2)", @@ -514,8 +514,8 @@ "index": "(chat_id, created_at, id)", "samples": [ { - "execution_ms": 0.201, - "planning_ms": 0.446, + "execution_ms": 0.085, + "planning_ms": 0.331, "actual_rows": 25, "shared_hit_blocks": 25, "shared_read_blocks": 3, @@ -524,11 +524,11 @@ "Parallel Aware": false, "Async Capable": false, "Startup Cost": 0.42, - "Total Cost": 50.73, + "Total Cost": 50.6, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.045, - "Actual Total Time": 0.156, + "Actual Startup Time": 0.034, + "Actual Total Time": 0.055, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 25, @@ -552,11 +552,11 @@ "Relation Name": "messages_composite", "Alias": "messages_composite", "Startup Cost": 0.42, - "Total Cost": 60.79, - "Plan Rows": 30, + "Total Cost": 62.64, + "Plan Rows": 31, "Plan Width": 41, - "Actual Startup Time": 0.044, - "Actual Total Time": 0.151, + "Actual Startup Time": 0.033, + "Actual Total Time": 0.053, "Actual Rows": 25, "Actual Loops": 1, "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", @@ -576,8 +576,8 @@ } }, { - "execution_ms": 0.1, - "planning_ms": 0.429, + "execution_ms": 0.087, + "planning_ms": 0.362, "actual_rows": 25, "shared_hit_blocks": 28, "shared_read_blocks": 0, @@ -586,11 +586,11 @@ "Parallel Aware": false, "Async Capable": false, "Startup Cost": 0.42, - "Total Cost": 50.73, + "Total Cost": 50.6, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.024, - "Actual Total Time": 0.048, + "Actual Startup Time": 0.022, + "Actual Total Time": 0.043, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 28, @@ -614,134 +614,10 @@ "Relation Name": "messages_composite", "Alias": "messages_composite", "Startup Cost": 0.42, - "Total Cost": 60.79, - "Plan Rows": 30, + "Total Cost": 62.64, + "Plan Rows": 31, "Plan Width": 41, - "Actual Startup Time": 0.023, - "Actual Total Time": 0.045, - "Actual Rows": 25, - "Actual Loops": 1, - "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", - "Rows Removed by Index Recheck": 0, - "Shared Hit Blocks": 28, - "Shared Read Blocks": 0, - "Shared Dirtied Blocks": 0, - "Shared Written Blocks": 0, - "Local Hit Blocks": 0, - "Local Read Blocks": 0, - "Local Dirtied Blocks": 0, - "Local Written Blocks": 0, - "Temp Read Blocks": 0, - "Temp Written Blocks": 0 - } - ] - } - }, - { - "execution_ms": 0.093, - "planning_ms": 0.433, - "actual_rows": 25, - "shared_hit_blocks": 28, - "shared_read_blocks": 0, - "plan": { - "Node Type": "Limit", - "Parallel Aware": false, - "Async Capable": false, - "Startup Cost": 0.42, - "Total Cost": 50.73, - "Plan Rows": 25, - "Plan Width": 41, - "Actual Startup Time": 0.02, - "Actual Total Time": 0.046, - "Actual Rows": 25, - "Actual Loops": 1, - "Shared Hit Blocks": 28, - "Shared Read Blocks": 0, - "Shared Dirtied Blocks": 0, - "Shared Written Blocks": 0, - "Local Hit Blocks": 0, - "Local Read Blocks": 0, - "Local Dirtied Blocks": 0, - "Local Written Blocks": 0, - "Temp Read Blocks": 0, - "Temp Written Blocks": 0, - "Plans": [ - { - "Node Type": "Index Scan", - "Parent Relationship": "Outer", - "Parallel Aware": false, - "Async Capable": false, - "Scan Direction": "Forward", - "Index Name": "messages_chat_history_idx", - "Relation Name": "messages_composite", - "Alias": "messages_composite", - "Startup Cost": 0.42, - "Total Cost": 60.79, - "Plan Rows": 30, - "Plan Width": 41, - "Actual Startup Time": 0.019, - "Actual Total Time": 0.043, - "Actual Rows": 25, - "Actual Loops": 1, - "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", - "Rows Removed by Index Recheck": 0, - "Shared Hit Blocks": 28, - "Shared Read Blocks": 0, - "Shared Dirtied Blocks": 0, - "Shared Written Blocks": 0, - "Local Hit Blocks": 0, - "Local Read Blocks": 0, - "Local Dirtied Blocks": 0, - "Local Written Blocks": 0, - "Temp Read Blocks": 0, - "Temp Written Blocks": 0 - } - ] - } - }, - { - "execution_ms": 0.097, - "planning_ms": 0.444, - "actual_rows": 25, - "shared_hit_blocks": 28, - "shared_read_blocks": 0, - "plan": { - "Node Type": "Limit", - "Parallel Aware": false, - "Async Capable": false, - "Startup Cost": 0.42, - "Total Cost": 50.73, - "Plan Rows": 25, - "Plan Width": 41, - "Actual Startup Time": 0.02, - "Actual Total Time": 0.044, - "Actual Rows": 25, - "Actual Loops": 1, - "Shared Hit Blocks": 28, - "Shared Read Blocks": 0, - "Shared Dirtied Blocks": 0, - "Shared Written Blocks": 0, - "Local Hit Blocks": 0, - "Local Read Blocks": 0, - "Local Dirtied Blocks": 0, - "Local Written Blocks": 0, - "Temp Read Blocks": 0, - "Temp Written Blocks": 0, - "Plans": [ - { - "Node Type": "Index Scan", - "Parent Relationship": "Outer", - "Parallel Aware": false, - "Async Capable": false, - "Scan Direction": "Forward", - "Index Name": "messages_chat_history_idx", - "Relation Name": "messages_composite", - "Alias": "messages_composite", - "Startup Cost": 0.42, - "Total Cost": 60.79, - "Plan Rows": 30, - "Plan Width": 41, - "Actual Startup Time": 0.019, + "Actual Startup Time": 0.021, "Actual Total Time": 0.041, "Actual Rows": 25, "Actual Loops": 1, @@ -762,8 +638,8 @@ } }, { - "execution_ms": 0.083, - "planning_ms": 0.473, + "execution_ms": 0.065, + "planning_ms": 0.307, "actual_rows": 25, "shared_hit_blocks": 28, "shared_read_blocks": 0, @@ -772,11 +648,11 @@ "Parallel Aware": false, "Async Capable": false, "Startup Cost": 0.42, - "Total Cost": 50.73, + "Total Cost": 50.6, "Plan Rows": 25, "Plan Width": 41, - "Actual Startup Time": 0.02, - "Actual Total Time": 0.044, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.035, "Actual Rows": 25, "Actual Loops": 1, "Shared Hit Blocks": 28, @@ -800,11 +676,135 @@ "Relation Name": "messages_composite", "Alias": "messages_composite", "Startup Cost": 0.42, - "Total Cost": 60.79, - "Plan Rows": 30, + "Total Cost": 62.64, + "Plan Rows": 31, "Plan Width": 41, - "Actual Startup Time": 0.019, - "Actual Total Time": 0.041, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.032, + "Actual Rows": 25, + "Actual Loops": 1, + "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + }, + { + "execution_ms": 0.079, + "planning_ms": 0.365, + "actual_rows": 25, + "shared_hit_blocks": 28, + "shared_read_blocks": 0, + "plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 50.6, + "Plan Rows": 25, + "Plan Width": 41, + "Actual Startup Time": 0.021, + "Actual Total Time": 0.042, + "Actual Rows": 25, + "Actual Loops": 1, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_history_idx", + "Relation Name": "messages_composite", + "Alias": "messages_composite", + "Startup Cost": 0.42, + "Total Cost": 62.64, + "Plan Rows": 31, + "Plan Width": 41, + "Actual Startup Time": 0.02, + "Actual Total Time": 0.039, + "Actual Rows": 25, + "Actual Loops": 1, + "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", + "Rows Removed by Index Recheck": 0, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0 + } + ] + } + }, + { + "execution_ms": 0.066, + "planning_ms": 0.299, + "actual_rows": 25, + "shared_hit_blocks": 28, + "shared_read_blocks": 0, + "plan": { + "Node Type": "Limit", + "Parallel Aware": false, + "Async Capable": false, + "Startup Cost": 0.42, + "Total Cost": 50.6, + "Plan Rows": 25, + "Plan Width": 41, + "Actual Startup Time": 0.015, + "Actual Total Time": 0.036, + "Actual Rows": 25, + "Actual Loops": 1, + "Shared Hit Blocks": 28, + "Shared Read Blocks": 0, + "Shared Dirtied Blocks": 0, + "Shared Written Blocks": 0, + "Local Hit Blocks": 0, + "Local Read Blocks": 0, + "Local Dirtied Blocks": 0, + "Local Written Blocks": 0, + "Temp Read Blocks": 0, + "Temp Written Blocks": 0, + "Plans": [ + { + "Node Type": "Index Scan", + "Parent Relationship": "Outer", + "Parallel Aware": false, + "Async Capable": false, + "Scan Direction": "Forward", + "Index Name": "messages_chat_history_idx", + "Relation Name": "messages_composite", + "Alias": "messages_composite", + "Startup Cost": 0.42, + "Total Cost": 62.64, + "Plan Rows": 31, + "Plan Width": 41, + "Actual Startup Time": 0.014, + "Actual Total Time": 0.033, "Actual Rows": 25, "Actual Loops": 1, "Index Cond": "((chat_id = 2) AND (ROW(created_at, id) > ROW('2025-01-01 23:36:40+00'::timestamp with time zone, 0)))", @@ -830,32 +830,32 @@ "chat_id_only": { "samples": [ { - "execution_ms": 5.747, - "planning_ms": 0.092, + "execution_ms": 4.163, + "planning_ms": 0.062, "shared_write_blocks": 25 }, { - "execution_ms": 5.886, - "planning_ms": 0.068, + "execution_ms": 3.936, + "planning_ms": 0.05, "shared_write_blocks": 40 }, { - "execution_ms": 5.27, - "planning_ms": 0.064, + "execution_ms": 3.97, + "planning_ms": 0.051, "shared_write_blocks": 25 }, { - "execution_ms": 5.177, - "planning_ms": 0.068, + "execution_ms": 3.722, + "planning_ms": 0.05, "shared_write_blocks": 24 }, { - "execution_ms": 5.428, - "planning_ms": 0.076, + "execution_ms": 4.037, + "planning_ms": 0.059, "shared_write_blocks": 25 } ], - "median_execution_ms": 5.428, + "median_execution_ms": 3.97, "median_shared_write_blocks": 25, "rows_per_sample": 2000, "rolled_back_after_each_measurement": true @@ -863,32 +863,32 @@ "composite_history": { "samples": [ { - "execution_ms": 7.341, - "planning_ms": 0.068, + "execution_ms": 5.166, + "planning_ms": 0.062, "shared_write_blocks": 26 }, { - "execution_ms": 8.717, - "planning_ms": 0.066, + "execution_ms": 7.163, + "planning_ms": 0.051, "shared_write_blocks": 120 }, { - "execution_ms": 6.492, - "planning_ms": 0.067, + "execution_ms": 4.777, + "planning_ms": 0.05, "shared_write_blocks": 23 }, { - "execution_ms": 6.163, - "planning_ms": 0.064, + "execution_ms": 4.501, + "planning_ms": 0.049, "shared_write_blocks": 24 }, { - "execution_ms": 7.218, - "planning_ms": 0.066, + "execution_ms": 5.049, + "planning_ms": 0.05, "shared_write_blocks": 25 } ], - "median_execution_ms": 7.218, + "median_execution_ms": 5.049, "median_shared_write_blocks": 25, "rows_per_sample": 2000, "rolled_back_after_each_measurement": true @@ -897,8 +897,8 @@ "backup_restore": { "format": "pg_dump custom format", "dump_bytes": 1596489, - "dump_seconds": 0.3118779160431586, - "restore_seconds": 0.38548716699006036, + "dump_seconds": 0.25986058299895376, + "restore_seconds": 0.31837804202223197, "restored_counts": { "messages_chat_only": 100000, "messages_composite": 100000 diff --git a/evidence/track5_query_restore.md b/evidence/track5_query_restore.md index c18ff3a2..36df7f0a 100644 --- a/evidence/track5_query_restore.md +++ b/evidence/track5_query_restore.md @@ -1,6 +1,6 @@ # Lab2 level-5 track 2 — query plans and recovery -- Run UTC: 2026-10-07T06:01:48.126545+00:00 +- Run UTC: 2026-10-09T13:21:05.722546+00:00 - Host: macOS-27.0-arm64-arm-64bit-Mach-O (arm64), logical CPUs: 10 - Host physical memory: 17179869184 bytes. - PostgreSQL: 16.4 in `postgres:16.4-alpine`; container memory limit: 0 bytes; CPU limit: 0 nanocores (0 means unlimited). @@ -9,16 +9,16 @@ | Index | Median execution (ms) | Shared hits | Shared reads | Rows | |---|---:|---:|---:|---:| -| `(chat_id)` | 0.297 | 206 | 2 | 25 | -| `(chat_id, created_at, id)` | 0.097 | 25 | 3 | 25 | +| `(chat_id)` | 0.245 | 206 | 2 | 25 | +| `(chat_id, created_at, id)` | 0.079 | 25 | 3 | 25 | | Index | Median time for 2,000 inserted rows (ms) | Median shared write blocks | |---|---:|---:| -| `(chat_id)` | 5.428 | 25 | -| `(chat_id, created_at, id)` | 7.218 | 25 | +| `(chat_id)` | 3.970 | 25 | +| `(chat_id, created_at, id)` | 5.049 | 25 | Each write-cost query was measured five times inside a transaction and rolled back. This isolates execution and index-maintenance work but does not measure durable commit/fsync latency. -Backup/restore: custom-format dump 1596489 bytes; dump 0.312s; restore into a separately created empty database 0.385s. Restored row counts and deterministic checksums for the selected messages were verified: **PASS**. +Backup/restore: custom-format dump 1596489 bytes; dump 0.260s; restore into a separately created empty database 0.318s. Restored row counts and deterministic checksums for the selected messages were verified: **PASS**. Raw JSON plans, samples, environment and restore checksums are in `track5_query_restore.json`. This single-host synthetic run is a reproducible comparison, not a production capacity or HA claim. diff --git a/src/handlers.py b/src/handlers.py index 42ed25db..ac96b003 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -235,13 +235,17 @@ class MessengerHandler(BaseHTTPRequestHandler): def _handle_list_chats(self, query: Dict[str, List[str]], request_id: str): actor = self._get_actor(request_id) try: - limit = int(query.get("limit", ["100"])[0]) + limit = int(query.get("limit", ["50"])[0]) except ValueError: raise ApiError(400, "invalid_request", "Invalid limit") - if limit < 1: + if not 1 <= limit <= 100: raise ApiError(400, "invalid_request", "Invalid limit") - chats = self.store.list_chats(actor["id"], min(limit, 100)) - self._send_json(200, {"items": chats, "next_cursor": None}, request_id) + cursor = query.get("cursor", [None])[0] + try: + chats, next_cursor = self.store.list_chats(actor["id"], limit, cursor) + except ValueError: + raise ApiError(400, "invalid_request", "Invalid cursor") + self._send_json(200, {"items": chats, "next_cursor": next_cursor}, request_id) def _handle_get_chat(self, chat_id: str, request_id: str): actor = self._get_actor(request_id) diff --git a/src/store.py b/src/store.py index a6debf66..90f17209 100644 --- a/src/store.py +++ b/src/store.py @@ -167,6 +167,10 @@ class Store: ) return token, expires_at + @staticmethod + def _session_lock_key(token_hash: bytes) -> int: + return int.from_bytes(token_hash[:8], byteorder="big", signed=True) + def session_user(self, token: str) -> Optional[Dict[str, Any]]: token_hash = hashlib.sha256(token.encode("utf-8")).digest() cache_key = "messenger:session:" + token_hash.hex() @@ -178,33 +182,37 @@ class Store: except (redis.RedisError, ValueError, TypeError): LOGGER.warning("session cache read failed; falling back to PostgreSQL") with self.pool.connection() as conn: - row = conn.execute( - "SELECT u.id, u.username, u.display_name, u.created_at, s.expires_at " - "FROM sessions s JOIN users u ON u.id = s.user_id " - "WHERE s.token_hash = %s AND s.expires_at > now()", - (token_hash,), - ).fetchone() - if not row: - return None - user = self.user(row) - if self.cache is not None: - ttl_ms = math.floor((row["expires_at"] - utc_now()).total_seconds() * 1000) - if ttl_ms > 0: - try: - self.cache.set(cache_key, json.dumps(user, separators=(",", ":")), px=ttl_ms) - except redis.RedisError: - LOGGER.warning("session cache write failed; PostgreSQL remains authoritative") + with conn.transaction(): + conn.execute("SELECT pg_advisory_xact_lock(%s)", (self._session_lock_key(token_hash),)) + row = conn.execute( + "SELECT u.id, u.username, u.display_name, u.created_at, s.expires_at " + "FROM sessions s JOIN users u ON u.id = s.user_id " + "WHERE s.token_hash = %s AND s.expires_at > now()", + (token_hash,), + ).fetchone() + if not row: + return None + user = self.user(row) + if self.cache is not None: + ttl_ms = math.floor((row["expires_at"] - utc_now()).total_seconds() * 1000) + if ttl_ms > 0: + try: + self.cache.set(cache_key, json.dumps(user, separators=(",", ":")), px=ttl_ms) + except redis.RedisError: + LOGGER.warning("session cache write failed; PostgreSQL remains authoritative") return user def revoke_session(self, token: str) -> bool: token_hash = hashlib.sha256(token.encode("utf-8")).digest() - if self.cache is not None: - try: - self.cache.delete("messenger:session:" + token_hash.hex()) - except redis.RedisError as exc: - raise CacheUnavailableError from exc with self.pool.connection() as conn: - result = conn.execute("DELETE FROM sessions WHERE token_hash = %s", (token_hash,)) + with conn.transaction(): + conn.execute("SELECT pg_advisory_xact_lock(%s)", (self._session_lock_key(token_hash),)) + if self.cache is not None: + try: + self.cache.delete("messenger:session:" + token_hash.hex()) + except redis.RedisError as exc: + raise CacheUnavailableError from exc + result = conn.execute("DELETE FROM sessions WHERE token_hash = %s", (token_hash,)) return result.rowcount == 1 def create_chat(self, title: str, member_ids: List[str], actor_id: str) -> Dict[str, Any]: @@ -239,22 +247,51 @@ class Store: ).fetchall() return self.chat(row, [str(item["user_id"]) for item in members]) - def list_chats(self, user_id: str, limit: int) -> List[Dict[str, Any]]: + def list_chats( + self, user_id: str, limit: int, cursor: Optional[str] + ) -> Tuple[List[Dict[str, Any]], Optional[str]]: + where = "own.user_id = %s" + params: List[Any] = [user_id] + if cursor is not None: + try: + raw = base64.b64decode( + cursor.encode("ascii") + b"=" * (-len(cursor) % 4), + altchars=b"-_", + validate=True, + ) + cursor_user, timestamp, chat_id = raw.decode("utf-8").split("|", 2) + if cursor_user != user_id or not valid_uuid(chat_id): + raise ValueError("cursor scope") + created_at = datetime.fromisoformat(timestamp.replace("Z", "+00:00")) + if created_at.tzinfo is None or created_at.utcoffset() is None: + raise ValueError("cursor timestamp must include a timezone") + where += " AND (c.created_at, c.id) > (%s, %s)" + params.extend((created_at, chat_id)) + except (ValueError, UnicodeError, TypeError) as exc: + raise ValueError("invalid cursor") from exc + params.append(limit + 1) with self.pool.connection() as conn: rows = conn.execute( "SELECT c.id, c.title, c.created_at FROM chats c " - "JOIN chat_members own ON own.chat_id = c.id AND own.user_id = %s " - "ORDER BY c.created_at, c.id LIMIT %s", - (user_id, limit), + "JOIN chat_members own ON own.chat_id = c.id " + f"WHERE {where} ORDER BY c.created_at, c.id LIMIT %s", + params, ).fetchall() + has_more = len(rows) > limit + page = rows[:limit] result = [] - for row in rows: + for row in page: members = conn.execute( "SELECT user_id FROM chat_members WHERE chat_id = %s ORDER BY joined_at, user_id", (row["id"],), ).fetchall() result.append(self.chat(row, [str(item["user_id"]) for item in members])) - return result + next_cursor = None + if has_more and page: + last = page[-1] + cursor_data = f"{user_id}|{isoformat(last['created_at'])}|{last['id']}".encode("utf-8") + next_cursor = base64.urlsafe_b64encode(cursor_data).decode("ascii").rstrip("=") + return result, next_cursor def create_message(self, chat_id: str, sender_id: str, text: str) -> Optional[Dict[str, Any]]: with self.pool.connection() as conn: diff --git a/student-tests/test_session_cache_race.py b/student-tests/test_session_cache_race.py new file mode 100644 index 00000000..594c9e3f --- /dev/null +++ b/student-tests/test_session_cache_race.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +"""Deterministically verify that a cache miss cannot repopulate a logged-out session.""" +import hashlib +import secrets +import threading + +from src.store import Store + + +class BlockingCache: + def __init__(self, cache): + self.cache = cache + self.set_started = threading.Event() + self.allow_set = threading.Event() + + def __getattr__(self, name): + return getattr(self.cache, name) + + def set(self, key, value, **kwargs): + self.set_started.set() + if not self.allow_set.wait(timeout=10): + raise RuntimeError("timed out waiting to release the paused cache write") + return self.cache.set(key, value, **kwargs) + + +def main(): + store = Store() + store.open() + underlying_cache = store.cache + if underlying_cache is None: + store.close() + raise RuntimeError("Redis cache is required for this test") + username = "sessionrace_" + secrets.token_hex(8) + user = store.create_user(username, "Session race test", "unused-test-hash") + token, _ = store.create_session(user["id"]) + token_hash = hashlib.sha256(token.encode("ascii")).digest() + cache_key = "messenger:session:" + token_hash.hex() + + blocking_cache = BlockingCache(underlying_cache) + store.cache = blocking_cache + outcomes = {} + failures = [] + revoker_done = threading.Event() + + def read_session(): + try: + outcomes["reader"] = store.session_user(token) + except Exception as exc: + failures.append(exc) + + def revoke_session(): + try: + outcomes["revoker"] = store.revoke_session(token) + except Exception as exc: + failures.append(exc) + finally: + revoker_done.set() + + reader = threading.Thread(target=read_session) + revoker = None + try: + underlying_cache.delete(cache_key) + reader.start() + if not blocking_cache.set_started.wait(timeout=10): + raise RuntimeError("session read did not reach the cache write") + + revoker_started = threading.Event() + + def start_revocation(): + revoker_started.set() + revoke_session() + + revoker = threading.Thread(target=start_revocation) + revoker.start() + if not revoker_started.wait(timeout=5): + raise RuntimeError("logout thread did not start") + if revoker_done.wait(timeout=0.2): + raise RuntimeError("logout passed the read's session lock before cache write completed") + + blocking_cache.allow_set.set() + reader.join(timeout=10) + revoker.join(timeout=10) + if reader.is_alive() or revoker.is_alive(): + raise RuntimeError("session race test threads did not finish") + if failures: + raise RuntimeError("session race worker failed") from failures[0] + if outcomes.get("reader") != user or outcomes.get("revoker") is not True: + raise RuntimeError("session read/logout returned unexpected results") + if underlying_cache.get(cache_key) is not None: + raise RuntimeError("logged-out session remains in Redis") + if store.session_user(token) is not None: + raise RuntimeError("logged-out session is still accepted") + print("PASS: concurrent session cache fill and logout leave no cached or valid session") + finally: + blocking_cache.allow_set.set() + reader.join(timeout=10) + if revoker is not None: + revoker.join(timeout=10) + store.cache = underlying_cache + underlying_cache.delete(cache_key) + with store.pool.connection() as conn: + conn.execute("DELETE FROM users WHERE id = %s", (user["id"],)) + store.close() + + +if __name__ == "__main__": + main() diff --git a/tests/README.md b/tests/README.md index 0dfa3f89..4e86d6fe 100644 --- a/tests/README.md +++ b/tests/README.md @@ -9,7 +9,7 @@ make test BASE_URL=http://localhost:8080 python3 tests/smoke.py --base-url http://localhost:8080 ``` -`smoke.py` выполняет **один сквозной сценарий**, автоматически выбирая профиль из `lab.json`: health → три пользователя → вход с lab2 → чат → сообщения → пагинация → ошибки доступа → идемпотентность с lab3 → файл с lab4 → thumbnail с lab5 → logout. Он проверяет JSON по используемому подмножеству схемы, коды/headers, Unicode, SHA-256 оригинала и фактические JPEG-размеры thumbnail. Это публичный минимум, не полный fuzz/security/load suite. +`smoke.py` выполняет **один сквозной сценарий**, автоматически выбирая профиль из `lab.json`: health → три пользователя → вход с lab2 → несколько чатов и их keyset-пагинация → сообщения и пагинация истории → ошибки доступа → идемпотентность с lab3 → файл с lab4 → thumbnail с lab5 → logout. Он проверяет JSON по используемому подмножеству схемы, коды/headers, Unicode, отсутствие повторов/пропусков при пагинации, SHA-256 оригинала и фактические JPEG-размеры thumbnail. Это публичный минимум, не полный fuzz/security/load suite. Каждый запуск создаёт уникальных синтетических пользователей, чат и сообщения. Данные автоматически не удаляются: в API курса нет delete. Для чистого повтора используйте отдельный тестовый стенд/volume и осознанный сброс своего окружения. Session tokens хранятся только в памяти процесса и не печатаются/не сохраняются. diff --git a/tests/smoke.py b/tests/smoke.py index 4cbfa558..5054440f 100644 --- a/tests/smoke.py +++ b/tests/smoke.py @@ -20,6 +20,54 @@ def main(): check(any(x['id']==chat['id'] for x in listed['items']),'member cannot list chat') hidden = c.call('GET','/api/v1/chats?limit=100',actor=eve,schema='ChatPage')['json'] check(all(x['id']!=chat['id'] for x in hidden['items']),'chat leaks to outsider') + page_chats = [ + c.call( + 'POST', + '/api/v1/chats', + 201, + actor=alice, + body={'title':'Pagination test %d' % index,'member_ids':[alice['id'],bob['id']]}, + schema='Chat', + )['json'] + for index in range(4) + ] + from urllib.parse import quote + chat_pages, seen_cursors = [], set() + cursor = None + while True: + query = '/api/v1/chats?limit=2' + if cursor is not None: + query += '&cursor=' + quote(cursor, safe='') + page = c.call('GET',query,actor=alice,schema='ChatPage')['json'] + check(1 <= len(page['items']) <= 2,'chat page must contain at most the requested limit') + chat_pages.extend(page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + check(cursor not in seen_cursors,'chat pagination cursor repeated') + seen_cursors.add(cursor) + check(len(seen_cursors) <= 3,'chat pagination did not finish within expected pages') + chat_ids = [item['id'] for item in chat_pages] + check(len(chat_ids)==len(set(chat_ids)),'chat pagination duplicated an item') + check({item['id'] for item in page_chats + [chat]} <= set(chat_ids),'chat pagination lost a chat') + ordered_chats = sorted( + chat_pages, + key=lambda item:(timestamp(item['created_at']),uuid.UUID(item['id']).int), + ) + check(chat_pages==ordered_chats,'chat pagination is not ordered by (created_at, id)') + check( + chat_pages == c.call('GET','/api/v1/chats?limit=100',actor=alice,schema='ChatPage')['json']['items'], + 'chat pages differ from the complete chat list', + ) + c.call('GET','/api/v1/chats?limit=2&cursor=not-a-valid-cursor',400,actor=alice) + if page['next_cursor'] is None and seen_cursors: + foreign_cursor = next(iter(seen_cursors)) + c.call( + 'GET', + '/api/v1/chats?limit=2&cursor=' + quote(foreign_cursor,safe=''), + 400, + actor=bob, + ) key = uuid.uuid4().hex first = c.message(alice,chat,'Привет, мир 👋',key=key)['json'] second = c.message(bob,chat,'Сообщение Боба')['json'] @@ -28,7 +76,6 @@ def main(): check(first['text']=='Привет, мир 👋' and second['text']=='Сообщение Боба','message text changed') page1 = c.call('GET',path+'/messages?limit=1',actor=bob,schema='MessagePage')['json'] check(len(page1['items'])==1 and page1['next_cursor'],'first page must have one item and cursor') - from urllib.parse import quote page2 = c.call('GET',path+'/messages?limit=1&cursor='+quote(page1['next_cursor'],safe=''),actor=alice,schema='MessagePage')['json'] check(len(page2['items'])==1 and page2['next_cursor'] is None,'second page must finish history') ordered = sorted([first,second],key=lambda x:(timestamp(x['created_at']),uuid.UUID(x['id']).int))