forked from mtusi2026/mtusi2026lab5
155 lines
4.1 KiB
Python
155 lines
4.1 KiB
Python
|
|
import asyncio
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
|
|
def test_worker_retry():
|
|
result = subprocess.run(
|
|
[
|
|
"docker", "compose", "exec",
|
|
"-T", "-w", "/app",
|
|
"worker", "python", "-"
|
|
],
|
|
input=Path(__file__).read_text(encoding="utf-8"),
|
|
text=True,
|
|
capture_output=True,
|
|
timeout=60,
|
|
)
|
|
|
|
print(result.stdout, end="")
|
|
|
|
if result.stderr:
|
|
print(result.stderr, file=sys.stderr)
|
|
|
|
assert result.returncode == 0, "Проверка Worker не прошла"
|
|
|
|
|
|
async def main():
|
|
import aio_pika
|
|
from sqlalchemy import select
|
|
|
|
from app.worker import (
|
|
async_session_maker,
|
|
engine,
|
|
RABBITMQ_URL,
|
|
QUEUE_NAME,
|
|
)
|
|
from app.models.attachment import Attachment
|
|
|
|
try:
|
|
async with async_session_maker() as session:
|
|
example = (
|
|
await session.execute(
|
|
select(Attachment).limit(1)
|
|
)
|
|
).scalars().first()
|
|
|
|
if example is None:
|
|
print(
|
|
"FAIL: нет вложений. Сначала запусти make test."
|
|
)
|
|
return False
|
|
|
|
test_id = uuid.uuid4()
|
|
|
|
test_att = Attachment(
|
|
id=test_id,
|
|
object_key=f"retry-test-{test_id}.jpg",
|
|
filename="retry-test.jpg",
|
|
mime_type="image/jpeg",
|
|
size_bytes=1,
|
|
sha256="0" * 64,
|
|
chat_id=example.chat_id,
|
|
user_id=example.user_id,
|
|
status="queued",
|
|
)
|
|
|
|
session.add(test_att)
|
|
await session.commit()
|
|
|
|
print(f"Тестовое вложение: {test_id}", flush=True)
|
|
|
|
connection = await aio_pika.connect_robust(
|
|
RABBITMQ_URL
|
|
)
|
|
|
|
async with connection:
|
|
channel = await connection.channel(
|
|
publisher_confirms=True
|
|
)
|
|
|
|
await channel.declare_queue(
|
|
QUEUE_NAME,
|
|
durable=True
|
|
)
|
|
|
|
await channel.default_exchange.publish(
|
|
aio_pika.Message(
|
|
body=json.dumps({
|
|
"attachment_id": str(test_id)
|
|
}).encode(),
|
|
delivery_mode=(
|
|
aio_pika.DeliveryMode.PERSISTENT
|
|
),
|
|
),
|
|
routing_key=QUEUE_NAME,
|
|
)
|
|
|
|
success = False
|
|
|
|
for _ in range(60):
|
|
await asyncio.sleep(0.5)
|
|
|
|
async with async_session_maker() as session:
|
|
att = await session.get(
|
|
Attachment,
|
|
test_id
|
|
)
|
|
|
|
if att and att.status == "failed":
|
|
print("Финальный статус:", att.status)
|
|
print(
|
|
"Безопасный error_code:",
|
|
att.error_code
|
|
)
|
|
|
|
success = (
|
|
att.error_code == "s3_download_error"
|
|
)
|
|
break
|
|
|
|
if success:
|
|
async with async_session_maker() as session:
|
|
att = await session.get(
|
|
Attachment,
|
|
test_id
|
|
)
|
|
|
|
if att:
|
|
await session.delete(att)
|
|
await session.commit()
|
|
|
|
print("Тестовая запись удалена.")
|
|
print("PASS: failed и безопасный error_code")
|
|
else:
|
|
print("FAIL: ожидаемый результат не получен.")
|
|
print(
|
|
"Тестовая запись оставлена для диагностики."
|
|
)
|
|
|
|
print(f"ID для проверки логов: {test_id}")
|
|
|
|
return success
|
|
|
|
finally:
|
|
await engine.dispose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = asyncio.run(main())
|
|
sys.exit(0 if success else 1)
|