forked from mtusi2026/mtusi2026lab7
174 lines
8.7 KiB
Python
174 lines
8.7 KiB
Python
"""Idempotency-Key через балансировщик (lab3). Только стандартная библиотека.
|
||
|
||
BASE_URL=https://localhost:8443 python -m unittest discover -s student-tests -p test_idempotency.py -v
|
||
|
||
Главная проверка — одновременные повторы с одним ключом, которые LB
|
||
раскладывает по РАЗНЫМ репликам (это проверяется по X-Instance-Id),
|
||
создают ровно одно сообщение. Остальное — семантика из contracts/README.md.
|
||
"""
|
||
import tlsenv # noqa: F401 lab7: HTTPS с проверкой CA стенда
|
||
import json
|
||
import threading
|
||
import unittest
|
||
import uuid
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
from urllib.parse import quote
|
||
|
||
from common import actor, create_chat, login, request
|
||
|
||
PARALLEL = 24
|
||
|
||
|
||
def history(token, chat_id):
|
||
items, cursor = [], None
|
||
for _ in range(100):
|
||
path = "/api/v1/chats/%s/messages?limit=100" % chat_id
|
||
if cursor:
|
||
path += "&cursor=" + quote(cursor, safe="")
|
||
page = request("GET", path, token=token).json
|
||
items.extend(page["items"])
|
||
cursor = page["next_cursor"]
|
||
if cursor is None:
|
||
return items
|
||
raise AssertionError("pagination did not finish")
|
||
|
||
|
||
def post(token, chat_id, body, key, raw=None):
|
||
# None -> заголовок не отправляется (см. common.request).
|
||
headers = {"Idempotency-Key": key}
|
||
if raw is not None:
|
||
return request("POST", "/api/v1/chats/%s/messages" % chat_id, token=token, raw=raw, headers=headers)
|
||
return request("POST", "/api/v1/chats/%s/messages" % chat_id, token=token, payload=body, headers=headers)
|
||
|
||
|
||
def parallel(n, fn):
|
||
barrier = threading.Barrier(n)
|
||
|
||
def run(i):
|
||
barrier.wait()
|
||
return fn(i)
|
||
|
||
with ThreadPoolExecutor(max_workers=n) as pool:
|
||
return list(pool.map(run, range(n)))
|
||
|
||
|
||
class IdempotencyTests(unittest.TestCase):
|
||
|
||
@classmethod
|
||
def setUpClass(cls):
|
||
cls.alice = actor("ida")
|
||
cls.bob = actor("idb")
|
||
cls.eve = actor("ide")
|
||
cls.chat = create_chat(cls.alice, cls.bob)
|
||
cls.chat2 = create_chat(cls.alice, cls.bob)
|
||
|
||
def test_concurrent_same_key_across_replicas_creates_one_message(self):
|
||
key = "par-" + uuid.uuid4().hex
|
||
text = "одновременно " + key
|
||
results = parallel(PARALLEL, lambda i: post(self.alice["_token"], self.chat["id"], {"text": text}, key))
|
||
statuses = [r.status for r in results]
|
||
self.assertEqual(statuses, [201] * PARALLEL, statuses)
|
||
ids = {r.json["id"] for r in results}
|
||
self.assertEqual(len(ids), 1, "same key produced different messages")
|
||
bodies = {json.dumps(r.json, sort_keys=True) for r in results}
|
||
self.assertEqual(len(bodies), 1, "replays must return the identical Message")
|
||
instances = {r.headers.get("X-Instance-Id") for r in results}
|
||
self.assertGreaterEqual(len(instances), 2, "requests did not reach >= 2 replicas: %s" % instances)
|
||
stored = [m for m in history(self.bob["_token"], self.chat["id"]) if m["text"] == text]
|
||
self.assertEqual(len(stored), 1, "history must contain exactly one message")
|
||
replayed = sum(1 for r in results if r.headers.get("Idempotency-Replayed") == "true")
|
||
self.assertEqual(replayed, PARALLEL - 1)
|
||
print("\n %d parallel POSTs, one key -> 1 message; replicas: %s" % (PARALLEL, sorted(instances)))
|
||
|
||
def test_concurrent_same_key_different_bodies_one_winner(self):
|
||
key = "race-" + uuid.uuid4().hex
|
||
results = parallel(12, lambda i: post(self.alice["_token"], self.chat["id"], {"text": "вариант %d" % i}, key))
|
||
winners = [r for r in results if r.status == 201]
|
||
losers = [r for r in results if r.status == 409]
|
||
self.assertEqual(len(winners) + len(losers), 12, [r.status for r in results])
|
||
self.assertGreaterEqual(len(winners), 1)
|
||
self.assertEqual(len({r.json["id"] for r in winners}), 1, "two bodies won with one key")
|
||
for r in losers:
|
||
self.assertEqual(r.json["error"]["code"], "conflict")
|
||
texts = [m["text"] for m in history(self.alice["_token"], self.chat["id"]) if m["text"].startswith("вариант ")]
|
||
self.assertEqual(len([t for t in texts if t == winners[0].json["text"]]), 1)
|
||
|
||
def test_sequential_replay_and_conflict(self):
|
||
key = "seq-" + uuid.uuid4().hex
|
||
first = post(self.alice["_token"], self.chat["id"], {"text": "раз"}, key)
|
||
self.assertEqual(first.status, 201)
|
||
again = post(self.alice["_token"], self.chat["id"], {"text": "раз"}, key)
|
||
self.assertEqual((again.status, again.json), (201, first.json))
|
||
other = post(self.alice["_token"], self.chat["id"], {"text": "два"}, key)
|
||
self.assertEqual(other.status, 409)
|
||
self.assertEqual(other.json["error"]["code"], "conflict")
|
||
|
||
def test_json_formatting_and_key_order_do_not_matter(self):
|
||
key = "fmt-" + uuid.uuid4().hex
|
||
first = post(self.alice["_token"], self.chat["id"], None, key, raw=b'{"text":"format"}')
|
||
spaced = post(self.alice["_token"], self.chat["id"], None, key, raw=b'{ \n "text" : "format"\n}')
|
||
self.assertEqual(first.status, 201)
|
||
self.assertEqual((spaced.status, spaced.json), (201, first.json))
|
||
|
||
def test_missing_or_invalid_key_is_400(self):
|
||
token, chat_id = self.alice["_token"], self.chat["id"]
|
||
# Не-ASCII ключ передаём сырыми UTF-8 байтами (urllib пропускает только latin-1).
|
||
non_ascii = "ключ".encode("utf-8").decode("latin-1")
|
||
bad_keys = [None, "", " ", "a" * 129, non_ascii, "has space", "semi;colon", "slash/key"]
|
||
for key in bad_keys:
|
||
with self.subTest(key=key):
|
||
r = post(token, chat_id, {"text": "x"}, key)
|
||
self.assertEqual(r.status, 400, r.json)
|
||
self.assertEqual(r.json["error"]["code"], "invalid_request")
|
||
ok = post(token, chat_id, {"text": "x"}, "A" * 128)
|
||
self.assertEqual(ok.status, 201)
|
||
ok2 = post(token, chat_id, {"text": "y"}, "a.b_c:d-1")
|
||
self.assertEqual(ok2.status, 201)
|
||
|
||
def test_auth_checked_before_key(self):
|
||
r = post(None, self.chat["id"], {"text": "x"}, None)
|
||
self.assertEqual(r.status, 401)
|
||
|
||
def test_scope_is_user_and_chat(self):
|
||
key = "scope-" + uuid.uuid4().hex
|
||
a1 = post(self.alice["_token"], self.chat["id"], {"text": "scope"}, key)
|
||
b1 = post(self.bob["_token"], self.chat["id"], {"text": "scope"}, key)
|
||
a2 = post(self.alice["_token"], self.chat2["id"], {"text": "scope"}, key)
|
||
self.assertEqual([a1.status, b1.status, a2.status], [201, 201, 201])
|
||
self.assertEqual(len({a1.json["id"], b1.json["id"], a2.json["id"]}), 3)
|
||
# Другое тело другим пользователем с тем же ключом — не конфликт.
|
||
b2 = post(self.bob["_token"], self.chat2["id"], {"text": "другое"}, key)
|
||
self.assertEqual(b2.status, 201)
|
||
|
||
def test_key_survives_new_session_of_same_user(self):
|
||
key = "sess-" + uuid.uuid4().hex
|
||
first = post(self.alice["_token"], self.chat["id"], {"text": "до перелогина"}, key)
|
||
token2 = login(self.alice)
|
||
replay = post(token2, self.chat["id"], {"text": "до перелогина"}, key)
|
||
self.assertEqual((replay.status, replay.json), (201, first.json))
|
||
|
||
def test_failed_access_does_not_burn_key(self):
|
||
key = "burn-" + uuid.uuid4().hex
|
||
denied = post(self.eve["_token"], self.chat["id"], {"text": "чужое"}, key)
|
||
self.assertEqual(denied.status, 404)
|
||
# Тот же ключ у участника и даже у того же постороннего позже — не занят.
|
||
mine = post(self.alice["_token"], self.chat["id"], {"text": "своё"}, key)
|
||
self.assertEqual(mine.status, 201)
|
||
invalid = post(self.alice["_token"], self.chat2["id"], {"text": ""}, key)
|
||
self.assertEqual(invalid.status, 400)
|
||
valid = post(self.alice["_token"], self.chat2["id"], {"text": "после 400"}, key)
|
||
self.assertEqual(valid.status, 201)
|
||
|
||
def test_replay_rechecks_session(self):
|
||
key = "revoked-" + uuid.uuid4().hex
|
||
token = login(self.bob)
|
||
first = post(token, self.chat["id"], {"text": "до logout"}, key)
|
||
self.assertEqual(first.status, 201)
|
||
self.assertEqual(request("DELETE", "/api/v1/auth/sessions/current", token=token).status, 204)
|
||
replay = post(token, self.chat["id"], {"text": "до logout"}, key)
|
||
self.assertEqual(replay.status, 401)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|