Files
mtusi2026lab6/student-tests/idempotency_concurrency.py
T
2026-11-05 21:17:50 +03:00

33 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Один ключ одновременно через LB создаёт ровно одно сообщение."""
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import sys
import uuid
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, check, run # noqa: E402
def main() -> None:
client = Client()
alice, _bob, _eve, chat = client.bootstrap()
key = uuid.uuid4().hex
text = "concurrent idempotency " + key
def send(_number: int):
return Client().message(alice, chat, text, key=key)["json"]
with ThreadPoolExecutor(max_workers=16) as pool:
replies = list(pool.map(send, range(16)))
check(all(reply == replies[0] for reply in replies), "replays returned different messages")
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100"
history = client.call("GET", path, actor=alice, schema="MessagePage")["json"]["items"]
check(sum(message["id"] == replies[0]["id"] for message in history) == 1, "concurrent retries created duplicates")
print("PASS: concurrent idempotency through load balancer")
if __name__ == "__main__":
run(main)