forked from mtusi2026/mtusi2026lab4
262 lines
9.0 KiB
Python
262 lines
9.0 KiB
Python
"""
|
||
Согласованность S3 и БД при загрузке: любой отказ после записи строки
|
||
не оставляет неучтённых объектов — компенсация сразу или janitor позже.
|
||
"""
|
||
|
||
import asyncio
|
||
import hashlib
|
||
import io
|
||
from collections.abc import AsyncIterator
|
||
from datetime import UTC, datetime
|
||
from uuid import uuid4
|
||
|
||
import pytest
|
||
from fakes import (
|
||
FakeAttachmentRepository,
|
||
FakeChatRepository,
|
||
FakeObjectStorage,
|
||
chunks_of,
|
||
)
|
||
from PIL import Image
|
||
|
||
from domain import (
|
||
AttachmentStatus,
|
||
AttachmentVariant,
|
||
StoredAttachmentSchema,
|
||
UserSchema,
|
||
)
|
||
from domain.errors import (
|
||
InvalidRequestError,
|
||
NotFoundError,
|
||
PayloadTooLargeError,
|
||
UnavailableError,
|
||
UnsupportedMediaTypeError,
|
||
)
|
||
from infrastructure.media import ImageInspector
|
||
from infrastructure.services import AttachmentService, StorageReconciler
|
||
|
||
MAX_BYTES = 1024 * 1024
|
||
GRACE_SECONDS = 900
|
||
USER = UserSchema(
|
||
id=uuid4(), username="alice", display_name="Алиса", created_at=datetime.now(UTC)
|
||
)
|
||
|
||
|
||
def png(width: int = 8, height: int = 8) -> bytes:
|
||
buffer = io.BytesIO()
|
||
Image.new("RGB", (width, height), (1, 2, 3)).save(buffer, "PNG")
|
||
return buffer.getvalue()
|
||
|
||
|
||
class Stand:
|
||
"""Сервис с фейками; inspector настоящий"""
|
||
|
||
def __init__(self, store_timeout: float = 5) -> None:
|
||
self.chats = FakeChatRepository()
|
||
self.attachments = FakeAttachmentRepository()
|
||
self.storage = FakeObjectStorage()
|
||
self.inspector = ImageInspector(workers=1, max_pixels=1_000_000)
|
||
self.service = AttachmentService(
|
||
self.chats,
|
||
self.attachments,
|
||
self.storage,
|
||
self.inspector,
|
||
max_upload_bytes=MAX_BYTES,
|
||
spool_memory_bytes=1024,
|
||
store_timeout_seconds=store_timeout,
|
||
pending_grace_seconds=GRACE_SECONDS,
|
||
)
|
||
|
||
async def upload(self, data: bytes | None = None, content_type: str = "image/png"):
|
||
data = data if data is not None else png()
|
||
return await self.service.upload(
|
||
USER, uuid4(), "a.png", content_type, len(data), chunks_of(data, 100)
|
||
)
|
||
|
||
async def reconcile(self, grace_seconds: int = GRACE_SECONDS):
|
||
reconciler = StorageReconciler(self.attachments, self.storage, grace_seconds)
|
||
return await reconciler.run_once()
|
||
|
||
|
||
def run(coro):
|
||
return asyncio.run(coro)
|
||
|
||
|
||
def test_success_stores_object_and_ready_row() -> None:
|
||
stand = Stand()
|
||
data = png()
|
||
attachment = run(stand.upload(data))
|
||
assert attachment.sha256 == hashlib.sha256(data).hexdigest()
|
||
assert stand.attachments.statuses() == [AttachmentStatus.READY]
|
||
[(stored, _)] = stand.storage.objects.values()
|
||
assert stored == data
|
||
[key] = stand.storage.objects
|
||
assert key.startswith("originals/") and "a.png" not in key
|
||
# Строка записана до PUT, подтверждена после
|
||
assert stand.attachments.calls[:2] == ["create_pending", "mark_ready"]
|
||
|
||
|
||
def test_s3_failure_after_write_is_compensated() -> None:
|
||
"""PUT дошёл до S3, но ответ — ошибка: объект удаляется, строка тоже"""
|
||
stand = Stand()
|
||
stand.storage.fail.add("put")
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert stand.storage.objects == {}
|
||
assert stand.attachments.rows == {}
|
||
|
||
|
||
def test_db_failure_between_s3_and_commit_is_compensated() -> None:
|
||
"""Объект записан в S3, подтверждение в БД не прошло"""
|
||
stand = Stand()
|
||
stand.attachments.fail.add("mark_ready")
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert stand.storage.objects == {}
|
||
assert stand.attachments.rows == {}
|
||
|
||
|
||
def test_db_down_leaves_pending_row_that_janitor_cleans() -> None:
|
||
"""
|
||
БД пропала после PUT: ни подтвердить, ни компенсировать. Объект учтён
|
||
строкой pending; janitor после grace period удаляет и объект, и строку.
|
||
"""
|
||
stand = Stand()
|
||
stand.attachments.fail.update({"mark_ready", "claim_pending"})
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert len(stand.storage.objects) == 1
|
||
assert stand.attachments.statuses() == [AttachmentStatus.PENDING]
|
||
|
||
stand.attachments.fail.clear()
|
||
fresh = run(stand.reconcile(GRACE_SECONDS))
|
||
assert fresh.unfinished_claimed == 0, "свежая загрузка не трогается"
|
||
assert len(stand.storage.objects) == 1
|
||
|
||
report = run(stand.reconcile(grace_seconds=0))
|
||
assert report.unfinished_objects_deleted == report.unfinished_rows_deleted == 1
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
|
||
|
||
def test_s3_down_during_compensation_leaves_deleting_row_for_janitor() -> None:
|
||
stand = Stand()
|
||
stand.attachments.fail.add("mark_ready")
|
||
stand.storage.fail.add("delete")
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert stand.attachments.statuses() == [AttachmentStatus.DELETING]
|
||
assert len(stand.storage.objects) == 1
|
||
|
||
stand.storage.fail.clear()
|
||
# deleting очищается независимо от возраста
|
||
report = run(stand.reconcile(GRACE_SECONDS))
|
||
assert report.unfinished_rows_deleted == 1
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
|
||
|
||
def test_store_timeout_is_compensated() -> None:
|
||
stand = Stand(store_timeout=0.2)
|
||
stand.storage.hang.add("put")
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
|
||
|
||
def test_cancelled_upload_is_compensated() -> None:
|
||
"""Остановка реплики посреди PUT: задача отменяется, компенсация выполняется"""
|
||
stand = Stand()
|
||
stand.storage.hang.add("put")
|
||
|
||
async def scenario() -> None:
|
||
task = asyncio.create_task(stand.upload())
|
||
while "put" not in stand.storage.calls:
|
||
await asyncio.sleep(0.01)
|
||
task.cancel()
|
||
with pytest.raises(asyncio.CancelledError):
|
||
await task
|
||
|
||
run(scenario())
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
|
||
|
||
def test_upload_claimed_by_janitor_is_not_confirmed() -> None:
|
||
stand = Stand()
|
||
stand.attachments.ready_returns_none = True
|
||
with pytest.raises(UnavailableError):
|
||
run(stand.upload())
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
|
||
|
||
def test_validation_errors_store_nothing() -> None:
|
||
stand = Stand()
|
||
cases = [
|
||
(png(), "image/jpeg", UnsupportedMediaTypeError),
|
||
(b"not an image", "image/png", UnsupportedMediaTypeError),
|
||
(png()[:40], "image/png", UnsupportedMediaTypeError),
|
||
(png(2000, 1000), "image/png", PayloadTooLargeError),
|
||
(b"", "image/png", InvalidRequestError),
|
||
]
|
||
for data, content_type, error in cases:
|
||
with pytest.raises(error):
|
||
run(stand.upload(data, content_type))
|
||
assert stand.storage.objects == {} and stand.attachments.rows == {}
|
||
assert "create_pending" not in stand.attachments.calls
|
||
|
||
|
||
def test_declared_length_over_limit_is_rejected_without_reading() -> None:
|
||
stand = Stand()
|
||
consumed = []
|
||
|
||
async def body() -> AsyncIterator[bytes]:
|
||
consumed.append(True)
|
||
yield b"x"
|
||
|
||
with pytest.raises(PayloadTooLargeError):
|
||
run(
|
||
stand.service.upload(
|
||
USER, uuid4(), "a.png", "image/png", MAX_BYTES + 1, body()
|
||
)
|
||
)
|
||
assert consumed == []
|
||
|
||
|
||
def test_streamed_body_over_limit_stops_reading() -> None:
|
||
"""Без Content-Length чтение обрывается на первом байте сверх лимита"""
|
||
stand = Stand()
|
||
chunks_read = 0
|
||
|
||
async def endless() -> AsyncIterator[bytes]:
|
||
nonlocal chunks_read
|
||
while True:
|
||
chunks_read += 1
|
||
yield b"\x00" * 64 * 1024
|
||
|
||
with pytest.raises(PayloadTooLargeError):
|
||
run(stand.service.upload(USER, uuid4(), "a.png", "image/png", None, endless()))
|
||
assert chunks_read == MAX_BYTES // (64 * 1024) + 1
|
||
|
||
|
||
def test_foreign_chat_is_404_before_reading_body() -> None:
|
||
stand = Stand()
|
||
chat_id = uuid4()
|
||
stand.chats.foreign.add(chat_id)
|
||
with pytest.raises(NotFoundError):
|
||
run(stand.service.upload(USER, chat_id, "a.png", "image/png", 10, chunks_of(b"")))
|
||
|
||
|
||
def test_missing_variant_is_404() -> None:
|
||
stand = Stand()
|
||
attachment = run(stand.upload())
|
||
|
||
async def get_for_member(attachment_id, member_id):
|
||
[key] = stand.storage.objects
|
||
return StoredAttachmentSchema(attachment=attachment, object_key=key)
|
||
|
||
stand.attachments.get_for_member = get_for_member
|
||
_, content = run(
|
||
stand.service.open_content(USER, attachment.id, AttachmentVariant.ORIGINAL)
|
||
)
|
||
assert content.content_length == attachment.size_bytes
|
||
with pytest.raises(NotFoundError):
|
||
run(stand.service.open_content(USER, attachment.id, AttachmentVariant.THUMBNAIL))
|