Files
mtusi2026lab4/student-tests/idempotency.py
T

38 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Проверка общего Idempotency-Key через обе API-реплики."""
import sys
import uuid
from concurrent.futures import ThreadPoolExecutor
sys.path.insert(0, 'tests')
from client import Client, check
def main():
client = Client('http://localhost:8080')
alice, _, _, chat = client.bootstrap()
key = 'student-' + uuid.uuid4().hex
body = {'text': 'одно сообщение при восьми повторах'}
def send(_):
return client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 201,
actor=alice, body=body,
headers={'Idempotency-Key': key}, schema='Message',
)
with ThreadPoolExecutor(max_workers=8) as pool:
responses = list(pool.map(send, range(8)))
ids = {item['json']['id'] for item in responses}
check(len(ids) == 1, 'concurrent retries created more than one message')
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 409,
actor=alice, body={'text': 'другой текст'},
headers={'Idempotency-Key': key},
)
print('PASS: concurrent idempotency, one message id:', next(iter(ids)))
if __name__ == '__main__':
main()