#!/usr/bin/env python3 """Последовательная проверка остановки каждой API-реплики.""" import json import subprocess import sys import time import uuid from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client def docker(*args): return subprocess.run(('docker', 'compose') + args, cwd=ROOT, check=True, capture_output=True, text=True) def main(): results = [] for target in ('api-1', 'api-2', 'api-3', 'api-4'): docker('start', 'api-1', 'api-2', 'api-3', 'api-4') time.sleep(4) client = Client('http://localhost:8080', timeout=3) alice, _, _, chat = client.bootstrap() docker('stop', target) started = time.monotonic() successes = 0 for _ in range(12): try: client.message( alice, chat, 'failover-' + target + '-' + uuid.uuid4().hex, key=uuid.uuid4().hex, ) successes += 1 except Exception: pass time.sleep(0.1) recovery = time.monotonic() - started docker('start', target) if successes == 0 or recovery > 10: raise RuntimeError('failover did not recover for ' + target) results.append({'stopped': target, 'successful_writes': successes, 'seconds': round(recovery, 3)}) print(results[-1]) (ROOT / 'evidence/failover_all.json').write_text(json.dumps(results, indent=2) + '\n') if __name__ == '__main__': main()