forked from mtusi2026/mtusi2026lab6
122 lines
6.2 KiB
Python
122 lines
6.2 KiB
Python
"""Идемпотентность отправки сообщений (lab3) и распределение по репликам. Запуск:
|
|
python student-tests/test_idempotency.py (BASE_URL по умолчанию http://127.0.0.1:8080, через LB)
|
|
"""
|
|
import os
|
|
import sys
|
|
import unittest
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from urllib.parse import quote
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
|
from test_api import call, psql, register # noqa: E402
|
|
|
|
|
|
def chat_with(owner, *others):
|
|
ids = [owner["user"]["id"]] + [o["user"]["id"] for o in others]
|
|
return call("POST", "/api/v1/chats", {"title": "идемпотентность", "member_ids": ids}, owner["token"])[2]
|
|
|
|
|
|
def history(token, chat_id):
|
|
items, cursor = [], None
|
|
while True:
|
|
page = call("GET", "/api/v1/chats/%s/messages?limit=100%s" % (chat_id, "&cursor=" + quote(cursor, safe="") if cursor else ""), token=token)[2]
|
|
items += page["items"]
|
|
cursor = page["next_cursor"]
|
|
if not cursor:
|
|
return items
|
|
|
|
|
|
class IdempotencyTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.a, self.b, self.eve = register(), register(), register()
|
|
self.chat = chat_with(self.a, self.b)
|
|
self.path = "/api/v1/chats/%s/messages" % self.chat["id"]
|
|
|
|
def post(self, actor, body, key, path=None):
|
|
return call("POST", path or self.path, body, actor["token"], headers={"Idempotency-Key": key})
|
|
|
|
def test_key_validation(self):
|
|
for headers in ({}, {"Idempotency-Key": ""}, {"Idempotency-Key": "a b"},
|
|
{"Idempotency-Key": "x" * 129}, {"Idempotency-Key": "a/b"}):
|
|
h = dict(headers)
|
|
h.setdefault("Content-Type", "application/json")
|
|
r = call("POST", self.path, raw=b'{"text":"x"}', token=self.a["token"], headers=h)
|
|
self.assertEqual(r[0], 400, headers)
|
|
self.assertEqual(r[2]["error"]["code"], "invalid_request")
|
|
self.assertEqual(self.post(self.a, {"text": "ok"}, "A-z_0.9:-" + "k" * 100)[0], 201)
|
|
|
|
def test_replay_returns_same_message_and_creates_no_duplicate(self):
|
|
key = uuid.uuid4().hex
|
|
first = self.post(self.a, {"text": "один раз"}, key)
|
|
second = self.post(self.a, {"text": "один раз"}, key)
|
|
self.assertEqual((first[0], second[0]), (201, 201))
|
|
self.assertEqual(first[2], second[2])
|
|
self.assertEqual(len([m for m in history(self.b["token"], self.chat["id"]) if m["text"] == "один раз"]), 1)
|
|
|
|
def test_different_body_same_key_is_409(self):
|
|
key = uuid.uuid4().hex
|
|
self.post(self.a, {"text": "первое"}, key)
|
|
r = self.post(self.a, {"text": "другое"}, key)
|
|
self.assertEqual(r[0], 409)
|
|
self.assertEqual(r[2]["error"]["code"], "conflict")
|
|
|
|
def test_scope_is_user_and_chat(self):
|
|
key = uuid.uuid4().hex
|
|
m1 = self.post(self.a, {"text": "общий ключ"}, key)[2]
|
|
m2 = self.post(self.b, {"text": "общий ключ"}, key)[2] # другой пользователь
|
|
other = chat_with(self.a)
|
|
m3 = self.post(self.a, {"text": "общий ключ"}, key, "/api/v1/chats/%s/messages" % other["id"])[2] # другой чат
|
|
self.assertEqual(len({m1["id"], m2["id"], m3["id"]}), 3)
|
|
|
|
def test_replay_rechecks_session_and_membership(self):
|
|
key = uuid.uuid4().hex
|
|
self.post(self.a, {"text": "защищено"}, key)
|
|
self.assertEqual(self.post(self.eve, {"text": "защищено"}, key)[0], 404) # посторонний
|
|
r = call("POST", self.path, {"text": "защищено"}, "Z" * 43, headers={"Idempotency-Key": key})
|
|
self.assertEqual(r[0], 401) # недействительный токен
|
|
logout = register()
|
|
chat = chat_with(logout)
|
|
path = "/api/v1/chats/%s/messages" % chat["id"]
|
|
self.post(logout, {"text": "x"}, key, path)
|
|
call("DELETE", "/api/v1/auth/sessions/current", token=logout["token"])
|
|
self.assertEqual(call("POST", path, {"text": "x"}, logout["token"], headers={"Idempotency-Key": key})[0], 401)
|
|
|
|
def test_semantically_equal_body_is_replayed(self):
|
|
key = uuid.uuid4().hex
|
|
first = self.post(self.a, {"text": "эквивалент"}, key)
|
|
raw = call("POST", self.path, raw=b'{ "text" : "\u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442" }',
|
|
token=self.a["token"], headers={"Idempotency-Key": key, "Content-Type": "application/json"})
|
|
self.assertEqual((raw[0], raw[2]), (201, first[2]))
|
|
|
|
def test_concurrent_same_key_creates_exactly_one_message_across_replicas(self):
|
|
key = uuid.uuid4().hex
|
|
with ThreadPoolExecutor(24) as pool:
|
|
results = list(pool.map(lambda _: self.post(self.a, {"text": "гонка ключа"}, key), range(24)))
|
|
self.assertEqual({r[0] for r in results}, {201})
|
|
self.assertEqual(len({r[2]["id"] for r in results}), 1)
|
|
instances = {r[1]["X-Instance-Id"] for r in results}
|
|
self.assertGreaterEqual(len(instances), 2, "повторы должны обслуживаться разными репликами: %s" % instances)
|
|
found = [m for m in history(self.b["token"], self.chat["id"]) if m["text"] == "гонка ключа"]
|
|
self.assertEqual(len(found), 1)
|
|
|
|
def test_expired_key_is_reusable_after_ttl(self):
|
|
key = uuid.uuid4().hex
|
|
first = self.post(self.a, {"text": "ttl"}, key)[2]
|
|
psql("UPDATE idempotency_keys SET created_at = now() - interval '25 hours' WHERE idem_key = '%s'" % key)
|
|
again = self.post(self.a, {"text": "ttl"}, key)
|
|
self.assertEqual(again[0], 201)
|
|
self.assertNotEqual(again[2]["id"], first["id"], "после 24 ч ключ считается новым")
|
|
|
|
def test_session_is_shared_between_replicas_without_relogin(self):
|
|
seen = set()
|
|
for _ in range(30):
|
|
r = call("GET", "/api/v1/users/me", token=self.a["token"])
|
|
self.assertEqual(r[0], 200)
|
|
seen.add(r[1]["X-Instance-Id"])
|
|
self.assertGreaterEqual(len(seen), 2, seen)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|