Files
mtusi2026lab2/student-tests/unit/test_pipeline_domain.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

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

94 lines
3.4 KiB
Python

import json
from datetime import UTC, datetime, timedelta
from pathlib import Path
from uuid import uuid4
import pytest
from messenger.attachments.domain.jobs import Job, RetryPolicy
from messenger.attachments.domain.variants import fit, variant_key
from messenger.attachments.entrypoints.schemas import JobMessage
from messenger.attachments.infrastructure.jitter import RandomJitter
SCHEMA = json.loads(
(Path(__file__).resolve().parents[2] / "contracts" / "jobs.schema.json").read_text()
)
@pytest.mark.parametrize(
("size", "fitted"),
[
((320, 200), (256, 160)),
((300, 200), (256, 170)),
((801, 601), (256, 192)),
((17, 1000), (4, 256)),
((3000, 2), (256, 1)),
((2, 3000), (1, 256)),
((64, 64), (64, 64)),
((256, 100), (256, 100)),
((257, 257), (256, 256)),
],
)
def test_thumbnail_fits_the_box_rounding_down(
size: tuple[int, int], fitted: tuple[int, int]
) -> None:
assert fit(*size, 256) == fitted
@pytest.mark.parametrize(("spread", "share"), [(0.0, 0.5), (0.5, 0.75), (0.999, 0.9995)])
def test_retry_delay_has_equal_jitter_up_to_the_cap(spread: float, share: float) -> None:
policy = RetryPolicy(max_attempts=5, base_seconds=2, cap_seconds=10)
delays = [policy.delay(attempt, spread) for attempt in range(1, 6)]
ceilings = [2, 4, 8, 10]
assert delays[:4] == [timedelta(seconds=ceiling * share) for ceiling in ceilings]
assert delays[4] is None
assert policy.exhausted(5)
assert not policy.exhausted(4)
def test_jitter_spreads_retries_between_half_and_full_delay() -> None:
policy, jitter = RetryPolicy(5, 2, 60), RandomJitter()
delays = {policy.delay(3, jitter.spread()) for _ in range(200)}
assert all(timedelta(seconds=4) <= delay < timedelta(seconds=8) for delay in delays if delay)
assert len(delays) > 150
def test_job_message_follows_the_contract_schema() -> None:
job = Job(uuid4(), uuid4(), 1, 3, datetime(2026, 9, 1, 12, 0, tzinfo=UTC))
payload = job.payload()
assert set(SCHEMA["required"]) <= set(payload) <= set(SCHEMA["properties"])
assert payload["schema_version"] == SCHEMA["properties"]["schema_version"]["const"]
assert payload["type"] == SCHEMA["properties"]["type"]["const"]
assert payload["created_at"] == "2026-09-01T12:00:00.000000Z"
assert JobMessage.model_validate_json(json.dumps(payload)).job() == job
assert job.retried(4).payload()["job_id"] == payload["job_id"]
@pytest.mark.parametrize("example", SCHEMA["examples"])
def test_contract_examples_are_accepted(example: dict[str, object]) -> None:
assert JobMessage.model_validate_json(json.dumps(example)).attempt == example["attempt"]
@pytest.mark.parametrize(
"change",
[
{"schema_version": 2},
{"type": "attachment.process.v2"},
{"attempt": 0},
{"pipeline_version": 0},
{"filename": "a.png"},
{"traceparent": "01-xyz"},
{"created_at": "2026-09-01T12:00:00"},
],
)
def test_messages_off_the_schema_are_rejected(change: dict[str, object]) -> None:
message = {**SCHEMA["examples"][0], **change}
with pytest.raises(ValueError, match="validation error"):
JobMessage.model_validate_json(json.dumps(message))
def test_variant_keys_carry_version_and_content_hash() -> None:
attachment = uuid4()
key = variant_key(attachment, 2, "thumbnail", "ab" * 32, "jpg")
assert key == f"variants/{attachment}/2/thumbnail-{'ab' * 8}.jpg"