forked from mtusi2026/mtusi2026lab2
101 lines
4.5 KiB
Python
Executable File
101 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import hashlib
|
|
import time
|
|
|
|
from stack import BASE_URL, Client, Failure, check, compose, main_wrapper, ok, valkey
|
|
|
|
|
|
def wait_ready(c, seconds=60):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
c.call("GET", "/health/ready", 200)
|
|
return
|
|
except Failure:
|
|
time.sleep(0.5)
|
|
raise Failure("API did not become ready")
|
|
|
|
|
|
def main():
|
|
c = Client(BASE_URL, timeout=10)
|
|
alice, bob, _eve, chat = c.bootstrap()
|
|
history = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
|
|
before = int(valkey("DBSIZE"))
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
keys = valkey("--scan", "--pattern", "session:*").split()
|
|
check(len(keys) >= 1 and int(valkey("DBSIZE")) >= before, "session lookups must be cached in Valkey")
|
|
ttl = int(valkey("TTL", keys[0]))
|
|
check(0 < ttl <= 300, "cache entries must expire (TTL %d)" % ttl)
|
|
temp = c.user("tomb")
|
|
c.call("GET", "/api/v1/users/me", actor=temp, schema="User")
|
|
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=temp)
|
|
tombstones = [k for k in valkey("--scan", "--pattern", "session:*").split() if valkey("GET", k) == "revoked"]
|
|
check(tombstones, "logout must leave a 'revoked' tombstone in the cache")
|
|
ok("Valkey caches sessions with TTL<=300s and keeps logout tombstones")
|
|
|
|
try:
|
|
compose("stop", "valkey")
|
|
c.call("GET", "/health/ready", 200)
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
c.message(bob, chat, "без кэша")
|
|
c.call("GET", "/api/v1/users/me", 401, actor=temp)
|
|
start = time.monotonic()
|
|
for _ in range(10):
|
|
c.call("GET", history + "?limit=1", actor=alice, schema="MessagePage")
|
|
per_request = (time.monotonic() - start) / 10
|
|
check(per_request < 0.5, "cache bypass must be fast (%.3fs per request)" % per_request)
|
|
ok("Valkey stopped: ready=200, reads/writes OK, revoked token 401, %.0f ms/request" % (per_request * 1000))
|
|
finally:
|
|
compose("start", "valkey")
|
|
|
|
paused = c.user("pause")
|
|
key = "session:" + hashlib.sha256(paused["token"].encode("ascii")).hexdigest()
|
|
deadline = time.monotonic() + 15
|
|
while not valkey("GET", key).startswith("{"):
|
|
check(time.monotonic() < deadline, "live session must be cached before the failure")
|
|
c.call("GET", "/api/v1/users/me", actor=paused, schema="User")
|
|
time.sleep(0.5)
|
|
try:
|
|
compose("pause", "valkey")
|
|
status = c.call("DELETE", "/api/v1/auth/sessions/current", [204, 503], actor=paused)["status"]
|
|
finally:
|
|
compose("unpause", "valkey")
|
|
check(status == 503, "logout must not report success while the cache still holds the live session (got %d)" % status)
|
|
time.sleep(6)
|
|
c.call("GET", "/api/v1/users/me", 401, actor=paused)
|
|
deadline = time.monotonic() + 10
|
|
while valkey("GET", key) != "revoked":
|
|
check(time.monotonic() < deadline, "tombstone must be written once Valkey is back")
|
|
time.sleep(0.5)
|
|
c.call("GET", "/api/v1/users/me", 401, actor=paused)
|
|
c.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=paused)
|
|
ok("tombstone write failed during logout -> 503, cached session never accepted again, tombstone repaired")
|
|
|
|
fresh = c.user("dbdown")
|
|
try:
|
|
compose("stop", "postgres")
|
|
c.call("GET", "/health/live", 200)
|
|
start = time.monotonic()
|
|
r = c.call("GET", "/health/ready", 503)
|
|
check(r["json"]["error"]["code"] == "unavailable", "readiness must report unavailable")
|
|
c.call("GET", "/api/v1/chats", 503, actor=alice)
|
|
c.call("POST", history, 503, actor=alice, body={"text": "в никуда"})
|
|
c.call("GET", "/api/v1/users/me", 503, actor=fresh)
|
|
c.call("GET", "/api/v1/users/me", 401)
|
|
elapsed = time.monotonic() - start
|
|
check(elapsed < 20, "errors must be bounded in time (%.1fs)" % elapsed)
|
|
ok("PostgreSQL stopped: live=200, ready/business=503 in %.1fs total, unverified token not trusted" % elapsed)
|
|
finally:
|
|
compose("start", "postgres")
|
|
|
|
wait_ready(c)
|
|
c.call("GET", "/api/v1/users/me", actor=fresh, schema="User")
|
|
page = c.call("GET", history + "?limit=100", actor=alice, schema="MessagePage")["json"]
|
|
check(any(m["text"] == "без кэша" for m in page["items"]), "message written without cache must persist")
|
|
ok("PostgreSQL started again: API recovers without restart, data intact")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main_wrapper(main)
|