Files
mtusi2026lab3-fork/student_tests/test_rolling.py
T
2026-10-20 18:35:44 +03:00

79 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Rolling update одной реплики под нагрузкой (уровень 4)."""
import json
import threading
import time
import uuid
from common import (
BASE_URL, Client, Failure, check, compose, print_pass,
)
def main():
c = Client(BASE_URL)
alice, bob, eve, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id'] + '/messages'
stop = threading.Event()
stats = {'ok': 0, 'err': 0, 'instances': {}}
def load():
local = Client(BASE_URL, timeout=5)
while not stop.is_set():
try:
key = uuid.uuid4().hex
r = local.call('POST', path, 201, actor=alice,
body={'text': 'roll-' + key},
headers={'Idempotency-Key': key},
schema='Message')
stats['ok'] += 1
iid = r['headers'].get('X-Instance-Id')
stats['instances'][iid] = stats['instances'].get(iid, 0) + 1
except Failure:
stats['err'] += 1
time.sleep(0.05)
t = threading.Thread(target=load, daemon=True)
t.start()
time.sleep(10)
base_ok = stats['ok']
base_err = stats['err']
print(' base: ok=%d err=%d' % (base_ok, base_err))
# Rolling update api1.
compose('up', '-d', '--no-deps', '--force-recreate', 'api1', timeout=180)
time.sleep(15)
stop.set()
t.join(timeout=10)
delta_ok = stats['ok'] - base_ok
delta_err = stats['err'] - base_err
print(' after rolling: ok=+%d err=+%d instances=%r'
% (delta_ok, delta_err, stats['instances']))
check(delta_ok > 0, 'rolling update остановил запись полностью')
# Ошибки допустимы кратковременно, но не должны доминировать.
check(delta_err <= max(5, delta_ok // 3),
'слишком много ошибок при rolling update: +%d ok / +%d err'
% (delta_ok, delta_err))
# После rolling обе реплики должны снова обслуживать.
seen_final = {}
for _ in range(40):
r = c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
iid = r['headers']['X-Instance-Id']
seen_final[iid] = seen_final.get(iid, 0) + 1
check(len(seen_final) >= 2,
'после rolling видна одна реплика: %r' % seen_final)
print(' final instances:', seen_final)
print_pass('rolling update under load')
if __name__ == '__main__':
try:
main()
except Failure as e:
print('FAIL:', e)
raise SystemExit(1)