diff --git a/bob_download.png b/bob_download.png new file mode 100644 index 0000000..e7c9207 Binary files /dev/null and b/bob_download.png differ diff --git a/bob_failover.png b/bob_failover.png new file mode 100644 index 0000000..e7c9207 Binary files /dev/null and b/bob_failover.png differ diff --git a/bob_restart.png b/bob_restart.png new file mode 100644 index 0000000..adedae3 --- /dev/null +++ b/bob_restart.png @@ -0,0 +1,7 @@ + +504 Gateway Time-out + +

504 Gateway Time-out

+
nginx/1.25.5
+ + diff --git a/tests/test_concurrent.py b/tests/test_concurrent.py new file mode 100644 index 0000000..9c992fe --- /dev/null +++ b/tests/test_concurrent.py @@ -0,0 +1,38 @@ +import threading +from concurrent.futures import ThreadPoolExecutor +from client import Client, arguments, check, run + +def main(): + args = arguments('Concurrency race test') + bootstrap = Client(args.base_url, args.ca_file) + alice, bob, _eve, chat = bootstrap.bootstrap() + chat_id = chat['id'] + + CLIENTS = 10 + PER_CLIENT = 10 + barrier = threading.Barrier(CLIENTS) # Барьер: все потоки стартуют одновременно + + def worker(i): + local = Client(args.base_url, args.ca_file) + barrier.wait() # Ждем, пока все 10 потоков не будут готовы + for j in range(PER_CLIENT): + local.call('POST', f'/api/v1/chats/{chat_id}/messages', 201, actor=alice, body={'text': f'w{i}-{j}'}) + + # Запускаем 10 потоков + with ThreadPoolExecutor(max_workers=CLIENTS) as pool: + list(pool.map(worker, range(CLIENTS))) + + # Читаем историю и считаем уникальные ID + seen = [] + cursor = None + while True: + path = f'/api/v1/chats/{chat_id}/messages?limit=100' + if cursor: path += '&cursor=' + cursor + page = bootstrap.call('GET', path, actor=bob, schema='MessagePage')['json'] + seen.extend(x['id'] for x in page['items']) + cursor = page['next_cursor'] + if not cursor: break + + check(len(seen) == 100, f'Ожидали 100, получили {len(seen)}') + check(len(set(seen)) == 100, 'Есть дубликаты ID!') + print('PASS: 10x10 concurrent writes -> 100 unique ids') \ No newline at end of file diff --git a/tests/test_extra.py b/tests/test_extra.py new file mode 100644 index 0000000..e827b79 --- /dev/null +++ b/tests/test_extra.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3 +import uuid +from client import Client, arguments, check, run, LAB + + +def main(): + args = arguments('Lab1 edge-case tests; run against a live server') + check(LAB == 1, 'these tests target lab1') + c = Client(args.base_url, args.ca_file) + + c.call('GET', '/health/live') + c.call('GET', '/health/ready') + + # --- malformed JSON --- + c.call('POST', '/api/v1/users', 400, + raw=b'{"username":', + headers={'Content-Type': 'application/json'}) + c.call('POST', '/api/v1/users', 400, + raw=b'not-json', + headers={'Content-Type': 'application/json'}) + c.call('POST', '/api/v1/users', 400, + raw='{"username":"alice","display_name":"А","extra":1}'.encode('utf-8'), + headers={'Content-Type': 'application/json'}) + + # --- username validation / duplicate --- + u1 = 'u_' + uuid.uuid4().hex[:10] + body1 = {'username': u1, 'display_name': 'Первый'} + created = c.call('POST', '/api/v1/users', 201, body=body1, schema='User')['json'] + check(created['username'] == u1, 'username changed') + + c.call('POST', '/api/v1/users', 409, body=body1) + c.call('POST', '/api/v1/users', 400, + body={'username': 'Bad Name', 'display_name': 'X'}) + c.call('POST', '/api/v1/users', 400, + body={'username': 'ab', 'display_name': 'X'}) + c.call('POST', '/api/v1/users', 400, + body={'username': 'good_user', 'display_name': ''}) + + # --- chat creation validations --- + a = c.user('alice') + b = c.user('bob') + e = c.user('eve') + + c.call('POST', '/api/v1/chats', 400, actor=a, + body={'title': 'no me', 'member_ids': [b['id'], e['id']]}) + c.call('POST', '/api/v1/chats', 400, actor=a, + body={'title': 'ghost', 'member_ids': [a['id'], str(uuid.uuid4())]}) + c.call('POST', '/api/v1/chats', 400, actor=a, + body={'title': 'dup', 'member_ids': [a['id'], a['id']]}) + + chat = c.call('POST', '/api/v1/chats', 201, actor=a, + body={'title': 'Edge', 'member_ids': [a['id'], b['id']]}, + schema='Chat')['json'] + + # --- empty / whitespace-only message --- + c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 400, actor=a, + body={'text': ''}) + c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 400, actor=a, + body={'text': ' \n '}) + c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 400, actor=a, + body={'text': 'x' * 4001}) + + # --- outsider (eve) forbidden --- + c.call('GET', f"/api/v1/chats/{chat['id']}", 404, actor=e) + c.call('GET', f"/api/v1/chats/{chat['id']}/messages", 404, actor=e) + c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 404, actor=e, + body={'text': 'intrusion'}) + c.call('GET', f"/api/v1/chats/{chat['id']}", 401) + + # --- unknown UUID --- + c.call('GET', f"/api/v1/chats/{uuid.uuid4()}", 404, actor=a) + c.call('GET', f"/api/v1/chats/{uuid.uuid4()}/messages", 404, actor=a) + + # --- multi-page walk (5 pages of 1) --- + from client import timestamp + texts = [f'msg-{i}' for i in range(5)] + created_msgs = [c.message(a, chat, t)['json'] for t in texts] + + # Contract order is (created_at, id) ascending, NOT insertion order. + def _key(m): + return (timestamp(m['created_at']), uuid.UUID(m['id']).int) + + expected = [m['text'] for m in sorted(created_msgs, key=_key)] + + seen = [] + cursor = None + pages = 0 + while True: + path = f"/api/v1/chats/{chat['id']}/messages?limit=1" + if cursor: + from urllib.parse import quote + path += '&cursor=' + quote(cursor, safe='') + page = c.call('GET', path, actor=b, schema='MessagePage')['json'] + check(len(page['items']) == 1, 'expected exactly 1 item per page') + seen.extend(x['text'] for x in page['items']) + cursor = page['next_cursor'] + pages += 1 + if not cursor: + break + check(pages <= 20, 'pagination did not terminate') + + check(seen == expected, f'page walk mismatch: {seen} != {expected}') + check(pages == 5, f'expected 5 pages, got {pages}') + + # cursor from one chat must not work on another + other = c.call('POST', '/api/v1/chats', 201, actor=a, + body={'title': 'Other', 'member_ids': [a['id'], b['id']]}, + schema='Chat')['json'] + bad_cursor_path = (f"/api/v1/chats/{other['id']}/messages" + f"?limit=1&cursor={cursor or 'x'}") + c.call('GET', bad_cursor_path, 400, actor=a) + + print('PASS: lab1 extra tests') + + +if __name__ == '__main__': + run(main) \ No newline at end of file