Files
mtusi2026lab3-fork/student_tests/test_failover_both.py
T
2026-10-23 18:10:07 +03:00

59 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Останавливает КАЖДУЮ реплику по очереди и проверяет восстановление."""
import time
import uuid
from common import (
BASE_URL, Client, Failure, check, observe_instances, print_pass,
restore_api, stop_one,
)
def main():
c = Client(BASE_URL)
alice, bob, eve, chat = c.bootstrap()
seen = observe_instances(c, alice, n=40)
check(len(seen) >= 2, 'видна одна реплика: %r' % seen)
print(' instances:', seen)
for alias in sorted(seen):
print('--- failover test for %s' % alias)
stop_one(alias)
time.sleep(1)
first_ok = None
after = []
deadline = time.monotonic() + 15
key = 'fo-' + uuid.uuid4().hex
while time.monotonic() < deadline:
try:
r = c.message(alice, chat, 'after-stop-' + key, key=key)
if first_ok is None:
first_ok = time.monotonic()
after.append(r['headers']['X-Instance-Id'])
key = 'fo-' + uuid.uuid4().hex
except Failure:
pass
time.sleep(0.2)
check(first_ok is not None, 'запись не восстановилась за 15с')
check(len(after) >= 5, 'слишком мало успешных записей после отказа: %d' % len(after))
check(alias not in after[-5:], 'остановленная реплика %s всё ещё отвечает' % alias)
print(' after stop %s: %d writes, последние instances=%r'
% (alias, len(after), after[-3:]))
restore_api()
time.sleep(2)
seen2 = observe_instances(c, alice, n=60)
print(' после restore видно:', seen2)
seen3 = observe_instances(c, alice, n=60)
check(len(seen3) >= 2, 'после restore видна одна реплика: %r' % seen3)
print_pass('failover both replicas')
if __name__ == '__main__':
try:
main()
except Failure as e:
print('FAIL:', e)
raise SystemExit(1)