lab2: postgres, redis sessions, persistence, EXPLAIN and tests
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-19 21:35:11 +03:00
parent 65003b6119
commit 358ea9371b
20 changed files with 675 additions and 20 deletions
+34
View File
@@ -0,0 +1,34 @@
import sys, os, subprocess
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'tests')))
from client import Client
def main():
c = Client()
# Создаем одного пользователя и один чат через API
alice, bob, eve, chat = c.bootstrap()
chat_id = chat['id']
alice_id = alice['id']
print(f"Чат создан: {chat_id}")
print("Генерируем 10000 сообщений напрямую в PostgreSQL (это займет пару секунд)...")
# SQL-запрос, который создаст 10 000 строк за один раз
sql = f"""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT
gen_random_uuid(),
'{chat_id}'::uuid,
'{alice_id}'::uuid,
'Bulk message ' || i,
now() - interval '1 second' * (10000 - i)
FROM generate_series(1, 10000) AS i;
"""
subprocess.run(
["docker", "compose", "exec", "-T", "db", "psql", "-U", "user", "-d", "messenger", "-c", sql],
check=True
)
print("Готово! 10000 сообщений добавлено.")
print(f"ID чата для EXPLAIN: {chat_id}")
if __name__ == '__main__':
main()
+1
View File
@@ -0,0 +1 @@
docker compose restart
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
docker compose restart
+29
View File
@@ -0,0 +1,29 @@
import subprocess
import time
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'tests')))
from client import Client, Failure
def main():
print("Stopping Redis...")
# stdout и stderr в DEVNULL, чтобы PowerShell не ругался на вывод Docker
subprocess.run(["docker", "compose", "stop", "redis"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
time.sleep(2)
c = Client()
try:
# Пытаемся получить профиль с фейковым токеном.
# Так как Redis лежит, сервер должен попытаться прочитать сессию,
# поймать ошибку RedisError и вернуть 503, а не упасть с 500.
c.call('GET', '/api/v1/users/me', 503, headers={'Authorization': 'Bearer fake_token'})
print("PASS: Redis is down, API correctly returned 503")
except Failure as e:
print(f"FAIL: {e}")
sys.exit(1)
finally:
print("Starting Redis back...")
subprocess.run(["docker", "compose", "start", "redis"], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if __name__ == '__main__':
main()