#!/usr/bin/env python3 """Student checks for grades 4 and 5 (track 1: cache consistency).""" import base64 import json import os import sys import threading import time import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure, check, run # noqa: E402 def _basic(username, password): return "Basic " + base64.b64encode((username + ":" + password).encode()).decode() def lab_metrics(c: Client): return json.loads(c.call("GET", "/api/v1/_lab/metrics")["bytes"].decode()) def lab_flags(c: Client, body): return json.loads(c.call("POST", "/api/v1/_lab/flags", 200, body=body)["bytes"].decode()) def test_wrong_password_and_x_user_id(c: Client): actor = c.user("neg") bad = _basic(actor["registration"]["username"], "wrong-password-xx") c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": bad}) c.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": actor["id"]}) c.call("GET", "/api/v1/users/me", 401) def test_logout(c: Client): actor = c.user("out") c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor) c.call("GET", "/api/v1/users/me", 401, actor=actor) def test_register_race(c: Client): username = "r_" + uuid.uuid4().hex[:12] body = { "username": username, "display_name": "Race", "password": "Example-only-not-a-real-secret-123", } results = [] def once(): local = Client(c.base) try: results.append(local.call("POST", "/api/v1/users", [201, 409], body=body)["status"]) except Failure as exc: results.append(str(exc)) threads = [threading.Thread(target=once) for _ in range(16)] for t in threads: t.start() for t in threads: t.join() created = results.count(201) conflicted = results.count(409) check(created == 1, "username race: expected exactly one 201, got %s" % results) check(conflicted == 15, "username race: expected fifteen 409, got %s" % results) def test_chat_rollback(c: Client): alice = c.user("rb") before = c.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"] missing = str(uuid.uuid4()) c.call( "POST", "/api/v1/chats", 400, actor=alice, body={"title": "rollback-should-not-exist", "member_ids": [alice["id"], missing]}, ) after = c.call("GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage")["json"]["items"] check(len(after) == len(before), "composite chat create leaked a row after rollback") check(all(x["title"] != "rollback-should-not-exist" for x in after), "rolled-back title became visible") def test_read_after_write(c: Client): alice, bob, _eve, chat = c.bootstrap() path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100" lab_flags(c, {"invalidate": True, "singleflight": True, "reset_sql": True}) first = c.message(alice, chat, "raw-visible-" + uuid.uuid4().hex)["json"] page = c.call("GET", path, actor=bob, schema="MessagePage")["json"] check(any(m["id"] == first["id"] for m in page["items"]), "read-after-write lost the new message") def test_stale_without_invalidation_then_fixed(c: Client): alice, bob, _eve, chat = c.bootstrap() path = "/api/v1/chats/" + chat["id"] + "/messages?limit=50" c.call("GET", path, actor=alice, schema="MessagePage") lab_flags(c, {"invalidate": False, "singleflight": True}) stale_text = "stale-probe-" + uuid.uuid4().hex c.message(alice, chat, stale_text) page = c.call("GET", path, actor=bob, schema="MessagePage")["json"] stale = all(m["text"] != stale_text for m in page["items"]) check(stale, "expected a stale cache read when invalidation is disabled") lab_flags(c, {"invalidate": True}) fresh_text = "fresh-probe-" + uuid.uuid4().hex c.message(alice, chat, fresh_text) page2 = c.call("GET", path, actor=bob, schema="MessagePage")["json"] check(any(m["text"] == fresh_text for m in page2["items"]), "invalidation did not restore read-after-write") def test_stampede(c: Client): alice, _bob, _eve, chat = c.bootstrap() c.message(alice, chat, "stampede-seed") path = "/api/v1/chats/" + chat["id"] + "/messages?limit=50" def burst(n=24): barrier = threading.Barrier(n) def one(_i): local = Client(c.base) barrier.wait() return local.call("GET", path, actor=alice, schema="MessagePage") with ThreadPoolExecutor(max_workers=n) as pool: list(pool.map(one, range(n))) lab_flags(c, {"singleflight": False, "invalidate": True, "reset_sql": True}) c.message(alice, chat, "miss-" + uuid.uuid4().hex) lab_flags(c, {"reset_sql": True, "singleflight": False}) burst() unprotected = lab_metrics(c)["history_sql"] lab_flags(c, {"singleflight": True, "reset_sql": True}) c.message(alice, chat, "miss2-" + uuid.uuid4().hex) lab_flags(c, {"reset_sql": True, "singleflight": True}) burst() protected = lab_metrics(c)["history_sql"] print(json.dumps({"stampede_sql_without_singleflight": unprotected, "stampede_sql_with_singleflight": protected}, ensure_ascii=False)) check(unprotected >= 8, "unprotected stampede did not generate enough SQL") check(protected < unprotected, "singleflight did not reduce SQL during stampede") check(protected <= 4, "singleflight still issued too many history SQL queries") def test_expiry(c: Client): ttl = int(os.environ.get("SESSION_TTL_SECONDS", "3600")) check(ttl <= 3, "expiry test requires SESSION_TTL_SECONDS<=3 (recreate api with short TTL)") actor = c.user("ttl") c.call("GET", "/api/v1/users/me", actor=actor, schema="User") time.sleep(ttl + 1.5) c.call("GET", "/api/v1/users/me", 401, actor=actor) def main(): base = os.environ.get("BASE_URL", "http://localhost:8080") c = Client(base) c.call("GET", "/health/live", schema="Health") c.call("GET", "/health/ready", schema="Health") tests = [ test_wrong_password_and_x_user_id, test_logout, test_register_race, test_chat_rollback, test_read_after_write, test_stale_without_invalidation_then_fixed, test_stampede, ] if os.environ.get("RUN_EXPIRY") == "1": tests.append(test_expiry) try: for fn in tests: fn(c) print("PASS", fn.__name__) finally: try: lab_flags(c, {"invalidate": True, "singleflight": True}) except Exception: pass print("PASS: student grade 4/5 HTTP checks") if __name__ == "__main__": run(main)