This commit is contained in:
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Level-4 race-protection evidence: 10 clients send 100 messages concurrently;
|
||||
expect exactly 100 unique messages, with no loss/duplication under pagination.
|
||||
"""
|
||||
import sys
|
||||
import urllib.parse
|
||||
import concurrent.futures
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, check # noqa: E402
|
||||
|
||||
|
||||
def main():
|
||||
base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
|
||||
n_messages = 100
|
||||
n_clients = 10
|
||||
|
||||
c = Client(base_url)
|
||||
users = [c.user("race%d" % i) for i in range(n_clients)]
|
||||
chat = c.call(
|
||||
"POST", "/api/v1/chats", 201, actor=users[0],
|
||||
body={"title": "race", "member_ids": [u["id"] for u in users]},
|
||||
schema="Chat",
|
||||
)["json"]
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
|
||||
def send(i):
|
||||
actor = users[i % n_clients]
|
||||
return c.message(actor, chat, "race-%d" % i)["json"]["id"]
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=n_clients) as pool:
|
||||
ids = list(pool.map(send, range(n_messages)))
|
||||
|
||||
check(len(ids) == n_messages, "expected %d responses" % n_messages)
|
||||
check(len(set(ids)) == n_messages, "expected %d unique message ids, got %d" % (n_messages, len(set(ids))))
|
||||
|
||||
# Walk the full history back through the API to confirm the store itself
|
||||
# (not just the HTTP responses) holds exactly n_messages distinct rows.
|
||||
seen = []
|
||||
cursor = None
|
||||
while True:
|
||||
qs = "?limit=25" + ("&cursor=" + urllib.parse.quote(cursor, safe="") if cursor else "")
|
||||
page = c.call("GET", path + qs, actor=users[0], schema="MessagePage")["json"]
|
||||
seen.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if not cursor:
|
||||
break
|
||||
check(len(seen) == n_messages, "expected to read back %d messages, got %d" % (n_messages, len(seen)))
|
||||
check(len({m["id"] for m in seen}) == n_messages, "readback contains duplicates")
|
||||
|
||||
print("PASS: %d unique messages written and read back from %d concurrent clients" % (n_messages, n_clients))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except AssertionError as exc:
|
||||
print("FAIL:", exc)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user