This commit is contained in:
Your Name
2026-04-22 14:13:21 +08:00
parent e0406b5d0e
commit 7cbde2aa78
145 changed files with 23086 additions and 2243 deletions

View File

@@ -1,6 +1,10 @@
import unittest
from unittest.mock import patch
from app.services.sync_record_service import _collect_recent_domain_events
from app.services.sync_record_service import (
_build_runtime_projection_payload,
_collect_recent_domain_events,
)
class SyncRecordServiceTests(unittest.TestCase):
@@ -50,6 +54,82 @@ class SyncRecordServiceTests(unittest.TestCase):
self.assertEqual("domain_completed", events[1]["event_type"])
self.assertEqual("a.com", events[1]["payload"]["domain"])
@patch("app.services.sync_record_service._resolve_local_ip", return_value="152.53.37.118")
@patch("app.services.sync_record_service.socket.gethostname", return_value="overseas-control-01")
@patch("app.services.sync_record_service.settings")
def test_build_runtime_projection_payload_zeros_execution_for_overseas_control(
self,
mock_settings,
_mock_hostname,
_mock_resolve_ip,
) -> None:
mock_settings.node_code = "overseas-control-01"
mock_settings.node_region = "overseas"
mock_settings.node_role = "control"
mock_settings.sync_source_region = "overseas"
mock_settings.sync_target_region = "mainland"
payload = _build_runtime_projection_payload(
detect={
"worker_online": False,
"worker_mode": "linux-systemd",
"active_thread_count": 0,
"max_thread_count": 0,
"phase_label": "当前节点不承载",
"phase_detail": "当前节点为海外控制面,仅承载 API 控制与同步接收,不执行本机检测任务。",
"proxy_runtime_label": "不适用",
"proxy_runtime_reason": "not_applicable",
"progress": {
"pending": 125,
"running": 293,
"completed": 71,
"blacklisted": 0,
"failed": 1,
},
"active_job": {
"job_id": 2027,
"job_code": "sync-overseas-33501",
"status": "running",
"progress_percent": 7.2,
"items_total": 1000,
"items_terminal": 72,
"items_pending": 125,
"items_running": 293,
"items_failed": 1,
"node_stats": [{"node_code": "mainland-controller-01", "items_running": 293}],
},
},
cluster={
"nodes_total": 3,
"nodes": [
{
"node_code": "overseas-control-01",
"current_load": 293,
"detect_participating": True,
}
],
"summary": {
"online_worker_nodes": 3,
"dedicated_online_worker_nodes": 1,
"online_control_nodes": 2,
"busy_nodes": ["overseas-control-01"],
"stale_nodes": [],
"offline_nodes": [],
},
},
source_region="overseas",
target_region="mainland",
)
projection = payload["projection"]
self.assertFalse(projection["worker_online"])
self.assertFalse(projection["detect_participating"])
self.assertEqual(0, projection["active_thread_count"])
self.assertEqual(0, projection["max_thread_count"])
self.assertEqual(0, projection["progress"]["running"])
self.assertEqual("", projection["active_job"]["job_code"])
self.assertEqual([], projection["active_job"]["node_stats"])
if __name__ == "__main__":
unittest.main()