forked from mtusi2026/mtusi2026lab2
Alembic migrations и Docker Compose с postgres и redis
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
"""10 клиентов × 10 сообщений = 100 уникальных. Запуск: python tests/concurrent_test.py"""
|
||||
"""10 клиентов × 10 сообщений = 100 уникальных."""
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
BASE = "http://127.0.0.1:8080"
|
||||
PASSWORD = "Example-only-not-a-real-secret-123"
|
||||
|
||||
|
||||
def call(method, path, body=None, actor=None):
|
||||
def call(method, path, body=None, token=None):
|
||||
h = {"Accept": "application/json"}
|
||||
if actor:
|
||||
h["X-User-Id"] = actor
|
||||
if token:
|
||||
h["Authorization"] = f"Bearer {token}"
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
@@ -27,29 +30,35 @@ def call(method, path, body=None, actor=None):
|
||||
return e.code, (json.loads(payload) if payload else None)
|
||||
|
||||
|
||||
def create_user(name):
|
||||
s, u = call("POST", "/api/v1/users", {"username": name, "display_name": name})
|
||||
def create_user_and_login(name):
|
||||
s, u = call("POST", "/api/v1/users",
|
||||
{"username": name, "display_name": name, "password": PASSWORD})
|
||||
assert s == 201, f"create_user {name}: {s} {u}"
|
||||
return u
|
||||
cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode()
|
||||
req = urllib.request.Request(
|
||||
BASE + "/api/v1/auth/sessions",
|
||||
headers={"Authorization": f"Basic {cred}"},
|
||||
method="POST",
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
session = json.loads(r.read())
|
||||
return u, session["token"]
|
||||
|
||||
|
||||
def main():
|
||||
users = [create_user(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)]
|
||||
s, chat = call(
|
||||
"POST", "/api/v1/chats",
|
||||
{"title": "concurrent", "member_ids": [u["id"] for u in users]},
|
||||
actor=users[0]["id"],
|
||||
)
|
||||
assert s == 201, f"chat: {s} {chat}"
|
||||
users = [create_user_and_login(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)]
|
||||
owner, owner_token = users[0]
|
||||
chat = call("POST", "/api/v1/chats",
|
||||
{"title": "concurrent", "member_ids": [u["id"] for u, _ in users]},
|
||||
token=owner_token)[1]
|
||||
cid = chat["id"]
|
||||
|
||||
def post_10(user):
|
||||
def post_10(user_and_token):
|
||||
_, token = user_and_token
|
||||
results = []
|
||||
for k in range(10):
|
||||
s, m = call(
|
||||
"POST", f"/api/v1/chats/{cid}/messages",
|
||||
{"text": f"{user['username']}-{k}"}, actor=user["id"],
|
||||
)
|
||||
s, m = call("POST", f"/api/v1/chats/{cid}/messages",
|
||||
{"text": f"m{k}"}, token=token)
|
||||
results.append((s, m))
|
||||
return results
|
||||
|
||||
@@ -66,8 +75,7 @@ def main():
|
||||
path = f"/api/v1/chats/{cid}/messages?limit=100"
|
||||
if cursor:
|
||||
path += f"&cursor={cursor}"
|
||||
s, page = call("GET", path, actor=users[0]["id"])
|
||||
assert s == 200
|
||||
s, page = call("GET", path, token=owner_token)
|
||||
for m in page["items"]:
|
||||
seen.add(m["id"])
|
||||
cursor = page["next_cursor"]
|
||||
|
||||
Reference in New Issue
Block a user