43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
import sys
|
|
import threading
|
|
from uuid import uuid4
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
|
from client import Client, check
|
|
|
|
|
|
def main():
|
|
client = Client()
|
|
alice, _, _, chat = client.bootstrap()
|
|
key = uuid4().hex
|
|
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
text = "Проверка одновременного повтора " + key
|
|
barrier = threading.Barrier(12)
|
|
|
|
def send(_):
|
|
local = Client(timeout=10)
|
|
barrier.wait(timeout=10)
|
|
response = local.message(alice, chat, text, key=key)
|
|
return response["json"], response["headers"]["X-Instance-Id"]
|
|
|
|
with ThreadPoolExecutor(max_workers=12) as workers:
|
|
results = list(workers.map(send, range(12)))
|
|
|
|
ids = {message["id"] for message, _ in results}
|
|
instances = {instance for _, instance in results}
|
|
check(len(ids) == 1, "Повтор создал несколько сообщений")
|
|
check(len(instances) == 2, "Не удалось проверить обе реплики")
|
|
check(all(result[0] == results[0][0] for result in results), "Ответы различаются")
|
|
client.call("POST", path, 409, actor=alice, body={"text": "Другой текст"},
|
|
headers={"Idempotency-Key": key})
|
|
page = client.call("GET", path + "?limit=100", actor=alice, schema="MessagePage")["json"]
|
|
check(sum(item["id"] in ids for item in page["items"]) == 1,
|
|
"В истории найден дубль сообщения")
|
|
print("PASS: 12 параллельных запросов; 2 реплики; 1 сообщение; повтор с другим текстом — 409")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|