Files
mtusi2026lab6/student-tests/api/test_cost_experiment.py
T

27 lines
1.1 KiB
Python

import pytest
from opentelemetry.sdk.trace.sampling import Decision
from opentelemetry.trace import SpanKind
from conftest import actor, register, running_app
from messenger.shared.infrastructure.telemetry import ServerRoots
@pytest.mark.parametrize("enabled", [False, True])
async def test_controlled_errors_are_opt_in(enabled: bool) -> None:
async with running_app(cost_experiment=enabled) as client:
user = await register(client, "cost")
result = await client.get(
"/api/v1/users/me", headers={**actor(user), "X-Lab-Cost": "error"}
)
assert result.status_code == (503 if enabled else 200)
assert (await client.get("/api/v1/users/me", headers=actor(user))).status_code == 200
def test_head_zero_drops_roots_and_full_keeps_them() -> None:
for kind in (SpanKind.SERVER, SpanKind.PRODUCER, SpanKind.CONSUMER):
assert ServerRoots(0).should_sample(None, 42, "root", kind).decision == Decision.DROP
assert (
ServerRoots(1).should_sample(None, 42, "root", kind).decision
== Decision.RECORD_AND_SAMPLE
)