add code for level 4
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверки гонки регистрации и rollback создания чата."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, check, run # noqa: E402
|
||||
|
||||
|
||||
def main() -> None:
|
||||
client = Client()
|
||||
alice, bob, _eve, chat = client.bootstrap()
|
||||
|
||||
username = "race_" + uuid.uuid4().hex[:12]
|
||||
body = {
|
||||
"username": username,
|
||||
"display_name": "Гонка регистрации",
|
||||
"password": "Race-password-" + uuid.uuid4().hex,
|
||||
}
|
||||
|
||||
def register_once() -> int:
|
||||
local = Client()
|
||||
return local.call("POST", "/api/v1/users", expected=[201, 409], body=body)["status"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
statuses = list(pool.map(lambda _unused: register_once(), range(8)))
|
||||
check(statuses.count(201) == 1 and statuses.count(409) == 7, "username race was not serialized")
|
||||
|
||||
title = "rollback-" + uuid.uuid4().hex
|
||||
client.call(
|
||||
"POST",
|
||||
"/api/v1/chats",
|
||||
400,
|
||||
actor=alice,
|
||||
body={"title": title, "member_ids": [alice["id"], str(uuid.uuid4())]},
|
||||
)
|
||||
chats = client.call(
|
||||
"GET", "/api/v1/chats?limit=100", actor=alice, schema="ChatPage"
|
||||
)["json"]["items"]
|
||||
check(all(chat["title"] != title for chat in chats), "failed chat creation left a chat")
|
||||
|
||||
first = client.message(alice, chat, "before cached read")["json"]
|
||||
client.call(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
||||
actor=alice,
|
||||
schema="MessagePage",
|
||||
)
|
||||
second = client.message(bob, chat, "after cached read")["json"]
|
||||
history = client.call(
|
||||
"GET",
|
||||
"/api/v1/chats/" + chat["id"] + "/messages?limit=100",
|
||||
actor=alice,
|
||||
schema="MessagePage",
|
||||
)["json"]["items"]
|
||||
check(
|
||||
{message["id"] for message in history} == {first["id"], second["id"]},
|
||||
"history cache returned stale data after write",
|
||||
)
|
||||
print("PASS: registration race, chat rollback and cache read-after-write")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка 503 readiness при остановленном PostgreSQL."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, run # noqa: E402
|
||||
|
||||
|
||||
def main() -> None:
|
||||
Client().call("GET", "/health/ready", 503)
|
||||
print("PASS: readiness rejects unavailable PostgreSQL")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка expiry; запускается при SESSION_TTL_SECONDS=2."""
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, check, run # noqa: E402
|
||||
|
||||
|
||||
def main() -> None:
|
||||
client = Client()
|
||||
actor = client.user("expiry")
|
||||
time.sleep(3)
|
||||
client.call("GET", "/api/v1/users/me", 401, actor=actor)
|
||||
print("PASS: expired Bearer session was rejected")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user