forked from mtusi2026/mtusi2026lab3
create tasks from 5
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Prove that one idempotency scope is shared across API replicas."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Concurrent cross-replica idempotency check')
|
||||
bootstrap = Client(args.base_url, args.ca_file, timeout=5)
|
||||
alice, _, _, chat = bootstrap.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Один результат для всех реплик ' + key
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
|
||||
def send(_):
|
||||
client = Client(args.base_url, args.ca_file, timeout=10)
|
||||
return client.call(
|
||||
'POST',
|
||||
path,
|
||||
201,
|
||||
actor=alice,
|
||||
body={'text': text},
|
||||
headers={'Idempotency-Key': key},
|
||||
schema='Message')
|
||||
|
||||
with ThreadPoolExecutor(max_workers=16) as pool:
|
||||
responses = list(pool.map(send, range(32)))
|
||||
|
||||
messages = [response['json'] for response in responses]
|
||||
instances = {
|
||||
response['headers']['X-Instance-Id'] for response in responses
|
||||
}
|
||||
check(all(message == messages[0] for message in messages),
|
||||
'same key returned different messages')
|
||||
check(len(instances) >= 2,
|
||||
'requests did not exercise at least two API replicas')
|
||||
|
||||
page = bootstrap.call(
|
||||
'GET', path + '?limit=100', actor=alice, schema='MessagePage')['json']
|
||||
matches = [item for item in page['items'] if item['id'] == messages[0]['id']]
|
||||
check(len(matches) == 1, 'idempotent message is missing or duplicated')
|
||||
print('PASS: 32 concurrent requests used ' + str(len(instances))
|
||||
+ ' replicas and created one message')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user