test: update concurrent test with idempotency key for lab3
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""10 клиентов × 10 сообщений = 100 уникальных."""
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
@@ -11,11 +10,14 @@ from concurrent.futures import ThreadPoolExecutor
|
|||||||
BASE = "http://127.0.0.1:8080"
|
BASE = "http://127.0.0.1:8080"
|
||||||
PASSWORD = "Example-only-not-a-real-secret-123"
|
PASSWORD = "Example-only-not-a-real-secret-123"
|
||||||
|
|
||||||
|
|
||||||
def call(method, path, body=None, token=None):
|
def call(method, path, body=None, token=None):
|
||||||
h = {"Accept": "application/json"}
|
h = {"Accept": "application/json"}
|
||||||
if token:
|
if token:
|
||||||
h["Authorization"] = f"Bearer {token}"
|
h["Authorization"] = f"Bearer {token}"
|
||||||
|
# ДОБАВЛЯЕМ IDEMPOTENCY-KEY ДЛЯ POST
|
||||||
|
if method == "POST" and "/messages" in path:
|
||||||
|
h["Idempotency-Key"] = uuid.uuid4().hex
|
||||||
|
|
||||||
data = None
|
data = None
|
||||||
if body is not None:
|
if body is not None:
|
||||||
data = json.dumps(body).encode("utf-8")
|
data = json.dumps(body).encode("utf-8")
|
||||||
@@ -29,7 +31,6 @@ def call(method, path, body=None, token=None):
|
|||||||
payload = e.read()
|
payload = e.read()
|
||||||
return e.code, (json.loads(payload) if payload else None)
|
return e.code, (json.loads(payload) if payload else None)
|
||||||
|
|
||||||
|
|
||||||
def create_user_and_login(name):
|
def create_user_and_login(name):
|
||||||
s, u = call("POST", "/api/v1/users",
|
s, u = call("POST", "/api/v1/users",
|
||||||
{"username": name, "display_name": name, "password": PASSWORD})
|
{"username": name, "display_name": name, "password": PASSWORD})
|
||||||
@@ -44,7 +45,6 @@ def create_user_and_login(name):
|
|||||||
session = json.loads(r.read())
|
session = json.loads(r.read())
|
||||||
return u, session["token"]
|
return u, session["token"]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
users = [create_user_and_login(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)]
|
users = [create_user_and_login(f"conc_{i}_{uuid.uuid4().hex[:6]}") for i in range(10)]
|
||||||
owner, owner_token = users[0]
|
owner, owner_token = users[0]
|
||||||
@@ -86,6 +86,5 @@ def main():
|
|||||||
assert len(seen) == 100, f"expected 100, got {len(seen)}"
|
assert len(seen) == 100, f"expected 100, got {len(seen)}"
|
||||||
print("PASS: no lost or duplicated messages")
|
print("PASS: no lost or duplicated messages")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user