Files
mtusi2026lab5-fork/experiments/tests/test_broker_readiness.py
T

63 lines
2.7 KiB
Python

"""Startup races must not be hidden during experimental cases."""
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from driver import Stack, HarnessError
class BrokerReadinessTests(unittest.TestCase):
def make_stack(self, root):
private = root / ".private" / "r1"
(private / "secrets").mkdir(parents=True)
return Stack("lab5-idem-unit", root / "config.json", private, {})
def test_booting_application_is_retried_before_warmup(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
stack = self.make_stack(root)
responses = [
subprocess.CompletedProcess([], 64, "", "this command requires the 'rabbit' app to be running"),
subprocess.CompletedProcess([], 0, "[]", ""),
]
with patch("driver.subprocess.run", side_effect=responses) as run, patch("driver.time.sleep"):
stack.wait_broker_ready(timeout=10)
self.assertEqual(run.call_count, 2)
self.assertEqual(json.loads((root / "broker-readiness-r1.json").read_text())["attempts"], 2)
self.assertEqual(len((root / "broker-startup.jsonl").read_text().splitlines()), 1)
def test_nonstartup_errors_fail_without_retry(self):
with tempfile.TemporaryDirectory() as temporary:
stack = self.make_stack(Path(temporary))
response = subprocess.CompletedProcess([], 70, "", "Authentication failed")
with patch("driver.subprocess.run", return_value=response) as run:
with self.assertRaises(HarnessError):
stack.wait_broker_ready(timeout=10)
self.assertEqual(run.call_count, 1)
def test_runtime_queue_probe_does_not_swallow_startup_or_outage(self):
with tempfile.TemporaryDirectory() as temporary:
stack = self.make_stack(Path(temporary))
response = subprocess.CompletedProcess([], 64, "", "requires the 'rabbit' app to be running")
with patch("driver.subprocess.run", return_value=response) as run:
with self.assertRaises(HarnessError):
stack.queue_depth()
self.assertEqual(run.call_count, 1)
def test_expired_setup_budget_does_not_start_a_probe(self):
with tempfile.TemporaryDirectory() as temporary:
stack = self.make_stack(Path(temporary))
with patch("driver.subprocess.run") as run:
with self.assertRaises(HarnessError):
stack.wait_broker_ready(timeout=0)
run.assert_not_called()
if __name__ == "__main__":
unittest.main()