feat: implement lab3 load balancing and idempotency

This commit is contained in:
Fedoroff
2026-10-27 20:34:22 +03:00
parent 0ab3a341aa
commit 35ae6f722e
43 changed files with 3045 additions and 42 deletions
+6 -3
View File
@@ -14,7 +14,7 @@ import uuid
ROOT = Path(__file__).resolve().parents[1]
LAB = json.loads((ROOT / 'lab.json').read_text())['number']
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text())
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text(encoding='utf-8'))
class Failure(AssertionError):
pass
@@ -24,11 +24,14 @@ def check(condition, message):
raise Failure(message)
def timestamp(value):
check(isinstance(value, str) and value.endswith('Z'), 'timestamp must be UTC ending in Z')
check(
isinstance(value, str) and value.endswith('Z'),
'timestamp must be UTC ending in Z'
)
try:
return dt.datetime.fromisoformat(value[:-1] + '+00:00')
except ValueError as exc:
raise Failure('invalid RFC3339 timestamp') from exc
raise Failure(f'invalid RFC3339 timestamp: {value!r}') from exc
def validate(value, schema, where='response'):
"""Validate the JSON Schema subset used by the supplied contract, not arbitrary OpenAPI."""
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Concurrent idempotency smoke through the public load balancer."""
import concurrent.futures
import uuid
from client import Client, arguments, check, run
def main():
args = arguments('Concurrent idempotency verification')
c = Client(args.base_url, args.ca_file)
alice, bob, _, chat = c.bootstrap()
key = uuid.uuid4().hex
text = 'Конкурентная отправка ' + key
def send(_):
worker = Client(args.base_url, args.ca_file)
return worker.message(alice, chat, text, key=key)
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
results = list(executor.map(send, range(16)))
messages = [r['json'] for r in results]
check(all(m == messages[0] for m in messages), 'retries returned different messages')
instances = {r['headers']['X-Instance-Id'] for r in results}
check(len(instances) >= 2, 'only one API replica observed during concurrent requests')
page = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100',
actor=bob, schema='MessagePage')['json']
check(sum(m['id'] == messages[0]['id'] for m in page['items']) == 1,
'duplicate or missing message in history')
print('PASS: 16 concurrent retries, one message, replicas:', ', '.join(sorted(instances)))
if __name__ == '__main__':
run(main)