38 lines
2.2 KiB
Python
38 lines
2.2 KiB
Python
"""Конкурентные и отрицательные проверки общей идемпотентности через LB."""
|
|
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, check
|
|
|
|
c = Client()
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
key = uuid.uuid4().hex
|
|
|
|
|
|
def send(i):
|
|
return Client().call('POST', path, 201, actor=alice,
|
|
body={'text': 'Общий конкурентный ключ', **({'attachment_ids': []} if i % 2 else {})},
|
|
headers={'Idempotency-Key': key}, schema='Message')
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=16) as pool:
|
|
results = list(pool.map(send, range(32)))
|
|
check(len({r['json']['id'] for r in results}) == 1, 'повтор создал дубли')
|
|
instances = {r['headers']['X-Instance-Id'] for r in results}
|
|
check(len(instances) >= 2, 'не наблюдаются разные процессы')
|
|
c.call('POST', path, 409, actor=alice, body={'text': 'Другой текст'}, headers={'Idempotency-Key': key})
|
|
for invalid in ('', 'bad key', 'x' * 129, 'bad/key'):
|
|
c.call('POST', path, 400, actor=alice, body={'text': 'Проверка'}, headers={'Idempotency-Key': invalid})
|
|
c.call('POST', path, 404, actor=eve, body={'text': 'Проверка'}, headers={'Idempotency-Key': key})
|
|
other = c.message(bob, chat, 'Независимый пользователь', key=key)['json']
|
|
check(other['id'] != results[0]['json']['id'], 'ключ не изолирован по пользователю')
|
|
second = c.call('POST', '/api/v1/chats', 201, actor=alice,
|
|
body={'title': 'Другой чат', 'member_ids': [alice['id']]}, schema='Chat')['json']
|
|
c.message(alice, second, 'Независимый чат', key=key)
|
|
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
|
c.call('POST', path, 401, actor=alice, body={'text': 'Общий конкурентный ключ'}, headers={'Idempotency-Key': key})
|
|
print('PASS: 32 параллельных запроса,', len(instances), 'реплики; один результат, конфликт, валидация, ACL, области ключа и отзыв сессии.')
|