Files
mtusi2026lab5/student-tests/adapters/test_rabbitmq.py
T
ArtyomandClaude Opus 5.5 b152b6e07e Process attachment jobs through an outbox, a relay and a worker
Upload now stages the checked original under staging/, and one
transaction moves the intent to queued, inserts the job (pipeline
version 1, attempt 1), its outbox row and an audit event; the 202 is
the in-memory snapshot, never a reread. Content answers 409 until the
attachment is ready, the thumbnail variant is served from the
published version and absent variants are 404.

The relay publishes outbox rows through FastStream with publisher
confirms, mandatory routing and persistent delivery, marks a row only
after the broker acked it, returns jobs whose lease expired and deletes
the staging object of ready attachments. The worker consumes the
quorum queue through a FastStream subscriber that declares nothing,
takes prefetch from WORKER_CONCURRENCY and acks manually after the
result commit, rejects messages off jobs.schema.json to the dead letter
exchange and requeues after an infrastructure error. A job is claimed
under a lease token; the original is copied to originals/ and a
256 px JPEG thumbnail is written under a content-addressed key; the
commit is fenced by the token, inserts variants with ON CONFLICT DO
NOTHING and publishes only a newer pipeline version. A transient error
reschedules the same job with an exponential delay and a new outbox
row, the last attempt fails with retry_exhausted, an undecodable image
fails at once with invalid_image, and a redelivered finished job is a
recorded no-op.

The worker and the relay are assembled in worker.py and relay.py with
the dishka FastStream integration and the platform providers of the
API; the shared core imports neither FastStream nor aio-pika. Tests
cover the thumbnail geometry and rendering, the job message against
the contract, the pipeline against PostgreSQL (duplicates, parallel
copies, retries, the attempt bound, leases and fencing) and the broker
adapters against a throwaway RabbitMQ with the stand's topology and
permissions.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 01:08:06 +03:00

260 lines
8.1 KiB
Python

import asyncio
import base64
import hashlib
import json
import os
import re
import subprocess
import time
from collections.abc import AsyncIterator, Iterator
from contextlib import asynccontextmanager
from datetime import UTC, datetime
from typing import Any
from uuid import uuid4
import pytest
from dishka import Provider, Scope, make_async_container, provide
from dishka_faststream import setup_dishka
from faststream.rabbit import Channel, RabbitBroker
from conftest import ROOT, STARTUP_SECONDS, container, image, published
from fakes import NullLogger
from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import ProcessJob
from messenger.attachments.domain.jobs import Job, Outcome
from messenger.attachments.entrypoints.worker import subscribe
from messenger.attachments.infrastructure.rabbitmq import RabbitJobPublisher
from messenger.shared.application.errors import Unavailable
from messenger.shared.application.ports import Logger
RABBITMQ_IMAGE = re.compile(r"image: (rabbitmq:\S+)")
PASSWORDS = {role: os.urandom(12).hex() for role in ("api", "worker", "ops")}
GRANTS = {
"api": ("^$", "^attachments$", "^$"),
"worker": ("^$", "^$", "^attachments\\.process$"),
"ops": (".*", ".*", ".*"),
}
def user(role: str) -> dict[str, Any]:
salt = os.urandom(4)
digest = hashlib.sha256(salt + PASSWORDS[role].encode()).digest()
return {
"name": f"messenger-{role}",
"password_hash": base64.b64encode(salt + digest).decode(),
"hashing_algorithm": "rabbit_password_hashing_sha256",
"tags": ["administrator"] if role == "ops" else [],
}
def definitions() -> dict[str, Any]:
return {
"users": [user(role) for role in PASSWORDS],
"permissions": [
{
"user": f"messenger-{role}",
"vhost": "messenger",
**dict(zip(("configure", "write", "read"), grant, strict=True)),
}
for role, grant in GRANTS.items()
],
}
def ready(name: str) -> bool:
probe = ["docker", "exec", name, "rabbitmq-diagnostics", "-q", "check_port_connectivity"]
return subprocess.run(probe, capture_output=True, check=False).returncode == 0
@pytest.fixture(scope="session")
def rabbitmq() -> Iterator[dict[str, Any]]:
name = f"mtusi-lab5-test-rabbitmq-{uuid4().hex[:8]}"
folder = ROOT / "build" / name
folder.mkdir(parents=True)
users = folder / "20-users.json"
users.write_text(json.dumps(definitions()))
users.chmod(0o644)
infra = ROOT / "infra" / "rabbitmq"
mounts = [
f"{infra / 'rabbitmq.conf'}:/etc/rabbitmq/conf.d/20-messenger.conf:ro",
f"{infra / 'enabled_plugins'}:/etc/rabbitmq/enabled_plugins:ro",
f"{infra / 'definitions.json'}:/etc/rabbitmq/definitions.d/10-topology.json:ro",
f"{users}:/etc/rabbitmq/definitions.d/20-users.json:ro",
]
volumes = [part for mount in mounts for part in ("-v", mount)]
container(
name,
"--hostname",
"broker",
"--user",
"100:101",
*volumes,
"-p",
"127.0.0.1::5672",
image(RABBITMQ_IMAGE),
)
try:
deadline = time.monotonic() + STARTUP_SECONDS * 2
while not ready(name):
assert time.monotonic() < deadline, "RabbitMQ не стал готов"
time.sleep(1)
port = published(name, 5672)
urls = {
role: f"amqp://messenger-{role}:{secret}@127.0.0.1:{port}/messenger"
for role, secret in PASSWORDS.items()
}
yield {"name": name, "urls": urls}
finally:
subprocess.run(["docker", "rm", "-f", name], capture_output=True, check=False)
users.unlink()
folder.rmdir()
def depth(broker: dict[str, Any], queue: str) -> int:
listing = subprocess.run(
[
"docker",
"exec",
broker["name"],
"rabbitmqctl",
"-q",
"list_queues",
"-p",
"messenger",
"name",
"messages",
"--formatter",
"json",
],
capture_output=True,
check=True,
text=True,
).stdout
return {row["name"]: row["messages"] for row in json.loads(listing)}[queue]
async def wait_for(probe: Any, seconds: float = 20) -> None:
deadline = time.monotonic() + seconds
while not probe():
assert time.monotonic() < deadline
await asyncio.sleep(0.1)
class NoEvents:
@asynccontextmanager
async def transaction(self) -> AsyncIterator[None]:
yield
async def commit(self) -> None: ...
def record(self, event: object) -> None: ...
def collect_new_events(self) -> list[Any]:
return []
class Recorder:
def __init__(self) -> None:
self.jobs: list[Job] = []
self.failures = 0
async def __call__(self, command: ProcessJob) -> Outcome:
if self.failures:
self.failures -= 1
raise Unavailable("PostgreSQL недоступен")
self.jobs.append(command.job)
return Outcome.DONE
class FakeBus(Provider):
def __init__(self, recorder: Recorder) -> None:
super().__init__()
self._recorder = recorder
@provide(scope=Scope.APP)
def logger(self) -> Logger:
return NullLogger()
@provide(scope=Scope.REQUEST)
def bus(self) -> AttachmentsCommandBus:
return AttachmentsCommandBus({ProcessJob: self._recorder}, NoEvents(), NoPublisher())
class NoPublisher:
async def publish(self, events: Any) -> None: ...
@asynccontextmanager
async def worker(rabbitmq: dict[str, Any], recorder: Recorder) -> AsyncIterator[None]:
broker = RabbitBroker(rabbitmq["urls"]["worker"], logger=None, graceful_timeout=2)
subscribe(broker, 2)
setup_dishka(make_async_container(FakeBus(recorder)), broker=broker, auto_inject=True)
await broker.start()
try:
yield
finally:
await broker.stop()
@asynccontextmanager
async def publisher(
rabbitmq: dict[str, Any], role: str = "api"
) -> AsyncIterator[RabbitJobPublisher]:
broker = RabbitBroker(
rabbitmq["urls"][role],
default_channel=Channel(publisher_confirms=True, on_return_raises=True),
logger=None,
)
await broker.connect()
try:
yield RabbitJobPublisher(broker, 5)
finally:
await broker.stop()
def job() -> Job:
return Job(uuid4(), uuid4(), 1, 1, datetime.now(UTC))
def body(payload: dict[str, Any]) -> bytes:
return json.dumps(payload).encode()
async def test_confirmed_job_is_consumed_and_acked(rabbitmq: dict[str, Any]) -> None:
recorder, sent = Recorder(), job()
async with publisher(rabbitmq) as jobs:
await jobs.publish(sent.id, body(sent.payload()))
await wait_for(lambda: depth(rabbitmq, "attachments.process") == 1)
async with worker(rabbitmq, recorder):
await wait_for(lambda: recorder.jobs)
await wait_for(lambda: depth(rabbitmq, "attachments.process") == 0)
assert recorder.jobs == [sent]
async def test_message_off_the_schema_goes_to_the_dead_letter_queue(
rabbitmq: dict[str, Any],
) -> None:
before = depth(rabbitmq, "attachments.process.dlq")
async with publisher(rabbitmq) as jobs:
await jobs.publish(uuid4(), body({"schema_version": 2, "job_id": str(uuid4())}))
async with worker(rabbitmq, Recorder()):
await wait_for(lambda: depth(rabbitmq, "attachments.process.dlq") == before + 1)
assert depth(rabbitmq, "attachments.process") == 0
async def test_failed_processing_is_requeued_and_retried(rabbitmq: dict[str, Any]) -> None:
recorder, sent = Recorder(), job()
recorder.failures = 1
async with publisher(rabbitmq) as jobs:
await jobs.publish(sent.id, body(sent.payload()))
async with worker(rabbitmq, recorder):
await wait_for(lambda: recorder.jobs)
assert recorder.jobs == [sent]
async def test_worker_account_cannot_publish(rabbitmq: dict[str, Any]) -> None:
sent = job()
with pytest.raises(Unavailable):
async with publisher(rabbitmq, "worker") as jobs:
await jobs.publish(sent.id, body(sent.payload()))