Files
mtusi2026lab3/tests/concurrent_idempotency.py
T

41 lines
1.4 KiB
Python

"""Concurrent replay of one idempotency key through the public load balancer."""
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor
from client import Client, Failure, arguments, check, run
def main():
args = arguments("Concurrent idempotency acceptance test")
base = args.base_url
alice, bob, chat = None, None, None
setup = Client(base, args.ca_file)
alice, bob, eve, chat = setup.bootstrap()
key = uuid.uuid4().hex
barrier = threading.Barrier(16)
def send(_):
client = Client(base, args.ca_file, timeout=10)
barrier.wait()
return client.message(alice, chat, "одновременный повтор", key=key)["json"]
with ThreadPoolExecutor(max_workers=16) as pool:
messages = list(pool.map(send, range(16)))
ids = [message["id"] for message in messages]
check(len(set(ids)) == 1, "concurrent retry created duplicate message IDs")
history = setup.call(
"GET",
f"/api/v1/chats/{chat['id']}/messages?limit=100",
actor=bob,
schema="MessagePage",
)["json"]["items"]
matching = [message for message in history if message["id"] == ids[0]]
check(len(matching) == 1, "concurrent retry stored not exactly one message")
print("PASS: concurrent idempotency stored exactly one message")
if __name__ == "__main__":
run(main)