"""Additional lab2 API cases, using the unchanged teacher client.""" from concurrent.futures import ThreadPoolExecutor import uuid from urllib.parse import quote from client import check, timestamp def room(c, actor, members=None, title="Lab2 business fixture"): return c.call("POST", "/api/v1/chats", 201, actor=actor, body={"title": title, "member_ids": members or [actor["id"]]}, schema="Chat")["json"] def history(c, actor, chat, limit=100): path = "/api/v1/chats/" + chat["id"] + "/messages?limit=" + str(limit) items, cursors = [], set() for _ in range(2000): page = c.call("GET", path, actor=actor, schema="MessagePage")["json"] items.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: check(len({x["id"] for x in items}) == len(items), "history duplicates message IDs") return items check(cursor not in cursors, "cursor cycle") cursors.add(cursor) path = "/api/v1/chats/" + chat["id"] + "/messages?limit=" + str(limit) + "&cursor=" + quote(cursor, safe="") raise AssertionError("history traversal exceeded test bound") def boundaries(c): actor, other = c.user("bounds"), c.user("other") for title in ("", " \t\n", "😀" * 101): c.call("POST", "/api/v1/chats", 400, actor=actor, body={"title": title, "member_ids": [actor["id"]]}) for members in ([], [other["id"]], [actor["id"], actor["id"]], [actor["id"], str(uuid.uuid4())], [actor["id"], "invalid"]): c.call("POST", "/api/v1/chats", 400, actor=actor, body={"title": "Invalid members", "member_ids": members}) chat = room(c, actor, title="😀" * 100) path = "/api/v1/chats/" + chat["id"] + "/messages" for text in ("", " \t\n", "😀" * 4001): c.call("POST", path, 400, actor=actor, body={"text": text}) message = c.message(actor, chat, "😀" * 4000)["json"] check(message["text"] == "😀" * 4000, "Unicode message boundary changed") c.call("POST", path, 400, actor=actor, body={"text": "forged", "sender_id": other["id"]}) for limit in ("0", "101", "-1", "abc", ""): c.call("GET", path + "?limit=" + limit, 400, actor=actor) for raw in (b'{"text":"x","extra":1}', b'{"Text":"x"}', b'{"text":null}', b'{"text":1}', b'{"text":"x"} {}', b'{"text":"\xff"}'): c.call("POST", path, 400, actor=actor, raw=raw, headers={"Content-Type": "application/json"}) def cursors(c): actor, outsider = c.user("cursor"), c.user("outside") first, second = room(c, actor), room(c, actor) empty = c.call("GET", "/api/v1/chats/" + second["id"] + "/messages", actor=actor, schema="MessagePage")["json"] check(empty == {"items": [], "next_cursor": None}, "empty history is not an empty finished page") sent = [c.message(actor, first, "Page " + str(i))["json"] for i in range(7)] path = "/api/v1/chats/" + first["id"] + "/messages" page = c.call("GET", path + "?limit=1", actor=actor, schema="MessagePage")["json"] cursor = quote(page["next_cursor"], safe="") rest = c.call("GET", path + "?limit=100&cursor=" + cursor, actor=actor, schema="MessagePage")["json"] ordered = sorted(sent, key=lambda x: (timestamp(x["created_at"]), uuid.UUID(x["id"]).int)) check(page["items"] + rest["items"] == ordered and rest["next_cursor"] is None, "changing limit lost/repeated/reordered messages") c.call("GET", "/api/v1/chats/" + second["id"] + "/messages?cursor=" + cursor, 400, actor=actor) for value in ("", "not-a-cursor"): c.call("GET", path + "?cursor=" + value, 400, actor=actor) c.call("GET", path, 404, actor=outsider) c.call("POST", path, 404, actor=outsider, body={"text": "hidden"}) check(history(c, actor, first, limit=2) == ordered, "complete keyset traversal differs") def read_after_write(c, client_factory, concurrency=4): actor = c.user("concurrent") chat = room(c, actor) path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100" # Warm the empty-history cache before starting concurrent writes. c.call("GET", path, actor=actor, schema="MessagePage") def write_then_read(index): local = client_factory() confirmed = local.message(actor, chat, "Read-after-write " + str(index))["json"] page = local.call("GET", path, actor=actor, schema="MessagePage")["json"] check(any(m == confirmed for m in page["items"]), "confirmed message absent from next read") return confirmed with ThreadPoolExecutor(max_workers=concurrency) as pool: messages = list(pool.map(write_then_read, range(min(32, concurrency * 4)))) check({m["id"] for m in history(c, actor, chat)} == {m["id"] for m in messages}, "concurrent writes lost or duplicated confirmations")