47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Конкурентная запись: 10 клиентов параллельно отправляют по 10 сообщений.
|
|
Ожидаем ровно 100 уникальных сообщений в истории.
|
|
python student-tests/race.py --base-url http://localhost:8080
|
|
"""
|
|
import argparse
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from common import Api
|
|
|
|
CLIENTS, PER_CLIENT = 10, 10
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--base-url", default="http://localhost:8080")
|
|
api = Api(ap.parse_args().base_url)
|
|
user = api.new_user("race")
|
|
st, chat, _ = api.call("POST", "/api/v1/chats", {"title": "race", "member_ids": [user["id"]]}, token=user["token"])
|
|
mpath = f"/api/v1/chats/{chat['id']}/messages"
|
|
|
|
def worker(w):
|
|
return [api.call("POST", mpath, {"text": f"c{w}-m{i}"}, token=user["token"])[0] for i in range(PER_CLIENT)]
|
|
|
|
with ThreadPoolExecutor(max_workers=CLIENTS) as pool:
|
|
codes = [c for batch in pool.map(worker, range(CLIENTS)) for c in batch]
|
|
|
|
ids, texts, cursor = [], [], None
|
|
while True:
|
|
st, page, _ = api.call("GET", f"{mpath}?limit=50" + (f"&cursor={cursor}" if cursor else ""), token=user["token"])
|
|
ids += [m["id"] for m in page["items"]]
|
|
texts += [m["text"] for m in page["items"]]
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
expected = {f"c{w}-m{i}" for w in range(CLIENTS) for i in range(PER_CLIENT)}
|
|
ok = codes.count(201) == 100 and len(ids) == 100 and len(set(ids)) == 100 and set(texts) == expected
|
|
print(f"sent=100 accepted={codes.count(201)} received={len(ids)} unique_ids={len(set(ids))} "
|
|
f"all_texts_present={set(texts) == expected}")
|
|
print("PASS" if ok else "FAIL")
|
|
raise SystemExit(0 if ok else 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|