forked from mtusi2026/mtusi2026lab4
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
"""Eight parallel retries with one key must create one message."""
|
|
|
|
import sys
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure, check # noqa: E402
|
|
|
|
|
|
def main() -> None:
|
|
client = Client("http://127.0.0.1:8080", timeout=20)
|
|
alice = client.user("race_alice")
|
|
bob = client.user("race_bob")
|
|
chat = client.call(
|
|
"POST",
|
|
"/api/v1/chats",
|
|
201,
|
|
actor=alice,
|
|
body={"title": "Гонка", "member_ids": [alice["id"], bob["id"]]},
|
|
schema="Chat",
|
|
)["json"]
|
|
key = "race-" + alice["id"][:8]
|
|
text = "одно сообщение " + key
|
|
|
|
def once(_index):
|
|
local = Client("http://127.0.0.1:8080", timeout=20)
|
|
return local.message(alice, chat, text, key=key)["json"]
|
|
|
|
with ThreadPoolExecutor(max_workers=8) as pool:
|
|
bodies = list(pool.map(once, range(8)))
|
|
check(len({item["id"] for item in bodies}) == 1, "parallel retries created more than one message")
|
|
check(all(item == bodies[0] for item in bodies), "parallel retries returned different messages")
|
|
seen = client.call(
|
|
"GET",
|
|
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
|
actor=bob,
|
|
schema="MessagePage",
|
|
)["json"]["items"]
|
|
check(sum(1 for item in seen if item["text"] == text) == 1, "history contains duplicate race messages")
|
|
print("PASS: eight parallel idempotent retries created one message")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (Failure, Exception) as exc:
|
|
print("FAIL:", exc)
|
|
raise SystemExit(1)
|