forked from mtusi2026/mtusi2026lab3
ЛР 3 — БПИ2302 — Щербацевич — на 5
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Тот же сценарий, что tests/resilience.py, но hook запускается так, как умеет ОС.
|
||||
|
||||
Linux выполняет scripts/restart.sh напрямую.
|
||||
Windows не умеет CreateProcess для shell-скрипта, поэтому здесь используется Git Bash.
|
||||
Скрипт тот же: перезапуск api, postgres и redis без удаления томов.
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
|
||||
from client import Client, Failure, check, run
|
||||
|
||||
|
||||
def hook_command():
|
||||
script = ROOT / "scripts" / "restart.sh"
|
||||
if os.name != "nt":
|
||||
return [str(script)]
|
||||
bash = Path(os.environ.get("BASH", r"C:\Program Files\Git\bin\bash.exe"))
|
||||
check(bash.is_file(), "Git Bash is required to launch scripts/restart.sh on Windows")
|
||||
return [str(bash), "-c", "sh scripts/restart.sh"]
|
||||
|
||||
|
||||
def history(client, actor, chat):
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100"
|
||||
result = []
|
||||
seen = set()
|
||||
for _ in range(1000):
|
||||
page = client.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
||||
result.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
return result
|
||||
check(cursor not in seen, "cursor cycle")
|
||||
seen.add(cursor)
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100&cursor=" + quote(cursor, safe="")
|
||||
raise Failure("history pagination exceeded safety bound")
|
||||
|
||||
|
||||
def main():
|
||||
client = Client("http://127.0.0.1:8080", timeout=5)
|
||||
alice, bob, _eve, chat = client.bootstrap()
|
||||
original = client.message(alice, chat, "Сохранить при отказе")["json"]
|
||||
print("Executing scripts/restart.sh")
|
||||
completed = subprocess.run(hook_command(), cwd=str(ROOT))
|
||||
check(completed.returncode == 0, "restart hook failed")
|
||||
deadline = time.monotonic() + 90
|
||||
while True:
|
||||
try:
|
||||
client.call("GET", "/health/ready", schema="Health")
|
||||
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
||||
break
|
||||
except Failure:
|
||||
check(time.monotonic() < deadline, "stack/session did not recover within 90 seconds")
|
||||
time.sleep(0.5)
|
||||
messages = history(client, bob, chat)
|
||||
check(any(item == original for item in messages), "confirmed message lost or altered")
|
||||
print("PASS: persistence preserved confirmed data and existing session")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user