Shutdown budget leaves room for SDK shutdown, stand-from-scratch accepts only its throwaway project, OTLP exporters ignore proxy environment, telemetry-check covers log/trace links, health series and worker instance id; spec deltas match what the parser and SDK lifetime allow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.4 KiB
Python
97 lines
3.4 KiB
Python
"""Значения `[uploads]` (спек `service-configuration`, «Неверный параметр
|
||
загрузок») и сочетание срока остановки API с телеметрией (спек
|
||
`graceful-shutdown`, «Остановка при недоступном Collector»). Секция или
|
||
`Settings` только из аргументов: настоящий читает `.env` и `config.toml`.
|
||
"""
|
||
|
||
from typing import Any
|
||
|
||
import pytest
|
||
from pydantic import ValidationError
|
||
from pydantic_settings import (
|
||
BaseSettings,
|
||
PydanticBaseSettingsSource,
|
||
SettingsConfigDict,
|
||
)
|
||
|
||
from src.config.settings import Settings, Uploads, Worker
|
||
from src.utils.constants import (
|
||
API_STOP_BUDGET_SECONDS,
|
||
CONTAINER_STOP_TIMEOUT_SECONDS,
|
||
TELEMETRY_SHUTDOWN_TIMEOUT_SECONDS,
|
||
)
|
||
from src.worker import main as worker_main
|
||
|
||
|
||
class _ArgumentsOnly(Settings):
|
||
"""`Settings` без окружения, `.env` и `config.toml`."""
|
||
|
||
model_config = SettingsConfigDict(
|
||
**{**Settings.model_config, "env_file": None, "toml_file": None},
|
||
)
|
||
|
||
@classmethod
|
||
def settings_customise_sources(
|
||
cls,
|
||
settings_cls: type[BaseSettings], # noqa: ARG003
|
||
init_settings: PydanticBaseSettingsSource,
|
||
*args: PydanticBaseSettingsSource, # noqa: ARG003
|
||
**kwargs: Any, # noqa: ARG003
|
||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||
return (init_settings,)
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
("field", "value"),
|
||
[
|
||
("max_concurrent", 0),
|
||
("max_waiting", -1),
|
||
("max_unfinished_jobs", -1),
|
||
("body_timeout_seconds", "abc"),
|
||
],
|
||
)
|
||
def test_wrong_value_names_field(field: str, value: object) -> None:
|
||
with pytest.raises(ValidationError) as caught:
|
||
Uploads(**{field: value})
|
||
assert [error["loc"] for error in caught.value.errors()] == [(field,)]
|
||
|
||
|
||
def test_zero_turns_backlog_limit_off() -> None:
|
||
assert Uploads(max_unfinished_jobs=0).max_unfinished_jobs == 0
|
||
assert Uploads().max_unfinished_jobs == 250
|
||
|
||
|
||
def test_api_stop_with_telemetry_over_budget_names_field() -> None:
|
||
"""9 с дренажа + 1 с закрытия SDK — SIGKILL docker (verify W3)."""
|
||
with pytest.raises(ValidationError) as caught:
|
||
_ArgumentsOnly(
|
||
app={"graceful_shutdown_seconds": 9},
|
||
telemetry={"enabled": True},
|
||
)
|
||
errors = caught.value.errors(include_input=False)
|
||
assert [error["loc"] for error in errors] == [("telemetry",)]
|
||
assert "app.graceful_shutdown_seconds" in errors[0]["msg"]
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
("graceful", "enabled"),
|
||
[(9, False), (8, True), (1, True)],
|
||
)
|
||
def test_api_stop_within_budget(graceful: int, enabled: bool) -> None:
|
||
loaded = _ArgumentsOnly(
|
||
app={"graceful_shutdown_seconds": graceful},
|
||
telemetry={"enabled": enabled},
|
||
)
|
||
assert loaded.app.graceful_shutdown_seconds == graceful
|
||
|
||
|
||
def test_budgets_fit_container_stop() -> None:
|
||
"""API: дренаж до 8 с + закрытие SDK; worker: ожидание до 8 с, отмена,
|
||
потребитель и одно окно `gather` с закрытием SDK — по 0,5 с."""
|
||
assert API_STOP_BUDGET_SECONDS < CONTAINER_STOP_TIMEOUT_SECONDS
|
||
assert 8 + TELEMETRY_SHUTDOWN_TIMEOUT_SECONDS <= API_STOP_BUDGET_SECONDS
|
||
with pytest.raises(ValidationError):
|
||
Worker(graceful_shutdown_seconds=8.01)
|
||
worker_worst = 8 + 3 * worker_main._CLOSE_TIMEOUT_SECONDS
|
||
assert worker_worst < CONTAINER_STOP_TIMEOUT_SECONDS
|