PLBA2-2. Реализация
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,179 @@
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import asyncio
|
||||
import unittest
|
||||
|
||||
from plba.config import (
|
||||
ApplicationConfigLoadError,
|
||||
ApplicationConfigLoader,
|
||||
ApplicationConfigManager,
|
||||
ConfigReloadStatus,
|
||||
)
|
||||
from plba.runtime import ApplicationLifecycle, ApplicationRuntime, RuntimeContext
|
||||
from plba.runtime.worker import (
|
||||
WorkerConfig,
|
||||
WorkerDefinition,
|
||||
WorkerRegistry,
|
||||
WorkerSupervisor,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ServiceConfig:
|
||||
service_name: str
|
||||
|
||||
|
||||
def decode_service_config(raw: dict[str, object]) -> ServiceConfig:
|
||||
name = raw.get("service_name")
|
||||
if not isinstance(name, str):
|
||||
raise ValueError("service_name is required")
|
||||
return ServiceConfig(name)
|
||||
|
||||
|
||||
class ApplicationConfigTests(unittest.TestCase):
|
||||
def test_loads_common_fields_and_typed_application_config(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text(
|
||||
"application_id: service-a\nservice_name: alpha\n"
|
||||
"workers:\n sync:\n pause_seconds: 5\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
|
||||
snapshot = manager.load_initial()
|
||||
|
||||
self.assertEqual(snapshot.application_id, "service-a")
|
||||
self.assertEqual(snapshot.application_config, ServiceConfig("alpha"))
|
||||
self.assertEqual(manager.get_worker_config("sync").pause_seconds, 5)
|
||||
self.assertFalse(manager.get_worker_config("undeclared").disabled)
|
||||
|
||||
def test_invalid_initial_file_blocks_startup(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text("workers: [not-a-mapping]\n", encoding="utf-8")
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
|
||||
with self.assertRaises(ApplicationConfigLoadError):
|
||||
manager.load_initial()
|
||||
|
||||
def test_reload_is_atomic_and_invalid_update_keeps_last_snapshot(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text("service_name: alpha\n", encoding="utf-8")
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
previous = manager.load_initial()
|
||||
path.write_text("service_name: beta\n", encoding="utf-8")
|
||||
|
||||
reloaded = manager.reload_if_changed()
|
||||
|
||||
self.assertEqual(reloaded.status, ConfigReloadStatus.RELOADED)
|
||||
self.assertEqual(previous.application_config, ServiceConfig("alpha"))
|
||||
self.assertEqual(manager.current().application_config, ServiceConfig("beta"))
|
||||
revision = manager.current().revision
|
||||
path.write_text("service_name: [invalid]\n", encoding="utf-8")
|
||||
|
||||
failed = manager.reload_if_changed()
|
||||
|
||||
self.assertEqual(failed.status, ConfigReloadStatus.FAILED)
|
||||
self.assertEqual(manager.current().revision, revision)
|
||||
self.assertEqual(manager.current().application_config, ServiceConfig("beta"))
|
||||
|
||||
def test_disabled_worker_does_not_require_a_schedule(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text("service_name: alpha\nworkers:\n inactive:\n disabled: true\n", encoding="utf-8")
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
|
||||
manager.load_initial()
|
||||
|
||||
self.assertTrue(manager.get_worker_config("inactive").disabled)
|
||||
|
||||
|
||||
class ConfigWatcherTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_watcher_reports_invalid_reload_and_stops_promptly(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text("service_name: alpha\n", encoding="utf-8")
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
manager.load_initial()
|
||||
stop_event = asyncio.Event()
|
||||
error_reported = asyncio.Event()
|
||||
|
||||
async def report_error(_: Exception) -> None:
|
||||
error_reported.set()
|
||||
|
||||
async def write_invalid() -> None:
|
||||
await asyncio.sleep(0.01)
|
||||
path.write_text("workers: []\n", encoding="utf-8")
|
||||
|
||||
watcher = asyncio.create_task(
|
||||
manager.watch(stop_event, 0.02, report_error)
|
||||
)
|
||||
writer = asyncio.create_task(write_invalid())
|
||||
await asyncio.wait_for(error_reported.wait(), timeout=1)
|
||||
stop_event.set()
|
||||
await asyncio.wait_for(watcher, timeout=1)
|
||||
await writer
|
||||
|
||||
|
||||
class ConfigRuntimeIntegrationTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_disabled_change_waits_until_next_application_start(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "config.yaml"
|
||||
path.write_text(
|
||||
"service_name: alpha\nworkers:\n sync:\n pause_seconds: 0.01\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
manager = ApplicationConfigManager(
|
||||
ApplicationConfigLoader(path, decode_service_config)
|
||||
)
|
||||
manager.load_initial()
|
||||
first_cycle = asyncio.Event()
|
||||
later_cycle = asyncio.Event()
|
||||
calls = 0
|
||||
|
||||
async def process() -> None:
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
first_cycle.set()
|
||||
if calls >= 2:
|
||||
later_cycle.set()
|
||||
|
||||
registry = WorkerRegistry()
|
||||
registry.register(WorkerDefinition("sync", process))
|
||||
supervisor = WorkerSupervisor(registry, manager)
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
await runtime.start()
|
||||
await asyncio.wait_for(first_cycle.wait(), timeout=1)
|
||||
path.write_text(
|
||||
"service_name: alpha\nworkers:\n sync:\n disabled: true\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
self.assertEqual(manager.reload_if_changed().status, ConfigReloadStatus.RELOADED)
|
||||
await asyncio.wait_for(later_cycle.wait(), timeout=1)
|
||||
self.assertEqual(runtime.lifecycle, ApplicationLifecycle.RUNNING)
|
||||
|
||||
await runtime.stop()
|
||||
before_restart = calls
|
||||
restarted = await runtime.start()
|
||||
await asyncio.sleep(0.03)
|
||||
|
||||
self.assertEqual(restarted.workers.started, ())
|
||||
self.assertEqual(calls, before_restart)
|
||||
await runtime.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,219 @@
|
||||
import asyncio
|
||||
import json
|
||||
import unittest
|
||||
from typing import Any
|
||||
|
||||
from plba.health import (
|
||||
HealthAggregator,
|
||||
HealthComponent,
|
||||
HealthExpectation,
|
||||
HealthInput,
|
||||
HealthInputInvalid,
|
||||
HealthContributorRegistry,
|
||||
RuntimeHealthInputProvider,
|
||||
)
|
||||
from plba.http import ControlApi
|
||||
from plba.runtime import ApplicationLifecycle, ApplicationRuntime, RuntimeContext
|
||||
from plba.runtime.worker import (
|
||||
ComponentHealth,
|
||||
HealthStatus,
|
||||
WorkerConfig,
|
||||
WorkerDefinition,
|
||||
WorkerRegistry,
|
||||
WorkerSupervisor,
|
||||
)
|
||||
|
||||
|
||||
class StaticHealthProvider:
|
||||
def __init__(self, health_input: HealthInput) -> None:
|
||||
self.health_input = health_input
|
||||
|
||||
async def read(self) -> HealthInput:
|
||||
return self.health_input
|
||||
|
||||
|
||||
class EmptyWorkerConfigReader:
|
||||
def get_worker_config(self, worker_name: str) -> WorkerConfig:
|
||||
return WorkerConfig()
|
||||
|
||||
|
||||
class HealthAggregationTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_missing_critical_and_noncritical_components_apply_priority(self) -> None:
|
||||
health_input = HealthInput(
|
||||
"service-a",
|
||||
"running",
|
||||
(
|
||||
HealthExpectation("required", True),
|
||||
HealthExpectation("optional", False),
|
||||
),
|
||||
(
|
||||
HealthComponent("degraded", HealthStatus.DEGRADED, True),
|
||||
HealthComponent("optional-bad", HealthStatus.UNHEALTHY, False),
|
||||
),
|
||||
)
|
||||
|
||||
report = await HealthAggregator(StaticHealthProvider(health_input)).aggregate()
|
||||
|
||||
self.assertEqual(report.status, "unhealthy")
|
||||
self.assertEqual(
|
||||
{item.name: item.status for item in report.components}["required"],
|
||||
HealthStatus.UNHEALTHY,
|
||||
)
|
||||
self.assertEqual(
|
||||
{item.name: item.status for item in report.components}["optional"],
|
||||
HealthStatus.DEGRADED,
|
||||
)
|
||||
|
||||
async def test_noncritical_unhealthy_is_capped_at_degraded(self) -> None:
|
||||
health_input = HealthInput(
|
||||
"service-a",
|
||||
"running",
|
||||
(),
|
||||
(HealthComponent("cache", HealthStatus.UNHEALTHY, False),),
|
||||
)
|
||||
|
||||
report = await HealthAggregator(StaticHealthProvider(health_input)).aggregate()
|
||||
|
||||
self.assertEqual(report.status, "degraded")
|
||||
self.assertEqual(report.components[0].status, HealthStatus.UNHEALTHY)
|
||||
|
||||
async def test_lifecycle_controls_base_health_and_invalid_inputs_fail(self) -> None:
|
||||
for lifecycle, expected in (
|
||||
("new", "unhealthy"),
|
||||
("starting", "degraded"),
|
||||
("stopping", "degraded"),
|
||||
("failed", "unhealthy"),
|
||||
("stopped", "stopped"),
|
||||
):
|
||||
report = await HealthAggregator(
|
||||
StaticHealthProvider(HealthInput("service-a", lifecycle, (), ()))
|
||||
).aggregate()
|
||||
self.assertEqual(report.status, expected)
|
||||
|
||||
invalid = HealthInput(
|
||||
"service-a", "running", (), (HealthComponent("bad", "unknown", True),) # type: ignore[arg-type]
|
||||
)
|
||||
with self.assertRaises(HealthInputInvalid):
|
||||
await HealthAggregator(StaticHealthProvider(invalid)).aggregate()
|
||||
|
||||
async def test_disabled_worker_is_not_expected_before_application_start(self) -> None:
|
||||
registry = WorkerRegistry()
|
||||
|
||||
async def process() -> None:
|
||||
return None
|
||||
|
||||
registry.register(WorkerDefinition("disabled", process, critical=True))
|
||||
|
||||
class ConfigReader:
|
||||
def get_worker_config(self, worker_name: str) -> WorkerConfig:
|
||||
return WorkerConfig(disabled=True)
|
||||
|
||||
supervisor = WorkerSupervisor(registry, ConfigReader())
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
provider = RuntimeHealthInputProvider(runtime, supervisor, HealthContributorRegistry())
|
||||
|
||||
report = await HealthAggregator(provider).aggregate()
|
||||
|
||||
self.assertEqual(report.components, ())
|
||||
self.assertEqual(report.status, "unhealthy")
|
||||
|
||||
|
||||
class ControlApiTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self) -> None:
|
||||
self.registry = WorkerRegistry()
|
||||
self.supervisor = WorkerSupervisor(self.registry, EmptyWorkerConfigReader())
|
||||
self.runtime = ApplicationRuntime(RuntimeContext("service-http"), self.supervisor)
|
||||
provider = RuntimeHealthInputProvider(
|
||||
self.runtime, self.supervisor, HealthContributorRegistry()
|
||||
)
|
||||
self.api = ControlApi(self.runtime, HealthAggregator(provider))
|
||||
|
||||
async def test_status_start_health_and_stop_routes(self) -> None:
|
||||
status = await self.request("GET", "/runtime/status")
|
||||
self.assertEqual(status[0], 200)
|
||||
self.assertEqual(status[1], {"status": "new"})
|
||||
|
||||
started = await self.request("POST", "/runtime/start")
|
||||
self.assertEqual(started[0], 200)
|
||||
self.assertEqual(started[1], {"status": "running"})
|
||||
|
||||
health = await self.request("GET", "/health")
|
||||
self.assertEqual(health[0], 200)
|
||||
self.assertEqual(health[1]["application_id"], "service-http")
|
||||
self.assertEqual(health[1]["status"], "ok")
|
||||
|
||||
stopped = await self.request("POST", "/runtime/stop", b"{}")
|
||||
self.assertEqual(stopped, (200, {"status": "stopped"}))
|
||||
|
||||
async def test_invalid_stop_request_is_http_400(self) -> None:
|
||||
missing = await self.request("POST", "/runtime/stop")
|
||||
malformed = await self.request("POST", "/runtime/stop", b"not-json")
|
||||
invalid_timeout = await self.request(
|
||||
"POST", "/runtime/stop", b'{"timeout_seconds": 0}'
|
||||
)
|
||||
null_timeout = await self.request(
|
||||
"POST", "/runtime/stop", b'{"timeout_seconds": null}'
|
||||
)
|
||||
|
||||
self.assertEqual(missing[0], 400)
|
||||
self.assertEqual(malformed[0], 400)
|
||||
self.assertEqual(invalid_timeout[0], 400)
|
||||
self.assertEqual(null_timeout[0], 400)
|
||||
|
||||
async def test_timeout_returns_202_while_worker_stop_continues(self) -> None:
|
||||
active = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
|
||||
async def process() -> None:
|
||||
active.set()
|
||||
await release.wait()
|
||||
|
||||
self.registry.register(WorkerDefinition("blocking", process))
|
||||
# A fresh supervisor/runtime picks up the worker registered above.
|
||||
self.supervisor = WorkerSupervisor(
|
||||
self.registry,
|
||||
type("Reader", (), {"get_worker_config": lambda _self, _name: WorkerConfig(pause_seconds=1)})(),
|
||||
)
|
||||
self.runtime = ApplicationRuntime(RuntimeContext("service-http"), self.supervisor)
|
||||
provider = RuntimeHealthInputProvider(
|
||||
self.runtime, self.supervisor, HealthContributorRegistry()
|
||||
)
|
||||
self.api = ControlApi(self.runtime, HealthAggregator(provider))
|
||||
await self.runtime.start()
|
||||
await asyncio.wait_for(active.wait(), timeout=1)
|
||||
|
||||
result = await self.request(
|
||||
"POST", "/runtime/stop", b'{"timeout_seconds": 0.01}'
|
||||
)
|
||||
|
||||
self.assertEqual(result, (202, {"status": "stopping"}))
|
||||
release.set()
|
||||
await asyncio.wait_for(self._wait_until_stopped(), timeout=1)
|
||||
|
||||
async def _wait_until_stopped(self) -> None:
|
||||
while self.runtime.lifecycle is not ApplicationLifecycle.STOPPED:
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def request(
|
||||
self, method: str, path: str, body: bytes = b"", query: bytes = b""
|
||||
) -> tuple[int, dict[str, Any]]:
|
||||
incoming = [{"type": "http.request", "body": body, "more_body": False}]
|
||||
outgoing: list[dict[str, Any]] = []
|
||||
|
||||
async def receive() -> dict[str, Any]:
|
||||
return incoming.pop(0) if incoming else {"type": "http.disconnect"}
|
||||
|
||||
async def send(message: dict[str, Any]) -> None:
|
||||
outgoing.append(message)
|
||||
|
||||
await self.api(
|
||||
{"type": "http", "method": method, "path": path, "query_string": query},
|
||||
receive,
|
||||
send,
|
||||
)
|
||||
response = json.loads(outgoing[-1]["body"])
|
||||
return outgoing[0]["status"], response
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,54 @@
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import unittest
|
||||
from uuid import UUID
|
||||
|
||||
from plba.runtime.application_identity import (
|
||||
ApplicationIdentityResolver,
|
||||
TomlProjectNameReader,
|
||||
)
|
||||
|
||||
|
||||
class FixedProjectNameReader:
|
||||
def __init__(self, name: str | None) -> None:
|
||||
self.name = name
|
||||
self.calls = 0
|
||||
|
||||
def read_project_name(self) -> str | None:
|
||||
self.calls += 1
|
||||
return self.name
|
||||
|
||||
|
||||
class ApplicationIdentityTests(unittest.TestCase):
|
||||
def test_configured_identity_has_priority_and_is_unchanged(self) -> None:
|
||||
reader = FixedProjectNameReader("project-name")
|
||||
resolver = ApplicationIdentityResolver(reader)
|
||||
|
||||
context = resolver.resolve(" custom-id ")
|
||||
|
||||
self.assertEqual(context.application_id, " custom-id ")
|
||||
self.assertEqual(reader.calls, 0)
|
||||
|
||||
def test_project_name_is_fallback(self) -> None:
|
||||
resolver = ApplicationIdentityResolver(FixedProjectNameReader("project-name"))
|
||||
|
||||
self.assertEqual(resolver.resolve("").application_id, "project-name")
|
||||
|
||||
def test_missing_project_metadata_uses_uuidv4(self) -> None:
|
||||
expected = UUID("12345678-1234-4234-8234-123456789abc")
|
||||
resolver = ApplicationIdentityResolver(
|
||||
FixedProjectNameReader(None), uuid_factory=lambda: expected
|
||||
)
|
||||
|
||||
self.assertEqual(resolver.resolve().application_id, str(expected))
|
||||
|
||||
def test_toml_reader_uses_project_name(self) -> None:
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "pyproject.toml"
|
||||
path.write_text('[project]\nname = "demo-app"\n', encoding="utf-8")
|
||||
|
||||
self.assertEqual(TomlProjectNameReader(path).read_project_name(), "demo-app")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,130 @@
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from io import StringIO
|
||||
import unittest
|
||||
from uuid import UUID
|
||||
from typing import Any
|
||||
|
||||
from plba.logging import TechnicalJsonFormatter, create_stdout_handler
|
||||
from plba.tracing import (
|
||||
BusinessTraceContext,
|
||||
BusinessTraceMessage,
|
||||
BusinessTraceWriteError,
|
||||
PostgresBusinessTraceWriter,
|
||||
business_trace_context,
|
||||
)
|
||||
|
||||
|
||||
class FakeTraceConnection:
|
||||
def __init__(self, error: Exception | None = None) -> None:
|
||||
self.calls: list[tuple[str, tuple[object, ...]]] = []
|
||||
self.error = error
|
||||
|
||||
async def execute(self, query: str, *parameters: object) -> str:
|
||||
self.calls.append((query, parameters))
|
||||
if self.error:
|
||||
raise self.error
|
||||
return "INSERT 0 1"
|
||||
|
||||
|
||||
class FakeAcquire:
|
||||
def __init__(self, connection: FakeTraceConnection) -> None:
|
||||
self.connection = connection
|
||||
|
||||
async def __aenter__(self) -> FakeTraceConnection:
|
||||
return self.connection
|
||||
|
||||
async def __aexit__(self, *_: object) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class FakeTracePool:
|
||||
def __init__(self, connection: FakeTraceConnection) -> None:
|
||||
self.connection = connection
|
||||
|
||||
def acquire(self) -> FakeAcquire:
|
||||
return FakeAcquire(self.connection)
|
||||
|
||||
|
||||
class BusinessTraceAdapterTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_adapter_adds_application_id_to_context_and_message(self) -> None:
|
||||
connection = FakeTraceConnection()
|
||||
writer = PostgresBusinessTraceWriter(FakeTracePool(connection), "orders", "app-42")
|
||||
trace_id = UUID("12345678-1234-4234-8234-123456789abc")
|
||||
event_time = datetime.now(timezone.utc)
|
||||
|
||||
await writer.write_context(
|
||||
BusinessTraceContext(
|
||||
trace_id, "order", "create", event_time, {"application_id": "spoofed"}
|
||||
)
|
||||
)
|
||||
await writer.write_message(
|
||||
BusinessTraceMessage(trace_id, event_time, "reserve", "ok", "info", "stock reserved")
|
||||
)
|
||||
|
||||
self.assertEqual(len(connection.calls), 2)
|
||||
context_query, context_params = connection.calls[0]
|
||||
message_query, message_params = connection.calls[1]
|
||||
self.assertIn('"orders".trace_contexts', context_query)
|
||||
self.assertIn('"orders".trace_messages', message_query)
|
||||
self.assertEqual(json.loads(context_params[-1]), {"application_id": "app-42"})
|
||||
self.assertEqual(json.loads(message_params[-1]), {"application_id": "app-42"})
|
||||
|
||||
async def test_database_failure_is_passed_without_retry(self) -> None:
|
||||
connection = FakeTraceConnection(RuntimeError("database unavailable"))
|
||||
writer = PostgresBusinessTraceWriter(FakeTracePool(connection), "orders", "app-42")
|
||||
trace_id = UUID("12345678-1234-4234-8234-123456789abc")
|
||||
message = BusinessTraceMessage(
|
||||
trace_id, datetime.now(timezone.utc), "step", "failed", "error", "failed"
|
||||
)
|
||||
|
||||
with self.assertRaises(BusinessTraceWriteError):
|
||||
await writer.write_message(message)
|
||||
self.assertEqual(len(connection.calls), 1)
|
||||
|
||||
|
||||
class TechnicalLoggingTests(unittest.TestCase):
|
||||
def test_formatter_emits_contract_and_async_context_trace_id(self) -> None:
|
||||
trace_id = UUID("12345678-1234-4234-8234-123456789abc")
|
||||
stream = StringIO()
|
||||
logger = logging.getLogger("plba.test.structured")
|
||||
logger.handlers.clear()
|
||||
logger.propagate = False
|
||||
logger.setLevel(logging.INFO)
|
||||
handler = logging.StreamHandler(stream)
|
||||
handler.setFormatter(TechnicalJsonFormatter("app-42"))
|
||||
logger.addHandler(handler)
|
||||
try:
|
||||
with business_trace_context(trace_id):
|
||||
logger.info("reservation completed", extra={"attributes": {"order_id": 81}})
|
||||
logger.info("outside trace")
|
||||
finally:
|
||||
logger.removeHandler(handler)
|
||||
|
||||
records = [json.loads(line) for line in stream.getvalue().splitlines()]
|
||||
self.assertEqual(
|
||||
set(records[0]),
|
||||
{
|
||||
"timestamp",
|
||||
"level",
|
||||
"logger",
|
||||
"message",
|
||||
"application_id",
|
||||
"attributes",
|
||||
"business_trace_id",
|
||||
},
|
||||
)
|
||||
self.assertTrue(records[0]["timestamp"].endswith("Z"))
|
||||
self.assertEqual(records[0]["application_id"], "app-42")
|
||||
self.assertEqual(records[0]["business_trace_id"], str(trace_id))
|
||||
self.assertNotIn("business_trace_id", records[1])
|
||||
|
||||
def test_stdout_helper_uses_a_standard_stream_handler(self) -> None:
|
||||
handler = create_stdout_handler("app-42", StringIO())
|
||||
self.assertIsInstance(handler, logging.StreamHandler)
|
||||
self.assertIsInstance(handler.formatter, TechnicalJsonFormatter)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,153 @@
|
||||
import asyncio
|
||||
import unittest
|
||||
|
||||
from plba.runtime import (
|
||||
ApplicationLifecycle,
|
||||
ApplicationRuntime,
|
||||
RuntimeContext,
|
||||
)
|
||||
from plba.runtime.worker import (
|
||||
DuplicateWorkerError,
|
||||
WorkerConfig,
|
||||
WorkerDefinition,
|
||||
WorkerLifecycle,
|
||||
WorkerRegistry,
|
||||
WorkerSupervisor,
|
||||
)
|
||||
|
||||
|
||||
class FakeWorkerConfigReader:
|
||||
def __init__(self, configs: dict[str, WorkerConfig]) -> None:
|
||||
self.configs = configs
|
||||
|
||||
def get_worker_config(self, worker_name: str) -> WorkerConfig:
|
||||
return self.configs.get(worker_name, WorkerConfig())
|
||||
|
||||
|
||||
class WorkerRegistryTests(unittest.TestCase):
|
||||
def test_duplicate_names_are_rejected(self) -> None:
|
||||
registry = WorkerRegistry()
|
||||
process = self._process
|
||||
registry.register(WorkerDefinition("sync", process))
|
||||
with self.assertRaises(DuplicateWorkerError):
|
||||
registry.register(WorkerDefinition("sync", process))
|
||||
|
||||
@staticmethod
|
||||
async def _process() -> None:
|
||||
return None
|
||||
|
||||
|
||||
class WorkerRuntimeTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_disabled_worker_is_skipped_and_has_no_health_snapshot(self) -> None:
|
||||
calls = 0
|
||||
|
||||
async def process() -> None:
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
|
||||
registry = WorkerRegistry()
|
||||
registry.register(WorkerDefinition("cleanup", process))
|
||||
supervisor = WorkerSupervisor(
|
||||
registry, FakeWorkerConfigReader({"cleanup": WorkerConfig(disabled=True)})
|
||||
)
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
|
||||
started = await runtime.start()
|
||||
stopped = await runtime.stop()
|
||||
|
||||
self.assertEqual(started.lifecycle, ApplicationLifecycle.RUNNING)
|
||||
self.assertEqual(started.workers.started, ())
|
||||
self.assertEqual(supervisor.snapshots(), ())
|
||||
self.assertEqual(calls, 0)
|
||||
self.assertEqual(stopped.lifecycle, ApplicationLifecycle.STOPPED)
|
||||
|
||||
async def test_cycles_are_sequential_and_continue_after_failure(self) -> None:
|
||||
second_started = asyncio.Event()
|
||||
release_second = asyncio.Event()
|
||||
calls = 0
|
||||
active = 0
|
||||
max_active = 0
|
||||
|
||||
async def process() -> None:
|
||||
nonlocal calls, active, max_active
|
||||
calls += 1
|
||||
active += 1
|
||||
max_active = max(max_active, active)
|
||||
try:
|
||||
if calls == 1:
|
||||
raise RuntimeError("first cycle failed")
|
||||
if calls == 2:
|
||||
second_started.set()
|
||||
await release_second.wait()
|
||||
finally:
|
||||
active -= 1
|
||||
|
||||
registry = WorkerRegistry()
|
||||
registry.register(WorkerDefinition("sync", process))
|
||||
supervisor = WorkerSupervisor(
|
||||
registry, FakeWorkerConfigReader({"sync": WorkerConfig(pause_seconds=0.001)})
|
||||
)
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
await runtime.start()
|
||||
await asyncio.wait_for(second_started.wait(), timeout=1)
|
||||
|
||||
snapshot = supervisor.snapshots()[0]
|
||||
self.assertEqual(snapshot.health.status.value, "unhealthy")
|
||||
self.assertEqual(snapshot.lifecycle, WorkerLifecycle.RUNNING)
|
||||
self.assertEqual(max_active, 1)
|
||||
|
||||
stopping = await runtime.stop(timeout_seconds=0.01)
|
||||
self.assertEqual(stopping.lifecycle, ApplicationLifecycle.STOPPING)
|
||||
self.assertEqual(stopping.workers.stopping, ("sync",))
|
||||
release_second.set()
|
||||
await asyncio.wait_for(self._wait_until_stopped(runtime), timeout=1)
|
||||
self.assertEqual(runtime.lifecycle, ApplicationLifecycle.STOPPED)
|
||||
self.assertEqual(calls, 2)
|
||||
|
||||
@staticmethod
|
||||
async def _wait_until_stopped(runtime: ApplicationRuntime) -> None:
|
||||
while runtime.lifecycle is not ApplicationLifecycle.STOPPED:
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def test_force_stop_cancels_active_cycle(self) -> None:
|
||||
active = asyncio.Event()
|
||||
cancelled = asyncio.Event()
|
||||
|
||||
async def process() -> None:
|
||||
active.set()
|
||||
try:
|
||||
await asyncio.Event().wait()
|
||||
except asyncio.CancelledError:
|
||||
cancelled.set()
|
||||
raise
|
||||
|
||||
registry = WorkerRegistry()
|
||||
registry.register(WorkerDefinition("blocking", process))
|
||||
supervisor = WorkerSupervisor(
|
||||
registry,
|
||||
FakeWorkerConfigReader({"blocking": WorkerConfig(interval_seconds=60)}),
|
||||
)
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
await runtime.start()
|
||||
await asyncio.wait_for(active.wait(), timeout=1)
|
||||
|
||||
result = await runtime.stop(force=True, timeout_seconds=1)
|
||||
|
||||
self.assertEqual(result.lifecycle, ApplicationLifecycle.STOPPED)
|
||||
self.assertTrue(cancelled.is_set())
|
||||
|
||||
async def test_invalid_enabled_schedule_fails_application_start(self) -> None:
|
||||
registry = WorkerRegistry()
|
||||
registry.register(WorkerDefinition("missing_schedule", WorkerRegistryTests._process))
|
||||
supervisor = WorkerSupervisor(registry, FakeWorkerConfigReader({}))
|
||||
runtime = ApplicationRuntime(RuntimeContext("service-a"), supervisor)
|
||||
|
||||
result = await runtime.start()
|
||||
|
||||
self.assertEqual(result.lifecycle, ApplicationLifecycle.FAILED)
|
||||
self.assertIn("missing_schedule", result.workers.failures)
|
||||
self.assertEqual(supervisor.snapshots()[0].lifecycle, WorkerLifecycle.FAILED)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,108 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
import unittest
|
||||
|
||||
from plba.settings import (
|
||||
PostgresSettingsRepository,
|
||||
SettingInvalidError,
|
||||
SettingKey,
|
||||
SettingNotFoundError,
|
||||
SettingsDatabaseUnavailableError,
|
||||
SettingsReader,
|
||||
)
|
||||
|
||||
|
||||
class FakeSettingsRepository:
|
||||
def __init__(self, values: list[str]) -> None:
|
||||
self.values = values
|
||||
self.calls: list[tuple[str, str | None]] = []
|
||||
|
||||
async def fetch_values(self, setting_key: str, config_key: str | None = None) -> list[str]:
|
||||
self.calls.append((setting_key, config_key))
|
||||
return list(self.values)
|
||||
|
||||
|
||||
class FakeConnection:
|
||||
def __init__(self, rows: list[Mapping[str, Any]] | None = None, error: Exception | None = None) -> None:
|
||||
self.rows = rows or []
|
||||
self.error = error
|
||||
self.query: str | None = None
|
||||
self.parameters: tuple[object, ...] = ()
|
||||
|
||||
async def fetch(self, query: str, *parameters: object) -> list[Mapping[str, Any]]:
|
||||
self.query = query
|
||||
self.parameters = parameters
|
||||
if self.error:
|
||||
raise self.error
|
||||
return self.rows
|
||||
|
||||
|
||||
class FakeAcquire:
|
||||
def __init__(self, connection: FakeConnection) -> None:
|
||||
self.connection = connection
|
||||
|
||||
async def __aenter__(self) -> FakeConnection:
|
||||
return self.connection
|
||||
|
||||
async def __aexit__(self, *_: object) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class FakePool:
|
||||
def __init__(self, connection: FakeConnection) -> None:
|
||||
self.connection = connection
|
||||
|
||||
def acquire(self) -> FakeAcquire:
|
||||
return FakeAcquire(self.connection)
|
||||
|
||||
|
||||
class SettingsReaderTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_reader_decodes_all_values_in_repository_order(self) -> None:
|
||||
repository = FakeSettingsRepository(["4", "9"])
|
||||
reader = SettingsReader(repository)
|
||||
key: SettingKey[int] = SettingKey("retry_count", int)
|
||||
|
||||
values = await reader.read(key, config_key="orders")
|
||||
|
||||
self.assertEqual(values, [4, 9])
|
||||
self.assertEqual(repository.calls, [("retry_count", "orders")])
|
||||
|
||||
async def test_reader_uses_optional_default_and_reports_missing_required(self) -> None:
|
||||
reader = SettingsReader(FakeSettingsRepository([]))
|
||||
optional: SettingKey[int] = SettingKey("limit", int, required=False, default=20)
|
||||
|
||||
self.assertEqual(await reader.read(optional), [20])
|
||||
with self.assertRaises(SettingNotFoundError):
|
||||
await reader.read(SettingKey("required", int))
|
||||
|
||||
async def test_reader_exposes_decoder_failure(self) -> None:
|
||||
reader = SettingsReader(FakeSettingsRepository(["bad-value"]))
|
||||
with self.assertRaises(SettingInvalidError) as raised:
|
||||
await reader.read(SettingKey("limit", int))
|
||||
self.assertIsInstance(raised.exception.__cause__, ValueError)
|
||||
|
||||
async def test_postgres_adapter_parameterizes_filters_and_orders(self) -> None:
|
||||
connection = FakeConnection([{"setting_value": "first"}, {"setting_value": "second"}])
|
||||
repository = PostgresSettingsRepository(FakePool(connection), "app_data")
|
||||
|
||||
values = await repository.fetch_values("theme", "north")
|
||||
|
||||
self.assertEqual(values, ["first", "second"])
|
||||
self.assertIn('"app_data".settings', connection.query or "")
|
||||
self.assertIn("ORDER BY sort_order ASC, id ASC", connection.query or "")
|
||||
self.assertEqual(connection.parameters, ("theme", "north"))
|
||||
self.assertNotIn("theme", connection.query or "")
|
||||
|
||||
async def test_postgres_adapter_rejects_unsafe_schema_and_wraps_db_errors(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
PostgresSettingsRepository(FakePool(FakeConnection()), 'data"; DROP TABLE settings;--')
|
||||
repository = PostgresSettingsRepository(
|
||||
FakePool(FakeConnection(error=RuntimeError("connection failed"))), "app_data"
|
||||
)
|
||||
with self.assertRaises(SettingsDatabaseUnavailableError) as raised:
|
||||
await repository.fetch_values("theme")
|
||||
self.assertIsInstance(raised.exception.__cause__, RuntimeError)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,128 @@
|
||||
import unittest
|
||||
from typing import Any
|
||||
|
||||
from plba.workflow import (
|
||||
StepResult,
|
||||
WorkflowContext,
|
||||
WorkflowDefinition,
|
||||
WorkflowEngine,
|
||||
WorkflowNode,
|
||||
WorkflowTransitionError,
|
||||
)
|
||||
|
||||
|
||||
class MemoryPersistence:
|
||||
def __init__(self) -> None:
|
||||
self.events: list[tuple[str, str, dict[str, Any]]] = []
|
||||
|
||||
async def record_run_started(self, run_id: str, workflow_name: str, context: WorkflowContext) -> None:
|
||||
self.events.append(("run_started", workflow_name, dict(context.state)))
|
||||
|
||||
async def record_step_started(self, run_id: str, step_name: str, context: WorkflowContext) -> None:
|
||||
self.events.append(("step_started", step_name, dict(context.state)))
|
||||
|
||||
async def record_step_completed(
|
||||
self, run_id: str, step_name: str, context: WorkflowContext, result: StepResult
|
||||
) -> None:
|
||||
self.events.append(("step_completed", step_name, dict(context.state)))
|
||||
|
||||
async def record_step_failed(
|
||||
self, run_id: str, step_name: str, context: WorkflowContext, error: BaseException
|
||||
) -> None:
|
||||
self.events.append(("step_failed", step_name, dict(context.state)))
|
||||
|
||||
async def record_run_completed(
|
||||
self, run_id: str, workflow_name: str, context: WorkflowContext, status: str
|
||||
) -> None:
|
||||
self.events.append(("run_completed", workflow_name, dict(context.state)))
|
||||
|
||||
async def record_run_failed(
|
||||
self, run_id: str, workflow_name: str, context: WorkflowContext, error: BaseException
|
||||
) -> None:
|
||||
self.events.append(("run_failed", workflow_name, dict(context.state)))
|
||||
|
||||
|
||||
class WorkflowEngineTests(unittest.IsolatedAsyncioTestCase):
|
||||
async def test_named_steps_follow_only_declared_transitions(self) -> None:
|
||||
persistence = MemoryPersistence()
|
||||
|
||||
async def first(context: WorkflowContext) -> StepResult:
|
||||
self.assertEqual(dict(context.state), {})
|
||||
return StepResult(transition="continue", updates={"count": 1})
|
||||
|
||||
async def second(context: WorkflowContext) -> StepResult:
|
||||
self.assertEqual(context.state["count"], 1)
|
||||
return StepResult(updates={"done": True})
|
||||
|
||||
definition = WorkflowDefinition(
|
||||
name="inventory_sync",
|
||||
initial_step="load",
|
||||
nodes={
|
||||
"load": WorkflowNode(first, {"continue": "save"}),
|
||||
"save": WorkflowNode(second),
|
||||
},
|
||||
)
|
||||
|
||||
result = await WorkflowEngine(persistence).run(definition, WorkflowContext())
|
||||
|
||||
self.assertEqual(result.status, "completed")
|
||||
self.assertEqual(dict(result.context.state), {"count": 1, "done": True})
|
||||
self.assertEqual(
|
||||
[event[0] for event in persistence.events],
|
||||
[
|
||||
"run_started",
|
||||
"step_started",
|
||||
"step_completed",
|
||||
"step_started",
|
||||
"step_completed",
|
||||
"run_completed",
|
||||
],
|
||||
)
|
||||
self.assertEqual(persistence.events[0][2], {})
|
||||
self.assertEqual(persistence.events[-1][2], {"count": 1, "done": True})
|
||||
|
||||
async def test_step_error_is_persisted_and_original_exception_propagates(self) -> None:
|
||||
persistence = MemoryPersistence()
|
||||
error = RuntimeError("step failed")
|
||||
later_called = False
|
||||
|
||||
async def fail(_: WorkflowContext) -> StepResult:
|
||||
raise error
|
||||
|
||||
async def later(_: WorkflowContext) -> StepResult:
|
||||
nonlocal later_called
|
||||
later_called = True
|
||||
return StepResult()
|
||||
|
||||
definition = WorkflowDefinition(
|
||||
"failure_case",
|
||||
"first",
|
||||
{"first": WorkflowNode(fail, {"next": "later"}), "later": WorkflowNode(later)},
|
||||
)
|
||||
|
||||
with self.assertRaises(RuntimeError) as raised:
|
||||
await WorkflowEngine(persistence).run(definition, WorkflowContext())
|
||||
|
||||
self.assertIs(raised.exception, error)
|
||||
self.assertFalse(later_called)
|
||||
self.assertEqual([event[0] for event in persistence.events][-2:], ["step_failed", "run_failed"])
|
||||
|
||||
async def test_undeclared_transition_fails_without_running_target(self) -> None:
|
||||
persistence = MemoryPersistence()
|
||||
|
||||
async def choose_unknown(_: WorkflowContext) -> StepResult:
|
||||
return StepResult(transition="unknown")
|
||||
|
||||
definition = WorkflowDefinition(
|
||||
"invalid_transition",
|
||||
"first",
|
||||
{"first": WorkflowNode(choose_unknown), "later": WorkflowNode(choose_unknown)},
|
||||
)
|
||||
|
||||
with self.assertRaises(WorkflowTransitionError):
|
||||
await WorkflowEngine(persistence).run(definition, WorkflowContext())
|
||||
self.assertEqual([event[0] for event in persistence.events][-2:], ["step_failed", "run_failed"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user