154 lines
5.4 KiB
Python
154 lines
5.4 KiB
Python
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()
|