forked from mtusi2026/mtusi2026lab3
38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
"""Exercise concurrent retries through the shared load balancer."""
|
|
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, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Student checks: concurrent idempotency on two replicas')
|
|
client = Client(args.base_url, args.ca_file)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
key = uuid.uuid4().hex
|
|
|
|
def send(_):
|
|
return Client(args.base_url, args.ca_file).message(alice, chat, 'Повтор', key=key)
|
|
|
|
with ThreadPoolExecutor(max_workers=8) as executor:
|
|
responses = list(executor.map(send, range(16)))
|
|
first = responses[0]['json']
|
|
instances = {item['headers']['X-Instance-Id'] for item in responses}
|
|
check(len(instances) >= 2, 'concurrent requests did not reach two API replicas')
|
|
check(all(item['json'] == first for item in responses), 'replays differ')
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
page = client.call('GET', path, actor=bob, schema='MessagePage')['json']
|
|
check(page['items'] == [first], 'duplicate persisted message')
|
|
client.call('POST', path, 409, actor=alice, body={'text': 'Другой текст'},
|
|
headers={'Idempotency-Key': key})
|
|
independent = client.message(bob, chat, 'Повтор', key=key)['json']
|
|
check(independent['id'] != first['id'], 'key incorrectly shared between users')
|
|
print(f'PASS: {len(responses)} concurrent requests on {len(instances)} replicas yielded one message')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|