Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e. Contracts, public tests and evidence of lab 5 are not carried over. The compose project, image and CI prefix are renamed to lab6; the first broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the OpenSpec changes of lab 6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
402 lines
12 KiB
Python
402 lines
12 KiB
Python
"""`AttachmentCleanupService`: два блока уборки, сигнал, очистка staging."""
|
|
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
|
|
from builders import NOW, at, attachment_dto
|
|
from fakes import FakeDatabase, FakeObjectStorage, FakeUnitOfWork
|
|
from src.core import exceptions
|
|
from src.core.repos import attachment_object
|
|
from src.core.services.attachment_cleanup import (
|
|
INTERRUPTED_BY_DATABASE,
|
|
INTERRUPTED_BY_OBJECT_STORAGE,
|
|
INTERRUPTED_BY_STOP,
|
|
AttachmentCleanupService,
|
|
)
|
|
from src.schemas import dataclasses
|
|
from src.schemas.enums import (
|
|
AttachmentErrorCode,
|
|
AttachmentStatus,
|
|
AttachmentVariantName,
|
|
)
|
|
from src.utils.constants import ATTACHMENT_PIPELINE_VERSION
|
|
|
|
pytestmark = pytest.mark.anyio
|
|
|
|
|
|
@pytest.fixture
|
|
def service(
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
) -> AttachmentCleanupService:
|
|
return AttachmentCleanupService(uow, storage)
|
|
|
|
|
|
async def _stale(
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
*,
|
|
seconds: float = -1000,
|
|
staging: bool = True,
|
|
) -> dataclasses.AttachmentFullDTO:
|
|
row = attachment_dto(
|
|
chat_id=uuid4(),
|
|
owner_id=uuid4(),
|
|
created_at=at(seconds),
|
|
staging=staging,
|
|
)
|
|
async with uow:
|
|
await uow.attachments.insert_attachment(attachment=row)
|
|
await uow.commit()
|
|
storage.seed(row.staging_key or row.object_key)
|
|
return row
|
|
|
|
|
|
async def _clean(
|
|
service: AttachmentCleanupService,
|
|
*,
|
|
batch_size: int = 10,
|
|
stop_requested=None,
|
|
) -> dataclasses.CleanupPassDTO:
|
|
kwargs = (
|
|
{} if stop_requested is None else {"stop_requested": stop_requested}
|
|
)
|
|
return await service.clean_stale_pending(
|
|
older_than=NOW,
|
|
batch_size=batch_size,
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
async def test_two_blocks_object_before_row(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = await _stale(uow, storage)
|
|
database.events.clear()
|
|
result = await _clean(service)
|
|
assert result.removed == 1
|
|
assert database.events == [
|
|
"enter",
|
|
"attachments.get_stale_pending",
|
|
"exit",
|
|
"enter",
|
|
"attachments.lock_stale_pending",
|
|
"attachments.mark_removing",
|
|
"commit",
|
|
"exit",
|
|
"enter",
|
|
"attachments.lock_stale_pending",
|
|
"storage.delete",
|
|
"attachments.delete_attachment",
|
|
"commit",
|
|
"exit",
|
|
]
|
|
assert database.attachments == {}
|
|
assert row.staging_key not in storage.objects
|
|
|
|
|
|
async def test_old_version_row_deletes_final_object(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = await _stale(uow, storage, staging=False)
|
|
await _clean(service)
|
|
assert row.object_key not in storage.objects
|
|
assert database.attachments == {}
|
|
|
|
|
|
async def test_storage_failure_keeps_mark_and_row(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
"""Сценарий «Порядок уборки»: пометка зафиксирована, второй блок откачен."""
|
|
row = await _stale(uow, storage)
|
|
database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable()
|
|
result = await _clean(service)
|
|
assert result.interrupted_by == INTERRUPTED_BY_OBJECT_STORAGE
|
|
assert result.removed == 0
|
|
kept = database.attachments[row.id]
|
|
assert kept.updated_at > kept.created_at
|
|
assert "attachments.delete_attachment" not in database.events
|
|
# следующий проход убирает
|
|
database.fail_on.clear()
|
|
assert (await _clean(service)).removed == 1
|
|
|
|
|
|
async def test_row_delete_failure_rolls_back_second_block(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = await _stale(uow, storage)
|
|
database.fail_on["attachments.delete_attachment"] = (
|
|
exceptions.ServiceUnavailable()
|
|
)
|
|
result = await _clean(service)
|
|
assert result.interrupted_by == INTERRUPTED_BY_DATABASE
|
|
# объект удалён раньше строки: строка без объекта, а не наоборот
|
|
assert row.staging_key not in storage.objects
|
|
assert row.id in database.attachments
|
|
|
|
|
|
async def test_database_failure_on_candidates(
|
|
service: AttachmentCleanupService,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
database.fail_on["attachments.get_stale_pending"] = (
|
|
exceptions.ServiceUnavailable()
|
|
)
|
|
assert (await _clean(service)).interrupted_by == INTERRUPTED_BY_DATABASE
|
|
|
|
|
|
async def test_held_row_skipped(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = await _stale(uow, storage)
|
|
database.held.add(row.id)
|
|
result = await _clean(service)
|
|
assert (result.removed, result.skipped) == (0, 1)
|
|
assert row.staging_key in storage.objects
|
|
|
|
|
|
async def test_keyset_more_than_one_batch(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
for index in range(5):
|
|
await _stale(uow, storage, seconds=-1000 + index)
|
|
result = await _clean(service, batch_size=2)
|
|
assert result.removed == 5
|
|
assert database.attachments == {}
|
|
|
|
|
|
async def test_stop_before_row(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
await _stale(uow, storage)
|
|
result = await _clean(service, stop_requested=lambda: True)
|
|
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
|
assert "attachments.lock_stale_pending" not in database.events
|
|
|
|
|
|
async def test_stop_between_steps_keeps_mark(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
"""Сигнал между шагами: первый блок зафиксирован, второго нет."""
|
|
row = await _stale(uow, storage)
|
|
calls = 0
|
|
|
|
def stop_requested() -> bool:
|
|
nonlocal calls
|
|
calls += 1
|
|
return calls > 1
|
|
|
|
database.events.clear()
|
|
result = await _clean(service, stop_requested=stop_requested)
|
|
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
|
assert database.events.count("attachments.lock_stale_pending") == 1
|
|
assert database.attachments[row.id].updated_at > row.created_at
|
|
assert row.staging_key in storage.objects
|
|
|
|
|
|
async def _for_staging(
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
*,
|
|
status: AttachmentStatus,
|
|
updated_seconds: float = -1000,
|
|
object_key: str | None = None,
|
|
) -> dataclasses.AttachmentFullDTO:
|
|
row = attachment_dto(
|
|
chat_id=uuid4(),
|
|
owner_id=uuid4(),
|
|
status=AttachmentStatus.QUEUED
|
|
if status == AttachmentStatus.FAILED
|
|
else status,
|
|
created_at=at(updated_seconds),
|
|
object_key=object_key,
|
|
)
|
|
async with uow:
|
|
await uow.attachments.insert_attachment(attachment=row)
|
|
if status == AttachmentStatus.FAILED:
|
|
await uow.attachments.commit_failed(
|
|
attachment_id=row.id,
|
|
error_code=AttachmentErrorCode.INVALID_IMAGE,
|
|
updated_at=at(updated_seconds),
|
|
)
|
|
await uow.commit()
|
|
storage.seed(row.staging_key)
|
|
return row
|
|
|
|
|
|
async def _staging_pass(
|
|
service: AttachmentCleanupService,
|
|
*,
|
|
batch_size: int = 10,
|
|
stop_requested=None,
|
|
) -> dataclasses.StagingCleanupPassDTO:
|
|
kwargs = (
|
|
{} if stop_requested is None else {"stop_requested": stop_requested}
|
|
)
|
|
return await service.clean_staging(
|
|
retention_before=NOW,
|
|
batch_size=batch_size,
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
async def test_ready_staging_removed_at_next_pass(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
# у ready срока нет: даже свежая строка
|
|
row = await _for_staging(
|
|
uow,
|
|
storage,
|
|
status=AttachmentStatus.READY,
|
|
updated_seconds=100,
|
|
)
|
|
storage.seed(row.object_key)
|
|
database.events.clear()
|
|
result = await _staging_pass(service)
|
|
assert result.removed_ready == 1
|
|
assert row.staging_key not in storage.objects
|
|
# конечный оригинал ready не трогается
|
|
assert row.object_key in storage.objects
|
|
stored = database.attachments[row.id]
|
|
assert stored.staging_key is None
|
|
assert stored.updated_at == row.updated_at
|
|
block = database.events[database.events.index("enter", 2) :]
|
|
assert block == [
|
|
"enter",
|
|
"attachments.lock_staging_candidate",
|
|
"storage.delete",
|
|
"attachments.clear_staging_key",
|
|
"commit",
|
|
"exit",
|
|
*block[6:],
|
|
]
|
|
|
|
|
|
async def test_failed_staging_removed_with_variants_after_retention(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
old = await _for_staging(uow, storage, status=AttachmentStatus.FAILED)
|
|
young = await _for_staging(
|
|
uow,
|
|
storage,
|
|
status=AttachmentStatus.FAILED,
|
|
updated_seconds=100,
|
|
)
|
|
storage.seed(old.object_key)
|
|
keys = attachment_object.variant_keys(
|
|
storage,
|
|
attachment_id=old.id,
|
|
pipeline_version=ATTACHMENT_PIPELINE_VERSION,
|
|
)
|
|
for key in keys:
|
|
storage.seed(key)
|
|
result = await _staging_pass(service)
|
|
assert result.removed_failed == 1
|
|
assert old.staging_key not in storage.objects
|
|
assert old.object_key not in storage.objects
|
|
assert all(key not in storage.objects for key in keys)
|
|
assert len(keys) == len(AttachmentVariantName)
|
|
# моложе срока хранения: на месте
|
|
assert young.staging_key in storage.objects
|
|
assert database.attachments[young.id].staging_key is not None
|
|
|
|
|
|
async def test_current_work_not_touched(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
) -> None:
|
|
row = await _for_staging(uow, storage, status=AttachmentStatus.QUEUED)
|
|
result = await _staging_pass(service)
|
|
assert result.removed_ready + result.removed_failed == 0
|
|
assert row.staging_key in storage.objects
|
|
|
|
|
|
async def test_foreign_staging_key_not_removed(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = attachment_dto(
|
|
chat_id=uuid4(),
|
|
owner_id=uuid4(),
|
|
status=AttachmentStatus.READY,
|
|
namespace="other",
|
|
)
|
|
async with uow:
|
|
await uow.attachments.insert_attachment(attachment=row)
|
|
await uow.commit()
|
|
storage.seed(row.staging_key)
|
|
result = await _staging_pass(service)
|
|
assert result.skipped == 1
|
|
assert row.staging_key in storage.objects
|
|
assert database.attachments[row.id].staging_key == row.staging_key
|
|
|
|
|
|
async def test_staging_held_and_storage_failure_and_stop(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
database: FakeDatabase,
|
|
) -> None:
|
|
row = await _for_staging(uow, storage, status=AttachmentStatus.READY)
|
|
database.held.add(row.id)
|
|
assert (await _staging_pass(service)).skipped == 1
|
|
database.held.clear()
|
|
database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable()
|
|
result = await _staging_pass(service)
|
|
assert result.interrupted_by == INTERRUPTED_BY_OBJECT_STORAGE
|
|
assert database.attachments[row.id].staging_key is not None
|
|
database.fail_on.clear()
|
|
result = await _staging_pass(service, stop_requested=lambda: True)
|
|
assert result.interrupted_by == INTERRUPTED_BY_STOP
|
|
assert database.attachments[row.id].staging_key is not None
|
|
|
|
|
|
async def test_staging_keyset_more_than_one_batch(
|
|
service: AttachmentCleanupService,
|
|
uow: FakeUnitOfWork,
|
|
storage: FakeObjectStorage,
|
|
) -> None:
|
|
for index in range(5):
|
|
await _for_staging(
|
|
uow,
|
|
storage,
|
|
status=AttachmentStatus.READY,
|
|
updated_seconds=-1000 + index,
|
|
)
|
|
result = await _staging_pass(service, batch_size=2)
|
|
assert result.removed_ready == 5
|