forked from mtusi2026/mtusi2026lab4
129 lines
3.7 KiB
Python
129 lines
3.7 KiB
Python
import base64
|
|
import subprocess
|
|
import time
|
|
import uuid
|
|
|
|
import httpx
|
|
|
|
BASE = "http://localhost:8080"
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
|
|
|
|
|
def _register_and_login(client: httpx.Client) -> dict:
|
|
username = "cch_" + uuid.uuid4().hex[:10]
|
|
password = "Test-password-" + uuid.uuid4().hex[:8]
|
|
r = client.post(
|
|
f"{BASE}/api/v1/users",
|
|
json={
|
|
"username": username,
|
|
"display_name": "Cache Test",
|
|
"password": password,
|
|
},
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
user = r.json()
|
|
credential = base64.b64encode(
|
|
(username + ":" + password).encode("utf-8")
|
|
).decode("ascii")
|
|
r = client.post(
|
|
f"{BASE}/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + credential},
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
return {"id": user["id"], "token": r.json()["token"]}
|
|
|
|
|
|
def _auth(actor: dict) -> dict:
|
|
return {"Authorization": "Bearer " + actor["token"]}
|
|
|
|
|
|
def _auth_idem(actor: dict) -> dict:
|
|
return {
|
|
"Authorization": "Bearer " + actor["token"],
|
|
"Idempotency-Key": uuid.uuid4().hex,
|
|
}
|
|
|
|
|
|
def test_read_after_write_sees_new_message():
|
|
with httpx.Client() as c:
|
|
alice = _register_and_login(c)
|
|
bob = _register_and_login(c)
|
|
chat = c.post(
|
|
f"{BASE}/api/v1/chats",
|
|
json={"title": "raw", "member_ids": [alice["id"], bob["id"]]},
|
|
headers=_auth(alice),
|
|
).json()
|
|
|
|
r = c.get(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50",
|
|
headers=_auth(alice),
|
|
)
|
|
assert r.status_code == 200
|
|
before = r.json()["items"]
|
|
assert len(before) == 0
|
|
|
|
text = "read-after-write-" + uuid.uuid4().hex[:8]
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": text},
|
|
headers=_auth_idem(alice),
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
|
|
r = c.get(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50",
|
|
headers=_auth(alice),
|
|
)
|
|
assert r.status_code == 200
|
|
after = r.json()["items"]
|
|
texts = [m["text"] for m in after]
|
|
assert text in texts, f"read-after-write failed: {texts}"
|
|
|
|
|
|
def test_cache_recovers_after_valkey_restart():
|
|
with httpx.Client() as c:
|
|
alice = _register_and_login(c)
|
|
bob = _register_and_login(c)
|
|
chat = c.post(
|
|
f"{BASE}/api/v1/chats",
|
|
json={"title": "restart", "member_ids": [alice["id"], bob["id"]]},
|
|
headers=_auth(alice),
|
|
).json()
|
|
for i in range(5):
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": f"msg-{i}"},
|
|
headers=_auth_idem(alice),
|
|
)
|
|
assert r.status_code == 201, r.text
|
|
|
|
r = c.get(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50",
|
|
headers=_auth(alice),
|
|
)
|
|
assert r.status_code == 200
|
|
before = r.json()["items"]
|
|
assert len(before) == 5
|
|
|
|
subprocess.run(COMPOSE + ["restart", "valkey"], check=True)
|
|
time.sleep(3)
|
|
|
|
for _ in range(30):
|
|
try:
|
|
with httpx.Client(timeout=2) as c:
|
|
r = c.get(f"{BASE}/health/ready")
|
|
if r.status_code == 200:
|
|
break
|
|
except Exception:
|
|
pass
|
|
time.sleep(1)
|
|
|
|
with httpx.Client() as c:
|
|
r = c.get(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=50",
|
|
headers=_auth(alice),
|
|
)
|
|
assert r.status_code == 200
|
|
after = r.json()["items"]
|
|
assert len(after) == 5
|
|
assert [m["id"] for m in after] == [m["id"] for m in before] |