forked from mtusi2026/mtusi2026lab3
реализация 3 лабы
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import threading
|
||||
from uuid import uuid4
|
||||
|
||||
from helpers import ApiTestCase, Client
|
||||
|
||||
|
||||
class IdempotencyTests(ApiTestCase):
|
||||
def test_parallel_replay_across_replicas(self):
|
||||
key = uuid4().hex
|
||||
barrier = threading.Barrier(20)
|
||||
|
||||
def send(_):
|
||||
client = Client()
|
||||
barrier.wait(timeout=10)
|
||||
return client.message(self.alice, self.chat, "Одно сообщение 👋", key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=20) as pool:
|
||||
responses = list(pool.map(send, range(20)))
|
||||
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.read_all(self.path + "/messages", self.bob, "MessagePage"), [responses[0]["json"]])
|
||||
|
||||
def test_key_format_and_missing_key(self):
|
||||
for key in (None, "", " ", "a b", "a/b", "a" * 129):
|
||||
with self.subTest(key=key):
|
||||
headers = {} if key is None else {"Idempotency-Key": key}
|
||||
self.client.call("POST", self.path + "/messages", 400, actor=self.alice,
|
||||
body={"text": "Текст"}, headers=headers)
|
||||
for key in ("A", "a" * 128, "ABCdef012._:-"):
|
||||
self.client.message(self.alice, self.chat, key, key=key)
|
||||
|
||||
def test_conflict_and_semantic_json_replay(self):
|
||||
key = uuid4().hex
|
||||
first = self.client.message(self.alice, self.chat, "Текст 👋", key=key)["json"]
|
||||
replay = self.client.call("POST", self.path + "/messages", 201, actor=self.alice,
|
||||
raw=json.dumps({"text": "Текст 👋"}, ensure_ascii=True, indent=2).encode(),
|
||||
headers={"Content-Type": "application/json", "Idempotency-Key": key},
|
||||
schema="Message")["json"]
|
||||
self.assertEqual(first, replay)
|
||||
self.assert_error("POST", self.path + "/messages", 409, "conflict", actor=self.alice,
|
||||
body={"text": "Другой"}, headers={"Idempotency-Key": key})
|
||||
self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [first])
|
||||
|
||||
def test_scope_is_user_and_chat(self):
|
||||
key = uuid4().hex
|
||||
first = self.client.message(self.alice, self.chat, "Алиса", key=key)["json"]
|
||||
second = self.client.message(self.bob, self.chat, "Боб", key=key)["json"]
|
||||
other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, schema="Chat",
|
||||
body={"title": "Другой чат", "member_ids": [self.alice["id"]]})["json"]
|
||||
third = self.client.message(self.alice, other, "Другой чат", key=key)["json"]
|
||||
self.assertEqual(len({first["id"], second["id"], third["id"]}), 3)
|
||||
|
||||
def test_replay_rechecks_session_and_access(self):
|
||||
key = uuid4().hex
|
||||
self.client.message(self.alice, self.chat, "Текст", key=key)
|
||||
self.client.call("POST", self.path + "/messages", 404, actor=self.eve,
|
||||
body={"text": "Текст"}, headers={"Idempotency-Key": key})
|
||||
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
|
||||
self.client.call("POST", self.path + "/messages", 401, actor=self.alice,
|
||||
body={"text": "Текст"}, headers={"Idempotency-Key": key})
|
||||
Reference in New Issue
Block a user