Files
mtusi2026lab4/student-tests/test_replication.py
T

111 lines
6.2 KiB
Python

"""Cross-process idempotency, access and transaction boundaries."""
from concurrent.futures import ThreadPoolExecutor
import json
from pathlib import Path
import sys
import threading
import unittest
from uuid import uuid4
import psycopg
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
sys.path.insert(0, str(ROOT / "scripts"))
from client import Client
from ops import containers
from app.config import secret
class ReplicationTests(unittest.TestCase):
def setUp(self):
self.c = Client()
self.alice, self.bob, self.eve, self.chat = self.c.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"] + "/messages"
def sql(self, query, values=()):
with psycopg.connect(secret("DATABASE_URL"), connect_timeout=2) as conn:
cur = conn.execute(query, values)
return cur.fetchall() if cur.description else None
def test_parallel_replay_across_real_replicas(self):
replicas = containers()
self.assertGreaterEqual(len(replicas), 2)
clients = [Client("http://" + row["Name"].lstrip("/") + ":8080") for row in replicas]
barrier = threading.Barrier(10)
key = uuid4().hex
def send(index):
if index < 10:
barrier.wait()
return clients[index % len(clients)].message(self.alice, self.chat, "Один результат 🧪", key=key)
with ThreadPoolExecutor(max_workers=10) as pool:
responses = list(pool.map(send, range(100)))
self.assertEqual(len({r["json"]["id"] for r in responses}), 1)
self.assertGreaterEqual(len({r["headers"]["X-Instance-Id"] for r in responses}), 2)
self.assertTrue(all(r["json"] == responses[0]["json"] for r in responses))
self.assertEqual(self.sql("SELECT count(*) FROM messages WHERE chat_id=%s", (self.chat["id"],))[0][0], 1)
self.assertEqual(self.sql("SELECT count(*) FROM message_requests WHERE chat_id=%s", (self.chat["id"],))[0][0], 1)
for _ in range(12):
self.assertEqual(self.c.message(self.alice, self.chat, "Один результат 🧪", key=key)["json"], responses[0]["json"])
report = {"requests": 100, "clients": 10, "real_containers": len(replicas),
"observed_instances": len({r["headers"]["X-Instance-Id"] for r in responses}),
"messages": 1, "replay_records": 1, "lb_replays": 12}
(ROOT / "evidence" / "idempotency.json").write_text(json.dumps(report, indent=2) + "\n")
def test_key_validation_scope_conflict_and_logout(self):
for key in (None, "", "bad key", "bad/key", "a" * 129):
headers = {} if key is None else {"Idempotency-Key": key}
self.c.call("POST", self.path, 400, actor=self.alice, body={"text": "test"}, headers=headers)
key = uuid4().hex
first = self.c.message(self.alice, self.chat, "first", key=key)["json"]
replay = self.c.call("POST", self.path, 201, actor=self.alice,
raw=b'{\n "text" : "first"\n}', headers={"Content-Type": "application/json", "Idempotency-Key": key},
schema="Message")["json"]
self.assertEqual(first, replay)
self.c.call("POST", self.path, 409, actor=self.alice, body={"text": "changed"}, headers={"Idempotency-Key": key})
second = self.c.message(self.bob, self.chat, "first", key=key)["json"]
self.assertNotEqual(first["id"], second["id"])
other = self.c.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "scope", "member_ids": [self.alice["id"]]}, schema="Chat")["json"]
self.assertNotEqual(self.c.message(self.alice, other, "first", key=key)["json"]["id"], first["id"])
self.c.call("POST", self.path, 404, actor=self.eve, body={"text": "first"}, headers={"Idempotency-Key": key})
self.c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
self.c.call("POST", self.path, 401, actor=self.alice, body={"text": "first"}, headers={"Idempotency-Key": key})
def test_failed_transaction_does_not_save_message_or_replay(self):
key = uuid4().hex
# Fail after INSERT messages, at INSERT replay, to check the whole
# transaction rather than merely validation before the first write.
self.sql("""CREATE FUNCTION lab3_test_reject() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN RAISE EXCEPTION 'synthetic transaction failure'; END $$""")
self.sql("CREATE TRIGGER lab3_test_reject BEFORE INSERT ON message_requests "
"FOR EACH ROW EXECUTE FUNCTION lab3_test_reject()")
try:
self.c.call("POST", self.path, 503, actor=self.alice, body={"text": "atomic"}, headers={"Idempotency-Key": key})
self.assertEqual(self.sql("SELECT count(*) FROM messages WHERE chat_id=%s", (self.chat["id"],))[0][0], 0)
self.assertEqual(self.sql("SELECT count(*) FROM message_requests WHERE chat_id=%s", (self.chat["id"],))[0][0], 0)
finally:
self.sql("DROP TRIGGER lab3_test_reject ON message_requests")
self.sql("DROP FUNCTION lab3_test_reject()")
first = self.c.message(self.alice, self.chat, "atomic", key=key)["json"]
self.assertEqual(self.c.message(self.alice, self.chat, "atomic", key=key)["json"], first)
def test_expired_key_and_same_session_on_all_instances(self):
observed = set()
for _ in range(30):
response = self.c.call("GET", "/api/v1/users/me", actor=self.alice, schema="User")
self.assertEqual(response["json"]["id"], self.alice["id"])
observed.add(response["headers"]["X-Instance-Id"])
self.assertGreaterEqual(len(observed), 2)
key = uuid4().hex
original = self.c.message(self.alice, self.chat, "before expiry", key=key)["json"]
self.sql("UPDATE message_requests SET expires_at=clock_timestamp()-interval '1 second' "
"WHERE user_id=%s AND chat_id=%s AND request_key=%s", (self.alice["id"], self.chat["id"], key))
fresh = self.c.message(self.alice, self.chat, "after expiry", key=key)["json"]
self.assertNotEqual(original["id"], fresh["id"])
self.assertEqual(self.c.message(self.alice, self.chat, "after expiry", key=key)["json"], fresh)
if __name__ == "__main__":
unittest.main()