forked from mtusi2026/mtusi2026lab5
Очереди и асинхронная обработка изображений, трек 1
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import base64
|
||||
import io
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
from PIL import Image
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
||||
|
||||
|
||||
def _user(c):
|
||||
u = "crash_" + uuid.uuid4().hex[:10]
|
||||
p = "Test-password-1234"
|
||||
c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "C", "password": p})
|
||||
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
|
||||
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
|
||||
return {"id": s["user"]["id"], "token": s["token"]}
|
||||
|
||||
|
||||
def _auth(a):
|
||||
return {"Authorization": "Bearer " + a["token"]}
|
||||
|
||||
|
||||
def _png():
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", (320, 200), (0, 128, 255)).save(buf, format="PNG")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def _wait_ready(c, att_id, token, timeout=60):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth({"token": token}))
|
||||
if r.status_code == 200 and r.json()["status"] == "ready":
|
||||
return r.json()
|
||||
time.sleep(0.5)
|
||||
raise AssertionError("attachment did not become ready")
|
||||
|
||||
|
||||
def _delete_variant(chat_id, att_id, name):
|
||||
key = f"{name}s/{chat_id}/{att_id}.jpg"
|
||||
script = "from app.s3 import s3\ns3.remove('" + key + "')\nprint('removed')\n"
|
||||
subprocess.run(
|
||||
COMPOSE + ["exec", "-T", "worker", "python", "-c", script],
|
||||
check=True,
|
||||
)
|
||||
|
||||
|
||||
def test_worker_recovers_after_ack_lost():
|
||||
with httpx.Client(timeout=30) as c:
|
||||
a = _user(c)
|
||||
b = _user(c)
|
||||
chat = c.post(
|
||||
f"{BASE}/api/v1/chats",
|
||||
json={"title": "crash", "member_ids": [a["id"], b["id"]]},
|
||||
headers=_auth(a),
|
||||
).json()
|
||||
data = _png()
|
||||
r = c.post(
|
||||
f"{BASE}/api/v1/chats/{chat['id']}/attachments",
|
||||
params={"filename": "c.png"},
|
||||
content=data,
|
||||
headers={**_auth(a), "Content-Type": "image/png"},
|
||||
)
|
||||
assert r.status_code == 202, r.text
|
||||
att = r.json()
|
||||
att_id = att["id"]
|
||||
|
||||
_wait_ready(c, att_id, a["token"])
|
||||
before = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers=_auth(a)).json()
|
||||
thumb1 = c.get(
|
||||
f"{BASE}/api/v1/attachments/{att_id}/content?variant=thumbnail",
|
||||
headers=_auth(a),
|
||||
)
|
||||
assert thumb1.status_code == 200
|
||||
|
||||
_delete_variant(chat["id"], att_id, "thumbnail")
|
||||
|
||||
subprocess.run(
|
||||
COMPOSE + ["exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c",
|
||||
f"UPDATE attachments SET status='failed', error_code='ack_lost' WHERE id='{att_id}'"],
|
||||
check=True,
|
||||
)
|
||||
|
||||
r = c.post(
|
||||
f"{BASE}/api/v1/attachments/{att_id}/replay",
|
||||
headers=_auth(a),
|
||||
)
|
||||
assert r.status_code == 202, r.text
|
||||
|
||||
ready = _wait_ready(c, att_id, a["token"])
|
||||
thumb2 = c.get(
|
||||
f"{BASE}/api/v1/attachments/{att_id}/content?variant=thumbnail",
|
||||
headers=_auth(a),
|
||||
)
|
||||
assert thumb2.status_code == 200
|
||||
assert ready["variants"] == before["variants"], "variants changed after replay"
|
||||
Reference in New Issue
Block a user