forked from mtusi2026/mtusi2026lab4
chore(solution): port lab3 sources
This commit is contained in:
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check
|
||||
|
||||
WORKERS = 20
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, _, _, chat = c.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Одновременный повтор ' + key
|
||||
barrier = threading.Barrier(WORKERS)
|
||||
results, errors = [], []
|
||||
lock = threading.Lock()
|
||||
|
||||
def send():
|
||||
barrier.wait()
|
||||
try:
|
||||
r = c.message(alice, chat, text, key=key)
|
||||
with lock:
|
||||
results.append((r['json'], r['headers']['X-Instance-Id']))
|
||||
except AssertionError as exc:
|
||||
with lock:
|
||||
errors.append(str(exc))
|
||||
|
||||
threads = [threading.Thread(target=send) for _ in range(WORKERS)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', actor=alice, schema='MessagePage')['json']['items']
|
||||
stored = [m for m in history if m['text'] == text]
|
||||
instances = sorted({i for _, i in results})
|
||||
ids = {m['id'] for m, _ in results}
|
||||
print('parallel requests with one key:', WORKERS)
|
||||
print('successful responses (201):', len(results))
|
||||
print('failed requests:', len(errors), errors[:3])
|
||||
print('distinct message ids in responses:', len(ids))
|
||||
print('messages stored in history:', len(stored))
|
||||
print('instances that served requests:', instances)
|
||||
check(not errors, 'some requests failed')
|
||||
check(len(ids) == 1 and len(stored) == 1, 'duplicate message created')
|
||||
check(all(m == results[0][0] for m, _ in results), 'replayed responses differ')
|
||||
check(len(instances) >= 2, 'requests were not spread across replicas')
|
||||
print('OK: exactly one message created')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user